Submission #877798

# Submission time Handle Problem Language Result Execution time Memory
877798 2023-11-23T14:49:08 Z saddd Joker (BOI20_joker) C++17
100 / 100
800 ms 26628 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>

namespace std {

template<class Fun>
class y_combinator_result {
	Fun fun_;
public:
	template<class T>
	explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

	template<class ...Args>
	decltype(auto) operator()(Args &&...args) {
		return fun_(std::ref(*this), std::forward<Args>(args)...);
	}
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
	return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

} // namespace std

struct rollback_union_find {
	std::vector<int> par;
	std::stack<std::pair<int, int>> stk;

	rollback_union_find(int n) {
		par.resize(n);
		iota(par.begin(), par.end(), 0);
	}

	int leader(int u) {
		if (u == par[u])
			return u;
		stk.emplace(u, par[u]);
		return par[u] = leader(par[u]);
	}

	void merge(int u, int v) {
		u = leader(u), v = leader(v);
		if (u == v)
			return;
		stk.emplace(v, v);
		par[v] = u;
	}

	bool same(int u, int v) { return leader(u) == leader(v); }

	int version() { return (int)stk.size(); }

	void rollback(int ver) {
		while ((int)stk.size() > ver) {
			par[stk.top().first] = stk.top().second;
			stk.pop();
		}
	}
};

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N, M, Q;
	cin >> N >> M >> Q;
	vector<pair<int, int>> E(M);
	for (auto &[u, v] : E) {
		cin >> u >> v;
		u--, v--;
	}

	int max_l = 0;
	vector<pair<int, int>> Z(Q);
	for (auto &[l, r] : Z) {
		cin >> l >> r;
		l--, r--;
		max_l = max(max_l, l);
	}

	vector<int> ans(M, -1);
	rollback_union_find dsu(2 * N);

	y_combinator([&](auto self, int ql, int qr, int al, int ar) -> void {
		if (qr < ql)
			return;
		if (al == ar) {
			for (int i = ql; i <= qr; i++)
				ans[i] = al;
			return;
		}

		int qm = (ql + qr) / 2, ver = dsu.version();
		for (int i = ql; i < qm; i++) {
			dsu.merge(E[i].first, E[i].second + N);
			dsu.merge(E[i].first + N, E[i].second);
			if (dsu.same(E[i].first, E[i].first + N) || 
				dsu.same(E[i].second, E[i].second + N)) {
				ans[qm] = M;
				break;
			}
		}
		if (ans[qm] == -1) {
			for (int i = ar; i >= al; i--) {
				if (i == M)
					continue;
				dsu.merge(E[i].first, E[i].second + N);
				dsu.merge(E[i].first + N, E[i].second);
				if (dsu.same(E[i].first, E[i].first + N) || 
					dsu.same(E[i].second, E[i].second + N)) {
					ans[qm] = i;
					break;
				}
			}
			if (ans[qm] == -1)
				ans[qm] = qm;
		}
		dsu.rollback(ver);

		bool b = false;
		for (int i = ar; i > ans[qm]; i--) {
			if (i == M)
				continue;
			dsu.merge(E[i].first, E[i].second + N);
			dsu.merge(E[i].first + N, E[i].second);
			if (dsu.same(E[i].first, E[i].first + N) || 
				dsu.same(E[i].second, E[i].second + N)) {
				b = true;
				break;
			}
		}
		if (b) {
			for (int i = ql; i <= qm; i++)
				ans[i] = ans[qm];
		} else {
			self(ql, qm - 1, al, ans[qm]);
		}
		dsu.rollback(ver);

		b = false;
		for (int i = ql; i <= qm; i++) {
			dsu.merge(E[i].first, E[i].second + N);
			dsu.merge(E[i].first + N, E[i].second);	
			if (dsu.same(E[i].first, E[i].first + N) || 
				dsu.same(E[i].second, E[i].second + N)) {
				b = true;
				break;
			}
		}
		if (b) {
			for (int i = qm + 1; i <= qr; i++)
				ans[i] = M;
		} else {
			self(qm + 1, qr, ans[qm], ar);
		}
		dsu.rollback(ver);
	})(0, max_l, 0, M);
    
