Submission #893168

# Submission time Handle Problem Language Result Execution time Memory
893168 2023-12-26T15:27:33 Z amirhoseinfar1385 Joker (BOI20_joker) C++17
100 / 100
151 ms 12032 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
int n,m,q,dp[maxn],f;
pair<int,int>alle[maxn];

struct dsu{
	vector<int>ha;
	int par[maxn],sz[maxn],val[maxn];
	dsu(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
			sz[i]=1;
		}
	}
	int root(int u){
		if(par[u]==u){
			return u;
		}
		return root(par[u]);
	}
	int len(int u){
		if(u==par[u]){
			return 0;
		}
		return len(par[u])+val[u];
	}

	void con(int u,int v){
		int rootu=root(u),rootv=root(v);
		if(rootu!=rootv){
			if(sz[rootu]<sz[rootv]){
				swap(rootu,rootv);
				swap(u,v);
			}
			int lu=len(u),lv=len(v);
			par[rootv]=rootu;
			sz[rootu]+=sz[rootv];
			if((lu&1)==(lv&1)){
				val[rootv]=1;
			}
			else{
				val[rootv]=0;
			}
			ha.push_back(rootv);
//			if(f)
//			cout<<"con: "<<rootu<<" "<<rootv<<" "<<val[rootv]<<" "<<u<<" "<<v<<" "<<lu<<" "<<lv<<"\n";
			return ;
		}
		ha.push_back(-1);
	}
	bool two(int u,int v){
		int rootu=root(u),rootv=root(v);
		if(rootu!=rootv){
			return 0;
		}
		int lu=len(u),lv=len(v);
		if((lu&1)==(lv&1)){
			return 1;
		}
		return 0;
	}
	void back(){
		if((int)ha.back()==-1){
			ha.pop_back();
			return ;
		}
		int x=ha.back();
		sz[par[x]]-=sz[x];
		par[x]=x;
		val[x]=0;
		ha.pop_back();
		return ;
	}
}ds;

void solve(int l,int r,int tl,int tr){
	if(l>r){
		return ;
	}
	if(tl==tr){
		for(int i=l;i<=r;i++){
			dp[i]=tl;
		}
		return ;
	}
	int opt=0;
	int now=ds.ha.size();
	int mid=(l+r)>>1;
	for(int i=l;i<=mid;i++){
		ds.con(alle[i].first,alle[i].second);
	}
	for(int i=tr;i>=tl;i--){
		if(ds.two(alle[i].first,alle[i].second)){
			opt=i;
			break;
		}
		ds.con(alle[i].first,alle[i].second);
	}
//	cout<<l<<" "<<r<<" "<<tl<<" "<<tr<<" "<<mid<<" "<<opt<<" "<<now<<"\n";
	dp[mid]=opt;
	while((int)ds.ha.size()>now){
		ds.back();
	}
	for(int i=l;i<=mid;i++){
		ds.con(alle[i].first,alle[i].second);
	}
	solve(mid+1,r,opt,tr);
	while((int)ds.ha.size()>now){
		ds.back();
	}
	for(int i=tr;i>opt;i--){
		ds.con(alle[i].first,alle[i].second);
	}
	solve(l,mid-1,tl,opt);
	while((int)ds.ha.size()>now){
		ds.back();
	}
}

int pre(){
	for(int i=1;i<=m;i++){
		if(ds.two(alle[i].first,alle[i].second)){
			return i;
		}
		ds.con(alle[i].first,alle[i].second);
	}
	return m+1;
}

int suf(){
	for(int i=m;i>=0;i--){
//		cout<<alle[i].first<<" "<<alle[i].second<<" "<<ds.two(alle[i].first,alle[i].second)<<"\n";
		if(ds.two(alle[i].first,alle[i].second)){
			return i;
		}
		ds.con(alle[i].first,alle[i].second);
	}
	return 0;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);	
	cin>>n>>m>>q;
	for(int i=1;i<=m;i++){
		cin>>alle[i].first>>alle[i].second;
	}
	int ind=pre();
	while((int)ds.ha.size()>0){
		ds.back();
	}
	solve(1,ind-1,0,m);
	for(int i=ind;i<=m;i++){
		dp[i]=m+2;
	}
//	cout<<"ind: "<<ind<<"\n";
//	for(int i=1;i<=m;i++){
//		cout<<dp[i]<<" "<<i<<"\n";
//	}
//	cout<<"\n";
	if((int)ds.ha.size()!=0){
		assert(0);
	}
