QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#30509#2559. Endless RoadY25tWA 8ms60608kbC++203.0kb2022-04-29 16:26:392022-04-29 16:26:40

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 16:26:40]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:60608kb
  • [2022-04-29 16:26:39]
  • 提交

answer

#include<bits/stdc++.h>
#define N 500005

int n,a[N],b[N];

std::map<int,int> vl,vr;
int val[N],tot;

int T[N];
inline int lb(int x){
	return x&-x;
}
inline void add(int x,int d){
	for(;x<tot;x+=lb(x))
		T[x]+=d;
}
inline int sum(int x){
	int res=0;
	for(x--;x;x-=lb(x))
		res+=T[x];
	return res;
}

int f[N];
inline int fnd(int x){
	return f[x]?f[x]=fnd(f[x]):x;
}
inline void del(int i){
	for(int x=fnd(a[i]);x<b[i];x=fnd(x))
		add(x,-(val[x+1]-val[x])),f[x]=fnd(x+1);
}
inline int len(int i){
	return sum(b[i])-sum(a[i]);
}

struct sq{
	int xl,xr,yl,yr;
	sq(int x=0,int y=0,int z=0,int w=0){
		xl=x,xr=y,yl=z,yr=w;
	}
};
struct node{
	int mn,mxl,mnr;
	node(int x=0,int y=0,int z=0){
		mn=x,mxl=y,mnr=z;
	}
};
node operator + (node x,node y){
	if(!x.mn||!y.mn)
		return x.mn?x:y;
	return node(std::min(x.mn,y.mn),a[x.mxl]>a[y.mxl]?x.mxl:y.mxl,b[x.mnr]<b[y.mnr]?x.mnr:y.mnr);
}
struct tr{
	sq s;
	node x;
};

tr t[N<<2];
inline void build(int p,std::vector<int> V,int o){
	if((int)V.size()==1){
		int i=V.back();
		t[p].s=sq(a[i],a[i],b[i],b[i]);
		t[p].x=node(i,i,i);
		return;
	}
	int mid=V.size()>>1;
	std::nth_element(V.begin(),V.begin()+mid,V.end(),[&](int i,int j){
		return o?a[i]<a[j]:b[i]<b[j];
	});
	build(p<<1,std::vector<int>(V.begin(),V.begin()+mid),o^1);
	build(p<<1|1,std::vector<int>(V.begin()+mid,V.end()),o^1);
	auto uni=[&](sq x,sq y)->sq{
		return {std::min(x.xl,y.xl),std::max(x.xr,y.xr),
				std::min(x.yl,y.yl),std::max(x.yr,y.yr)};
	};
	t[p].s=uni(t[p<<1].s,t[p<<1|1].s);
	t[p].x=t[p<<1].x+t[p<<1|1].x;
}
inline void ers(int p,int x,int y){
	if(x<t[p].s.xl||x>t[p].s.xr||y<t[p].s.yl||y>t[p].s.yr)
		return;
	if(t[p].s.xl==t[p].s.xr&&t[p].s.yl==t[p].s.yr)
		return t[p].x=node(),void();
	ers(p<<1,x,y),ers(p<<1|1,x,y);
	t[p].x=t[p<<1].x+t[p<<1|1].x;
}
inline node sum(int p,sq s){
	if(t[p].s.xl>s.xr||t[p].s.xr<s.xl||t[p].s.yl>s.yr||t[p].s.yr<s.yl)
		return node();
	if(s.xl<=t[p].s.xl&&t[p].s.xr<=s.xr&&s.yl<=t[p].s.yl&&t[p].s.yr<=s.yr)
		return t[p].x;
	return sum(p<<1,s)+sum(p<<1|1,s);
}

#define pii std::pair<int,int>
std::priority_queue<pii,std::vector<pii>,std::greater<pii>> q;
bool vis[N];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vl[a[i]]++,vl[b[i]]++;
	}
	for(auto &[x,y]:vl){
		while(y--)
			val[++tot]=x;
		y=tot;
	}
	vr=vl;
	for(int i=1;i<=n;i++)
		a[i]=vl[a[i]]--,b[i]=vl[b[i]]--;
	for(int i=1;i<tot;i++)
		add(i,val[i+1]-val[i]);
	std::vector<int> V(n);
	std::iota(V.begin(),V.end(),1);
	build(1,V,0);
	for(int i=1;i<=n;i++)
		q.emplace(len(i),i);
	while(q.size()){
		int x=q.top().second;
		q.pop();
		if(vis[x])
			continue;
		printf("%d ",x);
		vis[x]=1,del(x),ers(1,a[x],b[x]);
		int y=sum(1,sq(1,vl[a[x]]-1,vr[b[x]]+1,tot)).mn;
		if(y)
			q.emplace(len(y),y);
		y=sum(1,sq(1,vr[a[x]],1,vr[b[x]])).mxl;
		if(y)
			q.emplace(len(y),y);
		y=sum(1,sq(vl[a[x]],tot,vl[b[x]],tot)).mnr;
		if(y)
			q.emplace(len(y),y);
	}
	puts("");
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 60504kb

input:

6
1 2
2 3
3 4
4 5
1 3
3 5

output:

1 2 5 3 4 6 

result:

ok 6 tokens

Test #2:

score: 0
Accepted
time: 4ms
memory: 60608kb

input:

4
3 7
10 14
1 6
6 11

output:

1 3 2 4 

result:

ok 4 tokens

Test #3:

score: 0
Accepted
time: 3ms
memory: 60576kb

input:

100
50 51
49 51
49 52
48 52
48 53
47 53
47 54
46 54
46 55
45 55
45 56
44 56
44 57
43 57
43 58
42 58
42 59
41 59
41 60
40 60
40 61
39 61
39 62
38 62
38 63
37 63
37 64
36 64
36 65
35 65
35 66
34 66
34 67
33 67
33 68
32 68
32 69
31 69
31 70
30 70
30 71
29 71
29 72
28 72
28 73
27 73
27 74
26 74
26 75
25...

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 

result:

ok 100 tokens

Test #4:

score: -100
Wrong Answer
time: 8ms
memory: 60588kb

input:

100
41 42
99 100
47 48
50 51
56 57
61 62
27 28
85 86
44 45
3 4
26 27
20 21
92 93
33 34
86 87
69 70
84 85
62 63
81 82
2 3
13 14
32 33
82 83
70 71
46 47
45 46
19 20
83 84
57 59
63 65
59 61
82 84
45 47
48 50
70 72
42 44
84 86
26 28
61 63
2 4
17 19
65 67
54 56
67 69
96 99
42 45
47 50
34 37
14 17
51 54
7...

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 40 24 25 26 27 28 32 29 30 31 33 34 35 36 37 38 46 71 39 41 42 43 44 45 47 48 77 49 50 51 52 53 54 55 60 63 56 57 58 59 61 89 62 66 64 65 67 72 79 96 68 82 69 70 75 80 87 90 94 81 86 88 91 73 83 97 93 99 74 84 76 78 95 85 100 98 92 

result:

wrong answer 12th words differ - expected: '38', found: '12'