Submission #911458

# Submission time Handle Problem Language Result Execution time Memory
911458 2024-01-19T01:12:32 Z NK_ Joker (BOI20_joker) C++17
100 / 100
230 ms 22716 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back

template<class T> using V = vector<T>;
using vi = V<int>;

const int INF = 1e9;
struct DSU {
	vi e, p; V<array<int, 7>> ed;void init(int N) { e = vi(N, -1); p = vi(N, 0); }
	int get(int x) { return e[x] < 0 ? x : get(e[x]); }
	int parity(int x) { return e[x] < 0 ? p[x] : p[x] ^ parity(e[x]); }
	bool unite(int x, int y, int t) {
		int rx = get(x), ry = get(y);
		int px = parity(x), py = parity(y);
		// cout << x << " " << y << " " << t << endl;
		// cout << rx << " " << rx << " --> " << px << " " << py << endl;

		if (rx == ry) {
			if (px == py) return 0;
			ed.pb({-1, -1, -1, -1, -1, -1, t}); return 1;
		}

		if (e[rx] > e[ry]) swap(rx, ry);

		ed.pb({rx, ry, e[rx], e[ry], p[rx], p[ry], t});
		e[rx] += e[ry]; e[ry] = rx; p[ry] = px ^ py ^ 1; return 1;
	}

	bool rollback(int T) {
		if (size(ed) == 0) return 0;
		auto [rx, ry, erx, ery, prx, pry, t] = ed.back(); 

		if (t < T) return 0;
		
		ed.pop_back();
		if (rx == -1) return 1;


		e[rx] = erx, p[rx] = prx;
		e[ry] = ery, p[ry] = pry;
		return 1;
	}
};