//	f=1;
//	cout<<"wtf: \n";
//	for(int i=1;i<=n;i++){
//		cout<<i<<" "<<ds.par[i]<<" "<<ds.sz[i]<<" "<<ds.val[i]<<"\n";
//	}
	dp[0]=suf();
	//cout<<"dp0: "<<dp[0]<<endl;
	for(int i=0;i<q;i++){
		int l,r;
		cin>>l>>r;
		if(dp[l-1]<=r){
			cout<<"NO\n";
		}
		else{
			cout<<"YES\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4952 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4696 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4952 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4696 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 2 ms 4700 KB Output is correct
34 Correct 2 ms 4700 KB Output is correct
35 Correct 2 ms 4696 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4700 KB Output is correct
38 Correct 2 ms 4700 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 2 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 2 ms 4700 KB Output is correct
45 Correct 2 ms 4700 KB Output is correct
46 Correct 2 ms 4696 KB Output is correct
47 Correct 2 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4700 KB Output is correct
50 Correct 2 ms 4700 KB Output is correct
51 Correct 2 ms 4700 KB Output is correct
52 Correct 2 ms 4700 KB Output is correct
53 Correct 2 ms 4700 KB Output is correct
54 Correct 2 ms 4748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 110 ms 6092 KB Output is correct
4 Correct 151 ms 7232 KB Output is correct
5 Correct 86 ms 11216 KB Output is correct
6 Correct 79 ms 10192 KB Output is correct
7 Correct 78 ms 10092 KB Output is correct
8 Correct 105 ms 9588 KB Output is correct
9 Correct 118 ms 9568 KB Output is correct
10 Correct 133 ms 9776 KB Output is correct
11 Correct 111 ms 10228 KB Output is correct
12 Correct 121 ms 10444 KB Output is correct
13 Correct 99 ms 10088 KB Output is correct
14 Correct 111 ms 10160 KB Output is correct
15 Correct 129 ms 10188 KB Output is correct
16 Correct 139 ms 10296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4952 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4696 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 110 ms 6092 KB Output is correct
30 Correct 151 ms 7232 KB Output is correct
31 Correct 86 ms 11216 KB Output is correct
32 Correct 79 ms 10192 KB Output is correct
33 Correct 78 ms 10092 KB Output is correct
34 Correct 105 ms 9588 KB Output is correct
35 Correct 118 ms 9568 KB Output is correct
36 Correct 133 ms 9776 KB Output is correct
37 Correct 111 ms 10228 KB Output is correct
38 Correct 121 ms 10444 KB Output is correct
39 Correct 99 ms 10088 KB Output is correct
40 Correct 111 ms 10160 KB Output is correct
41 Correct 129 ms 10188 KB Output is correct
42 Correct 139 ms 10296 KB Output is correct
43 Correct 107 ms 10452 KB Output is correct
44 Correct 147 ms 11444 KB Output is correct
45 Correct 126 ms 11016 KB Output is correct
46 Correct 76 ms 10452 KB Output is correct
47 Correct 82 ms 10608 KB Output is correct
48 Correct 112 ms 10196 KB Output is correct
49 Correct 144 ms 10448 KB Output is correct
50 Correct 114 ms 10452 KB Output is correct
51 Correct 115 ms 10704 KB Output is correct
52 Correct 111 ms 10704 KB Output is correct
53 Correct 112 ms 10392 KB Output is correct
54 Correct 116 ms 10388 KB Output is correct
55 Correct 124 ms 10516 KB Output is correct
56 Correct 144 ms 10804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4952 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4696 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 2 ms 4700 KB Output is correct
34 Correct 2 ms 4700 KB Output is correct
35 Correct 2 ms 4696 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4700 KB Output is correct
38 Correct 2 ms 4700 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 2 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 2 ms 4700 KB Output is correct
45 Correct 2 ms 4700 KB Output is correct
46 Correct 2 ms 4696 KB Output is correct
47 Correct 2 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4700 KB Output is correct
50 Correct 2 ms 4700 KB Output is correct
51 Correct 2 ms 4700 KB Output is correct
52 Correct 2 ms 4700 KB Output is correct
53 Correct 2 ms 4700 KB Output is correct
54 Correct 2 ms 4748 KB Output is correct
55 Correct 84 ms 7872 KB Output is correct
56 Correct 123 ms 9448 KB Output is correct
57 Correct 49 ms 8436 KB Output is correct
58 Correct 50 ms 7888 KB Output is correct
59 Correct 90 ms 7888 KB Output is correct
60 Correct 118 ms 8148 KB Output is correct
61 Correct 91 ms 7888 KB Output is correct
62 Correct 102 ms 8344 KB Output is correct
63 Correct 73 ms 7888 KB Output is correct
64 Correct 95 ms 7868 KB Output is correct
65 Correct 116 ms 8068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4952 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4952 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4696 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4696 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4696 KB Output is correct
25 Correct 1 ms 4700 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 2 ms 4696 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 2 ms 4700 KB Output is correct
34 Correct 2 ms 4700 KB Output is correct
35 Correct 2 ms 4696 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4700 KB Output is correct
38 Correct 2 ms 4700 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 2 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 2 ms 4700 KB Output is correct
45 Correct 2 ms 4700 KB Output is correct
46 Correct 2 ms 4696 KB Output is correct
47 Correct 2 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4700 KB Output is correct
50 Correct 2 ms 4700 KB Output is correct
51 Correct 2 ms 4700 KB Output is correct
52 Correct 2 ms 4700 KB Output is correct
53 Correct 2 ms 4700 KB Output is correct
54 Correct 2 ms 4748 KB Output is correct
55 Correct 110 ms 6092 KB Output is correct
56 Correct 151 ms 7232 KB Output is correct
57 Correct 86 ms 11216 KB Output is correct
58 Correct 79 ms 10192 KB Output is correct
59 Correct 78 ms 10092 KB Output is correct
60 Correct 105 ms 9588 KB Output is correct
61 Correct 118 ms 9568 KB Output is correct
62 Correct 133 ms 9776 KB Output is correct
63 Correct 111 ms 10228 KB Output is correct
64 Correct 121 ms 10444 KB Output is correct
65 Correct 99 ms 10088 KB Output is correct
66 Correct 111 ms 10160 KB Output is correct
67 Correct 129 ms 10188 KB Output is correct
68 Correct 139 ms 10296 KB Output is correct
69 Correct 107 ms 10452 KB Output is correct
70 Correct 147 ms 11444 KB Output is correct
71 Correct 126 ms 11016 KB Output is correct
72 Correct 76 ms 10452 KB Output is correct
73 Correct 82 ms 10608 KB Output is correct
74 Correct 112 ms 10196 KB Output is correct
75 Correct 144 ms 10448 KB Output is correct
76 Correct 114 ms 10452 KB Output is correct
77 Correct 115 ms 10704 KB Output is correct
78 Correct 111 ms 10704 KB Output is correct
79 Correct 112 ms 10392 KB Output is correct
80 Correct 116 ms 10388 KB Output is correct
81 Correct 124 ms 10516 KB Output is correct
82 Correct 144 ms 10804 KB Output is correct
83 Correct 84 ms 7872 KB Output is correct
84 Correct 123 ms 9448 KB Output is correct
85 Correct 49 ms 8436 KB Output is correct
86 Correct 50 ms 7888 KB Output is correct
87 Correct 90 ms 7888 KB Output is correct
88 Correct 118 ms 8148 KB Output is correct
89 Correct 91 ms 7888 KB Output is correct
90 Correct 102 ms 8344 KB Output is correct
91 Correct 73 ms 7888 KB Output is correct
92 Correct 95 ms 7868 KB Output is correct
93 Correct 116 ms 8068 KB Output is correct
94 Correct 116 ms 11220 KB Output is correct
95 Correct 149 ms 11724 KB Output is correct
96 Correct 81 ms 11872 KB Output is correct
97 Correct 81 ms 11220 KB Output is correct
98 Correct 89 ms 11160 KB Output is correct
99 Correct 108 ms 11292 KB Output is correct
100 Correct 107 ms 11476 KB Output is correct
101 Correct 118 ms 11096 KB Output is correct
102 Correct 128 ms 11216 KB Output is correct
103 Correct 139 ms 11352 KB Output is correct
104 Correct 109 ms 11244 KB Output is correct
105 Correct 139 ms 11212 KB Output is correct
106 Correct 145 ms 11472 KB Output is correct
107 Correct 66 ms 12032 KB Output is correct
108 Correct 113 ms 10896 KB Output is correct
109 Correct 131 ms 10888 KB Output is correct
110 Correct 109 ms 10984 KB Output is correct
111 Correct 112 ms 10964 KB Output is correct
112 Correct 113 ms 10904 KB Output is correct
113 Correct 113 ms 10964 KB Output is correct
114 Correct 110 ms 10964 KB Output is correct
115 Correct 110 ms 11076 KB Output is correct
116 Correct 122 ms 10960 KB Output is correct