Submission #764562

# Submission time Handle Problem Language Result Execution time Memory
764562 2023-06-23T15:17:57 Z MetalPower Joker (BOI20_joker) C++14
100 / 100
284 ms 18152 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define fi first
#define se second

const int MX = 2e5 + 10;

// Let dp[l] be the maximum index such that removing [l, dp[l]) doesn't make the graph bipartite
// Observe that dp[i] <= dp[i + 1] <= dp[i + 2]
// Hence we can do a dnc-esque solution

// also to make the complexity much lower, we can do the 2-pointer cost usually used for dp-dnc

struct dsu{
	int p[MX << 1], sz[MX << 1];
	vector<pii> operations;

	void init(){
		for(int i = 0; i < (MX << 1); i++) p[i] = i, sz[i] = 1;
	}

	int f(int x){
		if(p[x] == x) return x;
		else return f(p[x]);
	}

	int Join(int u, int v){
		int fu = f(u), fv = f(v);
		if(fu == fv) return 0;
		if(sz[fu] < sz[fv]){
			operations.push_back({p[fu], fu});
			p[fu] = fv;
			operations.push_back({sz[fv], fv});
			sz[fv] += sz[fu];
		}else{
			operations.push_back({p[fv], fv});
			p[fv] = fu;
			operations.push_back({sz[fu], fu});
			sz[fu] += sz[fv];
		}
		return 1;
	}

	void rev(int x){
		for(int i = 0; i < x; i++){
			{
				pii bk = operations.back();
				sz[bk.se] = bk.fi;
				operations.pop_back();
			}
			{
				pii bk = operations.back();
				p[bk.se] = bk.fi;
				operations.pop_back();
			}
		}
	}

	bool con(int x){
		return f(x * 2) == f(x * 2 + 1);
	}
} D;

int N, M, Q;
vector<pii> edges;
int lf = 0, rg = -1, dp[MX];

void solve(int l, int r, int optl, int optr){

	if(r < l) return;
	// cout << "solve " << l << " " << r << '\n';

	bool is_bipartite = true;
	int op = 0;
	int mid = l + r >> 1;
	for(int i = l; i < mid; i++){
		op += D.Join(edges[i].fi * 2, edges[i].se * 2 + 1);
		op += D.Join(edges[i].fi * 2 + 1, edges[i].se * 2);
		if(D.con(edges[i].fi) || D.con(edges[i].se)){
			dp[mid] = M; is_bipartite = false;
			D.rev(op); op = 0;
			break;
		} 
	}

	if(is_bipartite){

		dp[mid] = min(mid, optl);
		for(int i = optr; i >= optl && i >= mid; i--){
			op += D.Join(edges[i].fi * 2, edges[i].se * 2 + 1);
			op += D.Join(edges[i].fi * 2 + 1, edges[i].se * 2);
			if(D.con(edges[i].fi) || D.con(edges[i].se)){
				dp[mid] = i;
				D.rev(op); op = 0;
				break;
			}
		}

		for(int i = l; i <= mid; i++){
			op += D.Join(edges[i].fi * 2, edges[i].se * 2 + 1);
			op += D.Join(edges[i].fi * 2 + 1, edges[i].se * 2);
			if(D.con(edges[i].fi) || D.con(edges[i].se)){
				is_bipartite = false;
				D.rev(op); op = 0;
				break;
			}
		}

		if(is_bipartite){
			solve(mid + 1, r, dp[mid], optr);
			D.rev(op); op = 0;
		}else{
			for(int i = mid + 1; i <= r; i++) dp[i] = M;
		}

	} else {
		for(int i = mid + 1; i <= r; i++) dp[i] = M;
	}

	for(int i = optr; i > dp[mid]; i--){
		op += D.Join(edges[i].fi * 2, edges[i].se * 2 + 1);
		op += D.Join(edges[i].fi * 2 + 1, edges[i].se * 2);
	}

	solve(l, mid - 1, optl, dp[mid]);
	D.rev(op); op = 0;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N >> M >> Q;
	for(int i = 0; i < M; i++){
		int u, v; cin >> u >> v;
		edges.push_back({u, v});
	}
	edges.push_back({N + 1, N + 2});

	D.init();
	solve(0, M - 1, 0, M);

	for(int i =  0; i < Q; i++){
		int l, r; cin >> l >> r; l--, r--;
		if(r < dp[l]) cout << "YES\n";
		else cout << "NO\n";
	}
}

Compilation message

Joker.cpp: In function 'void solve(int, int, int, int)':
Joker.cpp:77:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   77 |  int mid = l + r >> 1;
      |            ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 3 ms 3412 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 3 ms 3412 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3412 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3540 KB Output is correct