int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M, Q; cin >> N >> M >> Q;

	V<array<int, 2>> E(M); for(auto& x : E) { cin >> x[0] >> x[1]; --x[0], --x[1]; }


	vi lst(M); // lst r where when [l, r] is removed the graph is bipartite

	DSU D; D.init(N);

	int T = 0;
	int cur = 0; for(; cur < M; cur++) {
		if (!D.unite(E[cur][0], E[cur][1], T)) break;
	}

	while(D.rollback(T));

	for(int i = cur+1; i < M; i++) lst[i] = M;

	function<void(int, int, int, int)> dnc = [&](int l, int r, int lx, int rx) {
		int mid = (l + r) / 2;
		
		// compute lst_mid

		++T;
		for(int i = l; i < mid; i++) D.unite(E[i][0], E[i][1], T);

		// cout << "START " << endl;
		// for(auto e : D.ed) {
		// 	cout << e[0] << " <-> " << e[1] << endl;
		// }

		for(int i = rx; i >= mid; i--) {
			if (!D.unite(E[i][0], E[i][1], T)) {
				lst[mid] = i;
				break;
			}
		}


		// cout << "MID" << endl;
		// for(auto e : D.ed) {
		// 	cout << e[0] << " <-> " << e[1] << endl;
		// }

		while(D.rollback(T));
		--T;

		// cout << l << " " << mid << " " << r << " " << lst[mid] << endl;
		// cout << lx << " " << rx << endl;

		// for(auto e : D.ed) {
			// cout << e[0] << " <-> " << e[1] << endl;
		// }

		// cout << "DONE" << endl;

		// [l, mid - 1]

		if (l < mid) {
			++T;
			for(int i = rx; i > lst[mid]; i--) D.unite(E[i][0], E[i][1], T);
			dnc(l, mid - 1, lx, lst[mid]);

			while(D.rollback(T));
			--T;
		}

		// [mid + 1, r]
		if (mid < r) {
			++T;

			for(int i = l; i <= mid; i++) D.unite(E[i][0], E[i][1], T);
			dnc(mid + 1, r, max(mid + 1, lst[mid]), rx);

			while(D.rollback(T));
			--T;
		}
	};	

	dnc(0, cur, 0, M - 1);

	// for(int i = 0; i < M; i++) {
		// lst[i] = max(lst[i], lst[i-1]);
		// cout << i << " => " << lst[i] << nl;
	// }

	for(int i = 0; i < Q; i++) {
		int l, r; cin >> l >> r; --l, --r;
		cout << (lst[l] <= r ? "NO" : "YES") << nl;
	}

	exit(0-0);
}
	
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 488 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 352 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 488 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 352 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 608 KB Output is correct
31 Correct 1 ms 476 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 544 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 3 ms 600 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 584 KB Output is correct
41 Correct 1 ms 576 KB Output is correct
42 Correct 2 ms 580 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 2 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 472 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 2 ms 344 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 127 ms 9064 KB Output is correct
4 Correct 230 ms 19028 KB Output is correct
5 Correct 136 ms 12228 KB Output is correct
6 Correct 106 ms 11632 KB Output is correct
7 Correct 105 ms 10868 KB Output is correct
8 Correct 148 ms 10888 KB Output is correct
9 Correct 154 ms 11200 KB Output is correct
10 Correct 198 ms 11796 KB Output is correct
11 Correct 140 ms 11560 KB Output is correct
12 Correct 167 ms 12076 KB Output is correct
13 Correct 148 ms 11028 KB Output is correct
14 Correct 139 ms 10632 KB Output is correct
15 Correct 169 ms 11248 KB Output is correct
16 Correct 191 ms 12616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 488 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 352 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 127 ms 9064 KB Output is correct
30 Correct 230 ms 19028 KB Output is correct
31 Correct 136 ms 12228 KB Output is correct
32 Correct 106 ms 11632 KB Output is correct
33 Correct 105 ms 10868 KB Output is correct
34 Correct 148 ms 10888 KB Output is correct
35 Correct 154 ms 11200 KB Output is correct
36 Correct 198 ms 11796 KB Output is correct
37 Correct 140 ms 11560 KB Output is correct
38 Correct 167 ms 12076 KB Output is correct
39 Correct 148 ms 11028 KB Output is correct
40 Correct 139 ms 10632 KB Output is correct
41 Correct 169 ms 11248 KB Output is correct
42 Correct 191 ms 12616 KB Output is correct
43 Correct 143 ms 11316 KB Output is correct
44 Correct 224 ms 22716 KB Output is correct
45 Correct 196 ms 16484 KB Output is correct
46 Correct 109 ms 11208 KB Output is correct
47 Correct 107 ms 11200 KB Output is correct
48 Correct 160 ms 11200 KB Output is correct
49 Correct 189 ms 12688 KB Output is correct
50 Correct 145 ms 11036 KB Output is correct
51 Correct 158 ms 11940 KB Output is correct
52 Correct 150 ms 12472 KB Output is correct
53 Correct 153 ms 11328 KB Output is correct
54 Correct 160 ms 11168 KB Output is correct
55 Correct 168 ms 11900 KB Output is correct
56 Correct 184 ms 12736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 488 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 352 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 608 KB Output is correct
31 Correct 1 ms 476 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 544 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 3 ms 600 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 584 KB Output is correct
41 Correct 1 ms 576 KB Output is correct
42 Correct 2 ms 580 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 2 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 472 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 2 ms 344 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 104 ms 10308 KB Output is correct
56 Correct 192 ms 21532 KB Output is correct
57 Correct 106 ms 15040 KB Output is correct
58 Correct 78 ms 11208 KB Output is correct
59 Correct 130 ms 10960 KB Output is correct
60 Correct 157 ms 11992 KB Output is correct
61 Correct 122 ms 9936 KB Output is correct
62 Correct 151 ms 11724 KB Output is correct
63 Correct 108 ms 8896 KB Output is correct
64 Correct 136 ms 10436 KB Output is correct
65 Correct 157 ms 10948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 488 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 352 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 352 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 608 KB Output is correct
31 Correct 1 ms 476 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 544 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 3 ms 600 KB Output is correct
38 Correct 3 ms 600 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 584 KB Output is correct
41 Correct 1 ms 576 KB Output is correct
42 Correct 2 ms 580 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 2 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 472 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 2 ms 344 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 127 ms 9064 KB Output is correct
56 Correct 230 ms 19028 KB Output is correct
57 Correct 136 ms 12228 KB Output is correct
58 Correct 106 ms 11632 KB Output is correct
59 Correct 105 ms 10868 KB Output is correct
60 Correct 148 ms 10888 KB Output is correct
61 Correct 154 ms 11200 KB Output is correct
62 Correct 198 ms 11796 KB Output is correct
63 Correct 140 ms 11560 KB Output is correct
64 Correct 167 ms 12076 KB Output is correct
65 Correct 148 ms 11028 KB Output is correct
66 Correct 139 ms 10632 KB Output is correct
67 Correct 169 ms 11248 KB Output is correct
68 Correct 191 ms 12616 KB Output is correct
69 Correct 143 ms 11316 KB Output is correct
70 Correct 224 ms 22716 KB Output is correct
71 Correct 196 ms 16484 KB Output is correct
72 Correct 109 ms 11208 KB Output is correct
73 Correct 107 ms 11200 KB Output is correct
74 Correct 160 ms 11200 KB Output is correct
75 Correct 189 ms 12688 KB Output is correct
76 Correct 145 ms 11036 KB Output is correct
77 Correct 158 ms 11940 KB Output is correct
78 Correct 150 ms 12472 KB Output is correct
79 Correct 153 ms 11328 KB Output is correct
80 Correct 160 ms 11168 KB Output is correct
81 Correct 168 ms 11900 KB Output is correct
82 Correct 184 ms 12736 KB Output is correct
83 Correct 104 ms 10308 KB Output is correct
84 Correct 192 ms 21532 KB Output is correct
85 Correct 106 ms 15040 KB Output is correct
86 Correct 78 ms 11208 KB Output is correct
87 Correct 130 ms 10960 KB Output is correct
88 Correct 157 ms 11992 KB Output is correct
89 Correct 122 ms 9936 KB Output is correct
90 Correct 151 ms 11724 KB Output is correct
91 Correct 108 ms 8896 KB Output is correct
92 Correct 136 ms 10436 KB Output is correct
93 Correct 157 ms 10948 KB Output is correct
94 Correct 147 ms 12764 KB Output is correct
95 Correct 198 ms 15808 KB Output is correct
96 Correct 150 ms 16196 KB Output is correct
97 Correct 113 ms 12168 KB Output is correct
98 Correct 115 ms 12224 KB Output is correct
99 Correct 149 ms 11588 KB Output is correct
100 Correct 155 ms 13000 KB Output is correct
101 Correct 143 ms 11796 KB Output is correct
102 Correct 158 ms 12536 KB Output is correct
103 Correct 182 ms 13632 KB Output is correct
104 Correct 144 ms 11456 KB Output is correct
105 Correct 198 ms 12240 KB Output is correct
106 Correct 188 ms 13504 KB Output is correct
107 Correct 118 ms 16324 KB Output is correct
108 Correct 131 ms 11832 KB Output is correct
109 Correct 136 ms 12000 KB Output is correct
110 Correct 132 ms 12512 KB Output is correct
111 Correct 134 ms 12324 KB Output is correct
112 Correct 132 ms 11708 KB Output is correct
113 Correct 138 ms 12500 KB Output is correct
114 Correct 135 ms 12224 KB Output is correct
115 Correct 132 ms 11708 KB Output is correct
116 Correct 133 ms 12048 KB Output is correct