Submission #660124

# Submission time Handle Problem Language Result Execution time Memory
660124 2022-11-20T12:05:56 Z jamezzz Joker (BOI20_joker) C++17
100 / 100
306 ms 14928 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define sf scanf
#define pf printf
#define pb push_back
typedef pair<int,int> ii;

#define maxn 200005

int n,m,M,q,u[maxn],v[maxn],far[maxn],joined[maxn];
int par[maxn],rnk[maxn],flip[maxn];
vector<pair<ii,ii>> hist;

int fp(int i){
	while(par[i]!=i)i=par[i];
	return i;
}

int col(int i){
	int res=0;
	while(par[i]!=i)res^=flip[i],i=par[i];
	return res;
}

bool join(int x,int y,bool f){
	x=fp(x),y=fp(y);
	if(x==y)return false;
	if(rnk[x]<rnk[y])swap(x,y);
	hist.pb({{x,rnk[x]},{y,rnk[y]}});
	par[y]=x;
	if(rnk[x]==rnk[y])++rnk[x];
	if(f)flip[y]=true;
	return true;
}

void undo(int i){
	if(!joined[i])return;
	joined[i]=false;
	assert(!hist.empty());
	auto[x,rx]=hist.back().fi;
	auto[y,ry]=hist.back().se;
	hist.pop_back();
	par[x]=x;par[y]=y;
	rnk[x]=rx;rnk[y]=ry;
	flip[x]=flip[y]=false;
}

bool tryjoin(int i){
	int cu=col(u[i]),cv=col(v[i]);
	int pu=fp(u[i]),pv=fp(v[i]);
	if(pu==pv&&cu==cv)return false;
	joined[i]=join(u[i],v[i],cu==cv);
	return true;
}

void dnc(int l,int r,int optl,int optr){
	//pf("dnc: %d %d, %d %d\n",l,r,optl,optr);
	if(l>r||optl>optr)return;
	int m=(l+r)>>1;
	//try to find answer for m
	for(int i=l;i<=m;++i)tryjoin(i);
	//find first pos that will win
	for(int i=optr;i>=optl;--i){
		if(!tryjoin(i)){
			far[m]=i;
			break;
		}
	}
	//pf("far %d: %d\n",m,far[m]);
	for(int i=far[m]+1;i<=optr;++i)undo(i);
	for(int i=m;i>=l;--i)undo(i);
	for(int i=optr;i>far[m];--i)tryjoin(i);
	dnc(l,m-1,optl,far[m]);
	for(int i=far[m]+1;i<=optr;++i)undo(i);
	for(int i=l;i<=m;++i)tryjoin(i);
	dnc(m+1,r,max(far[m],m+1),optr);
	for(int i=m;i>=l;--i)undo(i);
}