31 Correct 3 ms 3556 KB Output is correct
32 Correct 3 ms 3540 KB Output is correct
33 Correct 3 ms 3540 KB Output is correct
34 Correct 3 ms 3540 KB Output is correct
35 Correct 5 ms 3540 KB Output is correct
36 Correct 3 ms 3548 KB Output is correct
37 Correct 4 ms 3540 KB Output is correct
38 Correct 3 ms 3540 KB Output is correct
39 Correct 3 ms 3540 KB Output is correct
40 Correct 3 ms 3544 KB Output is correct
41 Correct 3 ms 3540 KB Output is correct
42 Correct 3 ms 3540 KB Output is correct
43 Correct 3 ms 3552 KB Output is correct
44 Correct 3 ms 3540 KB Output is correct
45 Correct 3 ms 3556 KB Output is correct
46 Correct 3 ms 3540 KB Output is correct
47 Correct 3 ms 3476 KB Output is correct
48 Correct 3 ms 3540 KB Output is correct
49 Correct 4 ms 3584 KB Output is correct
50 Correct 3 ms 3540 KB Output is correct
51 Correct 3 ms 3540 KB Output is correct
52 Correct 3 ms 3480 KB Output is correct
53 Correct 3 ms 3540 KB Output is correct
54 Correct 3 ms 3552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 207 ms 9684 KB Output is correct
4 Correct 256 ms 13312 KB Output is correct
5 Correct 201 ms 13288 KB Output is correct
6 Correct 159 ms 9640 KB Output is correct
7 Correct 149 ms 9664 KB Output is correct
8 Correct 165 ms 8472 KB Output is correct
9 Correct 185 ms 9296 KB Output is correct
10 Correct 214 ms 9868 KB Output is correct
11 Correct 179 ms 9264 KB Output is correct
12 Correct 203 ms 9860 KB Output is correct
13 Correct 159 ms 7952 KB Output is correct
14 Correct 165 ms 8292 KB Output is correct
15 Correct 233 ms 9660 KB Output is correct
16 Correct 218 ms 9784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 3 ms 3412 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3412 KB Output is correct
29 Correct 207 ms 9684 KB Output is correct
30 Correct 256 ms 13312 KB Output is correct
31 Correct 201 ms 13288 KB Output is correct
32 Correct 159 ms 9640 KB Output is correct
33 Correct 149 ms 9664 KB Output is correct
34 Correct 165 ms 8472 KB Output is correct
35 Correct 185 ms 9296 KB Output is correct
36 Correct 214 ms 9868 KB Output is correct
37 Correct 179 ms 9264 KB Output is correct
38 Correct 203 ms 9860 KB Output is correct
39 Correct 159 ms 7952 KB Output is correct
40 Correct 165 ms 8292 KB Output is correct
41 Correct 233 ms 9660 KB Output is correct
42 Correct 218 ms 9784 KB Output is correct
43 Correct 197 ms 13920 KB Output is correct
44 Correct 244 ms 16908 KB Output is correct
45 Correct 284 ms 17200 KB Output is correct
46 Correct 143 ms 13956 KB Output is correct
47 Correct 158 ms 14088 KB Output is correct
48 Correct 195 ms 13164 KB Output is correct
49 Correct 224 ms 14032 KB Output is correct
50 Correct 182 ms 13272 KB Output is correct
51 Correct 185 ms 14012 KB Output is correct
52 Correct 201 ms 14272 KB Output is correct
53 Correct 175 ms 12316 KB Output is correct
54 Correct 198 ms 13164 KB Output is correct
55 Correct 212 ms 14016 KB Output is correct
56 Correct 226 ms 14380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 3 ms 3412 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3412 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3540 KB Output is correct
31 Correct 3 ms 3556 KB Output is correct
32 Correct 3 ms 3540 KB Output is correct
33 Correct 3 ms 3540 KB Output is correct
34 Correct 3 ms 3540 KB Output is correct
35 Correct 5 ms 3540 KB Output is correct
36 Correct 3 ms 3548 KB Output is correct
37 Correct 4 ms 3540 KB Output is correct
38 Correct 3 ms 3540 KB Output is correct
39 Correct 3 ms 3540 KB Output is correct
40 Correct 3 ms 3544 KB Output is correct
41 Correct 3 ms 3540 KB Output is correct
42 Correct 3 ms 3540 KB Output is correct
43 Correct 3 ms 3552 KB Output is correct
44 Correct 3 ms 3540 KB Output is correct
45 Correct 3 ms 3556 KB Output is correct
46 Correct 3 ms 3540 KB Output is correct
47 Correct 3 ms 3476 KB Output is correct
48 Correct 3 ms 3540 KB Output is correct
49 Correct 4 ms 3584 KB Output is correct
50 Correct 3 ms 3540 KB Output is correct
51 Correct 3 ms 3540 KB Output is correct
52 Correct 3 ms 3480 KB Output is correct
53 Correct 3 ms 3540 KB Output is correct
54 Correct 3 ms 3552 KB Output is correct
55 Correct 168 ms 11568 KB Output is correct
56 Correct 188 ms 15932 KB Output is correct
57 Correct 170 ms 15936 KB Output is correct
58 Correct 124 ms 11604 KB Output is correct
59 Correct 167 ms 11580 KB Output is correct
60 Correct 210 ms 11728 KB Output is correct
61 Correct 167 ms 11620 KB Output is correct
62 Correct 166 ms 11712 KB Output is correct
63 Correct 134 ms 10308 KB Output is correct
64 Correct 184 ms 11668 KB Output is correct
65 Correct 196 ms 11720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 3 ms 3412 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3412 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 3 ms 3540 KB Output is correct
31 Correct 3 ms 3556 KB Output is correct
32 Correct 3 ms 3540 KB Output is correct
33 Correct 3 ms 3540 KB Output is correct
34 Correct 3 ms 3540 KB Output is correct
35 Correct 5 ms 3540 KB Output is correct
36 Correct 3 ms 3548 KB Output is correct
37 Correct 4 ms 3540 KB Output is correct
38 Correct 3 ms 3540 KB Output is correct
39 Correct 3 ms 3540 KB Output is correct
40 Correct 3 ms 3544 KB Output is correct
41 Correct 3 ms 3540 KB Output is correct
42 Correct 3 ms 3540 KB Output is correct
43 Correct 3 ms 3552 KB Output is correct
44 Correct 3 ms 3540 KB Output is correct
45 Correct 3 ms 3556 KB Output is correct
46 Correct 3 ms 3540 KB Output is correct
47 Correct 3 ms 3476 KB Output is correct
48 Correct 3 ms 3540 KB Output is correct
49 Correct 4 ms 3584 KB Output is correct
50 Correct 3 ms 3540 KB Output is correct
51 Correct 3 ms 3540 KB Output is correct
52 Correct 3 ms 3480 KB Output is correct
53 Correct 3 ms 3540 KB Output is correct
54 Correct 3 ms 3552 KB Output is correct
55 Correct 207 ms 9684 KB Output is correct
56 Correct 256 ms 13312 KB Output is correct
57 Correct 201 ms 13288 KB Output is correct
58 Correct 159 ms 9640 KB Output is correct
59 Correct 149 ms 9664 KB Output is correct
60 Correct 165 ms 8472 KB Output is correct
61 Correct 185 ms 9296 KB Output is correct
62 Correct 214 ms 9868 KB Output is correct
63 Correct 179 ms 9264 KB Output is correct
64 Correct 203 ms 9860 KB Output is correct
65 Correct 159 ms 7952 KB Output is correct
66 Correct 165 ms 8292 KB Output is correct
67 Correct 233 ms 9660 KB Output is correct
68 Correct 218 ms 9784 KB Output is correct
69 Correct 197 ms 13920 KB Output is correct
70 Correct 244 ms 16908 KB Output is correct
71 Correct 284 ms 17200 KB Output is correct
72 Correct 143 ms 13956 KB Output is correct
73 Correct 158 ms 14088 KB Output is correct
74 Correct 195 ms 13164 KB Output is correct
75 Correct 224 ms 14032 KB Output is correct
76 Correct 182 ms 13272 KB Output is correct
77 Correct 185 ms 14012 KB Output is correct
78 Correct 201 ms 14272 KB Output is correct
79 Correct 175 ms 12316 KB Output is correct
80 Correct 198 ms 13164 KB Output is correct
81 Correct 212 ms 14016 KB Output is correct
82 Correct 226 ms 14380 KB Output is correct
83 Correct 168 ms 11568 KB Output is correct
84 Correct 188 ms 15932 KB Output is correct
85 Correct 170 ms 15936 KB Output is correct
86 Correct 124 ms 11604 KB Output is correct
87 Correct 167 ms 11580 KB Output is correct
88 Correct 210 ms 11728 KB Output is correct
89 Correct 167 ms 11620 KB Output is correct
90 Correct 166 ms 11712 KB Output is correct
91 Correct 134 ms 10308 KB Output is correct
92 Correct 184 ms 11668 KB Output is correct
93 Correct 196 ms 11720 KB Output is correct
94 Correct 211 ms 14712 KB Output is correct
95 Correct 276 ms 17840 KB Output is correct
96 Correct 200 ms 17276 KB Output is correct
97 Correct 153 ms 14632 KB Output is correct
98 Correct 160 ms 14716 KB Output is correct
99 Correct 182 ms 13268 KB Output is correct
100 Correct 212 ms 14944 KB Output is correct
101 Correct 180 ms 13880 KB Output is correct
102 Correct 201 ms 14692 KB Output is correct
103 Correct 227 ms 15348 KB Output is correct
104 Correct 173 ms 12736 KB Output is correct
105 Correct 213 ms 14524 KB Output is correct
106 Correct 236 ms 14884 KB Output is correct
107 Correct 232 ms 18152 KB Output is correct
108 Correct 198 ms 14388 KB Output is correct
109 Correct 208 ms 14520 KB Output is correct
110 Correct 197 ms 14464 KB Output is correct
111 Correct 201 ms 14416 KB Output is correct
112 Correct 203 ms 14500 KB Output is correct
113 Correct 197 ms 14436 KB Output is correct
114 Correct 194 ms 14492 KB Output is correct
115 Correct 192 ms 14448 KB Output is correct
116 Correct 192 ms 14480 KB Output is correct