답안 #1034564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1034564 2024-07-25T14:55:22 Z dutinmeow Joker (BOI20_joker) C++17
100 / 100
884 ms 27704 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");
}
# 결과 실행 시간 메모리 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 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 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 600 KB Output is correct
32 Correct 2 ms 344 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 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 5 ms 604 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 2 ms 524 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 616 KB Output is correct
43 Correct 3 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 468 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 2 ms 520 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 80 ms 16464 KB Output is correct
4 Correct 89 ms 21216 KB Output is correct
5 Correct 59 ms 11192 KB Output is correct
6 Correct 62 ms 15632 KB Output is correct
7 Correct 59 ms 14932 KB Output is correct
8 Correct 80 ms 21472 KB Output is correct
9 Correct 75 ms 19616 KB Output is correct
10 Correct 65 ms 15448 KB Output is correct
11 Correct 65 ms 16464 KB Output is correct
12 Correct 70 ms 16992 KB Output is correct
13 Correct 72 ms 19564 KB Output is correct
14 Correct 78 ms 21880 KB Output is correct
15 Correct 70 ms 17636 KB Output is correct
16 Correct 63 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 80 ms 16464 KB Output is correct
30 Correct 89 ms 21216 KB Output is correct
31 Correct 59 ms 11192 KB Output is correct
32 Correct 62 ms 15632 KB Output is correct
33 Correct 59 ms 14932 KB Output is correct
34 Correct 80 ms 21472 KB Output is correct
35 Correct 75 ms 19616 KB Output is correct
36 Correct 65 ms 15448 KB Output is correct
37 Correct 65 ms 16464 KB Output is correct
38 Correct 70 ms 16992 KB Output is correct
39 Correct 72 ms 19564 KB Output is correct
40 Correct 78 ms 21880 KB Output is correct
41 Correct 70 ms 17636 KB Output is correct
42 Correct 63 ms 15956 KB Output is correct
43 Correct 162 ms 16212 KB Output is correct
44 Correct 293 ms 21984 KB Output is correct
45 Correct 257 ms 20216 KB Output is correct
46 Correct 81 ms 15192 KB Output is correct
47 Correct 81 ms 14536 KB Output is correct
48 Correct 158 ms 19172 KB Output is correct
49 Correct 199 ms 16468 KB Output is correct
50 Correct 239 ms 16728 KB Output is correct
51 Correct 201 ms 16748 KB Output is correct
52 Correct 208 ms 17324 KB Output is correct
53 Correct 182 ms 19424 KB Output is correct
54 Correct 177 ms 20712 KB Output is correct
55 Correct 185 ms 17508 KB Output is correct
56 Correct 201 ms 16468 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 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 600 KB Output is correct
32 Correct 2 ms 344 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 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 5 ms 604 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 2 ms 524 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 616 KB Output is correct
43 Correct 3 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 468 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 2 ms 520 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 291 ms 13728 KB Output is correct
56 Correct 884 ms 27528 KB Output is correct
57 Correct 160 ms 22004 KB Output is correct
58 Correct 131 ms 13608 KB Output is correct
59 Correct 289 ms 16280 KB Output is correct
60 Correct 355 ms 13100 KB Output is correct
61 Correct 272 ms 12872 KB Output is correct
62 Correct 292 ms 13768 KB Output is correct
63 Correct 211 ms 17996 KB Output is correct
64 Correct 309 ms 15076 KB Output is correct
65 Correct 334 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 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 600 KB Output is correct
32 Correct 2 ms 344 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 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 5 ms 604 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 2 ms 524 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 616 KB Output is correct
43 Correct 3 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 468 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 2 ms 520 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 80 ms 16464 KB Output is correct
56 Correct 89 ms 21216 KB Output is correct
57 Correct 59 ms 11192 KB Output is correct
58 Correct 62 ms 15632 KB Output is correct
59 Correct 59 ms 14932 KB Output is correct
60 Correct 80 ms 21472 KB Output is correct
61 Correct 75 ms 19616 KB Output is correct
62 Correct 65 ms 15448 KB Output is correct
63 Correct 65 ms 16464 KB Output is correct
64 Correct 70 ms 16992 KB Output is correct
65 Correct 72 ms 19564 KB Output is correct
66 Correct 78 ms 21880 KB Output is correct
67 Correct 70 ms 17636 KB Output is correct
68 Correct 63 ms 15956 KB Output is correct
69 Correct 162 ms 16212 KB Output is correct
70 Correct 293 ms 21984 KB Output is correct
71 Correct 257 ms 20216 KB Output is correct
72 Correct 81 ms 15192 KB Output is correct
73 Correct 81 ms 14536 KB Output is correct
74 Correct 158 ms 19172 KB Output is correct
75 Correct 199 ms 16468 KB Output is correct
76 Correct 239 ms 16728 KB Output is correct
77 Correct 201 ms 16748 KB Output is correct
78 Correct 208 ms 17324 KB Output is correct
79 Correct 182 ms 19424 KB Output is correct
80 Correct 177 ms 20712 KB Output is correct
81 Correct 185 ms 17508 KB Output is correct
82 Correct 201 ms 16468 KB Output is correct
83 Correct 291 ms 13728 KB Output is correct
84 Correct 884 ms 27528 KB Output is correct
85 Correct 160 ms 22004 KB Output is correct
86 Correct 131 ms 13608 KB Output is correct
87 Correct 289 ms 16280 KB Output is correct
88 Correct 355 ms 13100 KB Output is correct
89 Correct 272 ms 12872 KB Output is correct
90 Correct 292 ms 13768 KB Output is correct
91 Correct 211 ms 17996 KB Output is correct
92 Correct 309 ms 15076 KB Output is correct
93 Correct 334 ms 12380 KB Output is correct
94 Correct 348 ms 18560 KB Output is correct
95 Correct 531 ms 26716 KB Output is correct
96 Correct 168 ms 27704 KB Output is correct
97 Correct 145 ms 17992 KB Output is correct
98 Correct 177 ms 18764 KB Output is correct
99 Correct 261 ms 22848 KB Output is correct
100 Correct 252 ms 17024 KB Output is correct
101 Correct 276 ms 17588 KB Output is correct
102 Correct 317 ms 17420 KB Output is correct
103 Correct 328 ms 18324 KB Output is correct
104 Correct 245 ms 22888 KB Output is correct
105 Correct 331 ms 19772 KB Output is correct
106 Correct 380 ms 17172 KB Output is correct
107 Correct 109 ms 18320 KB Output is correct
108 Correct 257 ms 18212 KB Output is correct
109 Correct 281 ms 18304 KB Output is correct
110 Correct 271 ms 18420 KB Output is correct
111 Correct 292 ms 18204 KB Output is correct
112 Correct 267 ms 18300 KB Output is correct
113 Correct 278 ms 18376 KB Output is correct
114 Correct 265 ms 18408 KB Output is correct
115 Correct 307 ms 18348 KB Output is correct
116 Correct 269 ms 18176 KB Output is correct