int main(){
	sf("%d%d%d",&n,&m,&q);
	M=m;
	for(int i=1;i<=m;++i){
		sf("%d%d",&u[i],&v[i]);
	}
	for(int i=1;i<=n;++i){
		par[i]=i,rnk[i]=0,flip[i]=0;
	}
	for(int i=1;i<=m;++i)far[i]=m+1;
	int lst=-1;
	for(int i=1;i<=m;++i){
		if(!tryjoin(i))break;
		lst=i;
	}
	for(int i=1;i<=lst;++i)far[i]=-1;
	//>lst is autowin
	for(int i=lst;i>=1;--i)undo(i);
	far[0]=0;
	for(int i=m;i>=1;--i){
		if(!tryjoin(i)){
			far[0]=i;
			break;
		}
	}
	for(int i=far[0]+1;i<=m;++i)undo(i);
	dnc(1,lst,1,m);
	for(int i=0;i<q;++i){
		int l,r;sf("%d%d",&l,&r);
		if(r>=far[l-1])pf("NO\n");
		else pf("YES\n");
	}
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:84:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  sf("%d%d%d",&n,&m,&q);
      |    ^
Joker.cpp:87:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |   sf("%d%d",&u[i],&v[i]);
      |     ^
Joker.cpp:111:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   int l,r;sf("%d%d",&l,&r);
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 444 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 448 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 448 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 452 KB Output is correct
41 Correct 3 ms 444 KB Output is correct
42 Correct 3 ms 364 KB Output is correct
43 Correct 3 ms 364 KB Output is correct
44 Correct 2 ms 360 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 380 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 182 ms 8352 KB Output is correct
4 Correct 250 ms 13140 KB Output is correct
5 Correct 197 ms 13264 KB Output is correct
6 Correct 179 ms 10900 KB Output is correct
7 Correct 174 ms 10896 KB Output is correct
8 Correct 252 ms 9148 KB Output is correct
9 Correct 237 ms 10140 KB Output is correct
10 Correct 279 ms 11732 KB Output is correct
11 Correct 238 ms 10660 KB Output is correct
12 Correct 233 ms 12092 KB Output is correct
13 Correct 222 ms 8588 KB Output is correct
14 Correct 254 ms 9896 KB Output is correct
15 Correct 257 ms 11320 KB Output is correct
16 Correct 295 ms 12200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 182 ms 8352 KB Output is correct
30 Correct 250 ms 13140 KB Output is correct
31 Correct 197 ms 13264 KB Output is correct
32 Correct 179 ms 10900 KB Output is correct
33 Correct 174 ms 10896 KB Output is correct
34 Correct 252 ms 9148 KB Output is correct
35 Correct 237 ms 10140 KB Output is correct
36 Correct 279 ms 11732 KB Output is correct
37 Correct 238 ms 10660 KB Output is correct
38 Correct 233 ms 12092 KB Output is correct
39 Correct 222 ms 8588 KB Output is correct
40 Correct 254 ms 9896 KB Output is correct
41 Correct 257 ms 11320 KB Output is correct
42 Correct 295 ms 12200 KB Output is correct
43 Correct 190 ms 11156 KB Output is correct
44 Correct 209 ms 13628 KB Output is correct
45 Correct 300 ms 14092 KB Output is correct
46 Correct 155 ms 11180 KB Output is correct
47 Correct 175 ms 11160 KB Output is correct
48 Correct 243 ms 10768 KB Output is correct
49 Correct 283 ms 12312 KB Output is correct
50 Correct 210 ms 10568 KB Output is correct
51 Correct 233 ms 11676 KB Output is correct
52 Correct 249 ms 12484 KB Output is correct
53 Correct 234 ms 8840 KB Output is correct
54 Correct 254 ms 10524 KB Output is correct
55 Correct 258 ms 11792 KB Output is correct
56 Correct 293 ms 12524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 444 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 448 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 448 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 452 KB Output is correct
41 Correct 3 ms 444 KB Output is correct
42 Correct 3 ms 364 KB Output is correct
43 Correct 3 ms 364 KB Output is correct
44 Correct 2 ms 360 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 380 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 444 KB Output is correct
55 Correct 163 ms 8520 KB Output is correct
56 Correct 157 ms 12212 KB Output is correct
57 Correct 160 ms 12036 KB Output is correct
58 Correct 125 ms 8540 KB Output is correct
59 Correct 206 ms 8556 KB Output is correct
60 Correct 259 ms 9528 KB Output is correct
61 Correct 198 ms 8644 KB Output is correct
62 Correct 229 ms 9804 KB Output is correct
63 Correct 175 ms 6844 KB Output is correct
64 Correct 268 ms 8912 KB Output is correct
65 Correct 236 ms 9768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 304 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 308 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 444 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 2 ms 448 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 448 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 492 KB Output is correct
40 Correct 2 ms 452 KB Output is correct
41 Correct 3 ms 444 KB Output is correct
42 Correct 3 ms 364 KB Output is correct
43 Correct 3 ms 364 KB Output is correct
44 Correct 2 ms 360 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 380 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 444 KB Output is correct
55 Correct 182 ms 8352 KB Output is correct
56 Correct 250 ms 13140 KB Output is correct
57 Correct 197 ms 13264 KB Output is correct
58 Correct 179 ms 10900 KB Output is correct
59 Correct 174 ms 10896 KB Output is correct
60 Correct 252 ms 9148 KB Output is correct
61 Correct 237 ms 10140 KB Output is correct
62 Correct 279 ms 11732 KB Output is correct
63 Correct 238 ms 10660 KB Output is correct
64 Correct 233 ms 12092 KB Output is correct
65 Correct 222 ms 8588 KB Output is correct
66 Correct 254 ms 9896 KB Output is correct
67 Correct 257 ms 11320 KB Output is correct
68 Correct 295 ms 12200 KB Output is correct
69 Correct 190 ms 11156 KB Output is correct
70 Correct 209 ms 13628 KB Output is correct
71 Correct 300 ms 14092 KB Output is correct
72 Correct 155 ms 11180 KB Output is correct
73 Correct 175 ms 11160 KB Output is correct
74 Correct 243 ms 10768 KB Output is correct
75 Correct 283 ms 12312 KB Output is correct
76 Correct 210 ms 10568 KB Output is correct
77 Correct 233 ms 11676 KB Output is correct
78 Correct 249 ms 12484 KB Output is correct
79 Correct 234 ms 8840 KB Output is correct
80 Correct 254 ms 10524 KB Output is correct
81 Correct 258 ms 11792 KB Output is correct
82 Correct 293 ms 12524 KB Output is correct
83 Correct 163 ms 8520 KB Output is correct
84 Correct 157 ms 12212 KB Output is correct
85 Correct 160 ms 12036 KB Output is correct
86 Correct 125 ms 8540 KB Output is correct
87 Correct 206 ms 8556 KB Output is correct
88 Correct 259 ms 9528 KB Output is correct
89 Correct 198 ms 8644 KB Output is correct
90 Correct 229 ms 9804 KB Output is correct
91 Correct 175 ms 6844 KB Output is correct
92 Correct 268 ms 8912 KB Output is correct
93 Correct 236 ms 9768 KB Output is correct
94 Correct 192 ms 11948 KB Output is correct
95 Correct 306 ms 14648 KB Output is correct
96 Correct 217 ms 14232 KB Output is correct
97 Correct 193 ms 11988 KB Output is correct
98 Correct 167 ms 11964 KB Output is correct
99 Correct 250 ms 10900 KB Output is correct
100 Correct 226 ms 13292 KB Output is correct
101 Correct 222 ms 11328 KB Output is correct
102 Correct 253 ms 12472 KB Output is correct
103 Correct 270 ms 13496 KB Output is correct
104 Correct 219 ms 10276 KB Output is correct
105 Correct 271 ms 12012 KB Output is correct
106 Correct 300 ms 12992 KB Output is correct
107 Correct 170 ms 14928 KB Output is correct
108 Correct 190 ms 11640 KB Output is correct
109 Correct 180 ms 11632 KB Output is correct
110 Correct 184 ms 11668 KB Output is correct
111 Correct 199 ms 11740 KB Output is correct
112 Correct 188 ms 11748 KB Output is correct
113 Correct 197 ms 11672 KB Output is correct
114 Correct 212 ms 11704 KB Output is correct
115 Correct 188 ms 11684 KB Output is correct
116 Correct 192 ms 11712 KB Output is correct