	for (auto [l, r] : Z) 
		cout << (r < ans[l] ? "YES\n" : "NO\n");
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 3 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 616 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 672 KB Output is correct
38 Correct 5 ms 764 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 392 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 488 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 464 KB Output is correct
51 Correct 2 ms 600 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 64 ms 12788 KB Output is correct
4 Correct 78 ms 17816 KB Output is correct
5 Correct 53 ms 6828 KB Output is correct
6 Correct 66 ms 14120 KB Output is correct
7 Correct 63 ms 13392 KB Output is correct
8 Correct 80 ms 19684 KB Output is correct
9 Correct 79 ms 18144 KB Output is correct
10 Correct 66 ms 13908 KB Output is correct
11 Correct 68 ms 14912 KB Output is correct
12 Correct 69 ms 15168 KB Output is correct
13 Correct 72 ms 18012 KB Output is correct
14 Correct 83 ms 20156 KB Output is correct
15 Correct 79 ms 16008 KB Output is correct
16 Correct 67 ms 13976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 64 ms 12788 KB Output is correct
30 Correct 78 ms 17816 KB Output is correct
31 Correct 53 ms 6828 KB Output is correct
32 Correct 66 ms 14120 KB Output is correct
33 Correct 63 ms 13392 KB Output is correct
34 Correct 80 ms 19684 KB Output is correct
35 Correct 79 ms 18144 KB Output is correct
36 Correct 66 ms 13908 KB Output is correct
37 Correct 68 ms 14912 KB Output is correct
38 Correct 69 ms 15168 KB Output is correct
39 Correct 72 ms 18012 KB Output is correct
40 Correct 83 ms 20156 KB Output is correct
41 Correct 79 ms 16008 KB Output is correct
42 Correct 67 ms 13976 KB Output is correct
43 Correct 149 ms 14160 KB Output is correct
44 Correct 276 ms 20116 KB Output is correct
45 Correct 246 ms 18404 KB Output is correct
46 Correct 81 ms 13140 KB Output is correct
47 Correct 83 ms 12628 KB Output is correct
48 Correct 154 ms 17560 KB Output is correct
49 Correct 199 ms 14672 KB Output is correct
50 Correct 195 ms 14932 KB Output is correct
51 Correct 182 ms 14908 KB Output is correct
52 Correct 175 ms 15432 KB Output is correct
53 Correct 184 ms 17372 KB Output is correct
54 Correct 194 ms 18748 KB Output is correct
55 Correct 178 ms 15656 KB Output is correct
56 Correct 188 ms 14736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 3 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 616 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 672 KB Output is correct
38 Correct 5 ms 764 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 392 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 488 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 464 KB Output is correct
51 Correct 2 ms 600 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 3 ms 504 KB Output is correct
55 Correct 275 ms 12956 KB Output is correct
56 Correct 800 ms 26628 KB Output is correct
57 Correct 164 ms 21120 KB Output is correct
58 Correct 122 ms 12808 KB Output is correct
59 Correct 265 ms 15384 KB Output is correct
60 Correct 335 ms 12128 KB Output is correct
61 Correct 255 ms 11860 KB Output is correct
62 Correct 269 ms 12736 KB Output is correct
63 Correct 190 ms 16968 KB Output is correct
64 Correct 274 ms 14148 KB Output is correct
65 Correct 323 ms 11672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 3 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 616 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 672 KB Output is correct
38 Correct 5 ms 764 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 392 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 488 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 464 KB Output is correct
51 Correct 2 ms 600 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 3 ms 504 KB Output is correct
55 Correct 64 ms 12788 KB Output is correct
56 Correct 78 ms 17816 KB Output is correct
57 Correct 53 ms 6828 KB Output is correct
58 Correct 66 ms 14120 KB Output is correct
59 Correct 63 ms 13392 KB Output is correct
60 Correct 80 ms 19684 KB Output is correct
61 Correct 79 ms 18144 KB Output is correct
62 Correct 66 ms 13908 KB Output is correct
63 Correct 68 ms 14912 KB Output is correct
64 Correct 69 ms 15168 KB Output is correct
65 Correct 72 ms 18012 KB Output is correct
66 Correct 83 ms 20156 KB Output is correct
67 Correct 79 ms 16008 KB Output is correct
68 Correct 67 ms 13976 KB Output is correct
69 Correct 149 ms 14160 KB Output is correct
70 Correct 276 ms 20116 KB Output is correct
71 Correct 246 ms 18404 KB Output is correct
72 Correct 81 ms 13140 KB Output is correct
73 Correct 83 ms 12628 KB Output is correct
74 Correct 154 ms 17560 KB Output is correct
75 Correct 199 ms 14672 KB Output is correct
76 Correct 195 ms 14932 KB Output is correct
77 Correct 182 ms 14908 KB Output is correct
78 Correct 175 ms 15432 KB Output is correct
79 Correct 184 ms 17372 KB Output is correct
80 Correct 194 ms 18748 KB Output is correct
81 Correct 178 ms 15656 KB Output is correct
82 Correct 188 ms 14736 KB Output is correct
83 Correct 275 ms 12956 KB Output is correct
84 Correct 800 ms 26628 KB Output is correct
85 Correct 164 ms 21120 KB Output is correct
86 Correct 122 ms 12808 KB Output is correct
87 Correct 265 ms 15384 KB Output is correct
88 Correct 335 ms 12128 KB Output is correct
89 Correct 255 ms 11860 KB Output is correct
90 Correct 269 ms 12736 KB Output is correct
91 Correct 190 ms 16968 KB Output is correct
92 Correct 274 ms 14148 KB Output is correct
93 Correct 323 ms 11672 KB Output is correct
94 Correct 310 ms 16424 KB Output is correct
95 Correct 484 ms 24628 KB Output is correct
96 Correct 168 ms 25400 KB Output is correct
97 Correct 143 ms 16452 KB Output is correct
98 Correct 173 ms 16712 KB Output is correct
99 Correct 245 ms 20560 KB Output is correct
100 Correct 232 ms 14776 KB Output is correct
101 Correct 259 ms 15428 KB Output is correct
102 Correct 297 ms 15280 KB Output is correct
103 Correct 331 ms 16000 KB Output is correct
104 Correct 248 ms 20804 KB Output is correct
105 Correct 307 ms 17696 KB Output is correct
106 Correct 354 ms 15024 KB Output is correct
107 Correct 106 ms 15840 KB Output is correct
108 Correct 260 ms 16400 KB Output is correct
109 Correct 251 ms 16164 KB Output is correct
110 Correct 251 ms 16132 KB Output is correct
111 Correct 250 ms 16148 KB Output is correct
112 Correct 256 ms 16036 KB Output is correct
113 Correct 255 ms 15992 KB Output is correct
114 Correct 250 ms 16136 KB Output is correct
115 Correct 263 ms 16152 KB Output is correct
116 Correct 251 ms 16144 KB Output is correct