Submission #1007179

# Submission time Handle Problem Language Result Execution time Memory
1007179 2024-06-24T12:50:00 Z BuzzyBeez Joker (BOI20_joker) C++17
100 / 100
202 ms 26308 KB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second

pair<int, int> E[200008];
int best[200008];

struct Disjoint_Set_Union {
	static const int N = 2e5;
	int par[N + 8], dis[N + 8], sz[N + 8];
	vector<pair<int*, int>> history; int is_bi = 1;
	Disjoint_Set_Union() {
		iota(par, par + N + 8, 0);
		fill(dis, dis + N + 8, 0);
		fill(sz, sz + N + 8, 1);
	}
	pair<int, int> pu, pv; int du, dv;
	pair<int, int> find(int u) {
		du = 0;
		while (par[u] != u) du ^= dis[u], u = par[u];
		return {u, du};
	}
	void join(int u, int v) {
		pu = find(u); pv = find(v);
		u = pu.first; du = pu.second; v = pv.first; dv = pv.second;
		if (u == v) {
			if (du == dv) history.push_back({&is_bi, is_bi}), is_bi = 0;
			return;
		}
		if (sz[u] < sz[v]) swap(u, v);
		history.push_back({par + v, par[v]});
		history.push_back({dis + v, dis[v]});
		history.push_back({sz + u, sz[u]});
		par[v] = u; dis[v] = du ^ dv ^ 1; sz[u] += sz[v];
	}
	int current_snapshot() {return history.size();}
	void rollback_to(int snapshot) {
		while (history.size() > snapshot) {
			*history.back().first = history.back().second;
			history.pop_back();
		}
	}
} DSU;

void dnc(int l, int r, int optl, int optr) {
	if (l > r) return;
	int mid = (l + r) / 2, cur = DSU.current_snapshot();
	for (int i = l; i < mid; ++i) DSU.join(E[i].fi, E[i].se);
	int post_L = DSU.current_snapshot();
	for (int i = optr; i >= max(mid, optl); --i) {
		if (!DSU.is_bi) {best[mid] = i; break;}
		DSU.join(E[i].fi, E[i].se);
	}
	if (!best[mid]) best[mid] = mid - 1;
	DSU.rollback_to(post_L); DSU.join(E[mid].fi, E[mid].se);
	dnc(mid + 1, r, best[mid], optr);
	DSU.rollback_to(cur);
	for (int i = optr; i > best[mid]; --i) DSU.join(E[i].fi, E[i].se);
	dnc(l, mid - 1, optl, best[mid]);
	DSU.rollback_to(cur);
}

signed main() {
	// freopen("NOHOME.INP", "r", stdin);
	// freopen("NOHOME.OUT", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m, q, l, r; cin >> n >> m >> q;
	for (int i = 1; i <= m; ++i) cin >> E[i].first >> E[i].second;
	dnc(1, m, 1, m);
	// for (int i = 1; i <= m; ++i) cout << best[i] << ' ';
	// cout << '\n';
	while (q--) cin >> l >> r, cout << (best[l] >= r ? "YES\n" : "NO\n");
}

Compilation message

Joker.cpp: In member function 'void Disjoint_Set_Union::rollback_to(int)':
Joker.cpp:40:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int*, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |   while (history.size() > snapshot) {
      |          ~~~~~~~~~~~~~~~^~~~~~~~~~
# 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 4700 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 4700 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 4700 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 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 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 4952 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 4700 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 4700 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 4700 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 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 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 4952 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 2 ms 4884 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 2 ms 4956 KB Output is correct
35 Correct 2 ms 4876 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 1 ms 4956 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 1 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 4876 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4888 KB Output is correct
50 Correct 3 ms 4956 KB Output is correct
51 Correct 1 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 4956 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 120 ms 14028 KB Output is correct
4 Correct 202 ms 25284 KB Output is correct
5 Correct 96 ms 25352 KB Output is correct
6 Correct 106 ms 17260 KB Output is correct
7 Correct 107 ms 16108 KB Output is correct
8 Correct 107 ms 12736 KB Output is correct
9 Correct 127 ms 17616 KB Output is correct
10 Correct 166 ms 17540 KB Output is correct
11 Correct 117 ms 17872 KB Output is correct
12 Correct 139 ms 17592 KB Output is correct
13 Correct 100 ms 11344 KB Output is correct
14 Correct 102 ms 13504 KB Output is correct
15 Correct 127 ms 17352 KB Output is correct
16 Correct 145 ms 18128 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 4700 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 4700 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 4700 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 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 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 4952 KB Output is correct
29 Correct 120 ms 14028 KB Output is correct
30 Correct 202 ms 25284 KB Output is correct
31 Correct 96 ms 25352 KB Output is correct
32 Correct 106 ms 17260 KB Output is correct
33 Correct 107 ms 16108 KB Output is correct
34 Correct 107 ms 12736 KB Output is correct
35 Correct 127 ms 17616 KB Output is correct
36 Correct 166 ms 17540 KB Output is correct
37 Correct 117 ms 17872 KB Output is correct
38 Correct 139 ms 17592 KB Output is correct
39 Correct 100 ms 11344 KB Output is correct
40 Correct 102 ms 13504 KB Output is correct
41 Correct 127 ms 17352 KB Output is correct
42 Correct 145 ms 18128 KB Output is correct
43 Correct 131 ms 18288 KB Output is correct
44 Correct 193 ms 25284 KB Output is correct
45 Correct 153 ms 24716 KB Output is correct
46 Correct 122 ms 17640 KB Output is correct
47 Correct 105 ms 17356 KB Output is correct
48 Correct 135 ms 16584 KB Output is correct
49 Correct 150 ms 18128 KB Output is correct
50 Correct 118 ms 17608 KB Output is correct
51 Correct 124 ms 17848 KB Output is correct
52 Correct 132 ms 18360 KB Output is correct
53 Correct 112 ms 11604 KB Output is correct
54 Correct 151 ms 17396 KB Output is correct
55 Correct 144 ms 17200 KB Output is correct
56 Correct 159 ms 18616 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 4700 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 4700 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 4700 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 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 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 4952 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 2 ms 4884 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 2 ms 4956 KB Output is correct
35 Correct 2 ms 4876 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 1 ms 4956 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 1 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 4876 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4888 KB Output is correct
50 Correct 3 ms 4956 KB Output is correct
51 Correct 1 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 4956 KB Output is correct
55 Correct 94 ms 13520 KB Output is correct
56 Correct 157 ms 25032 KB Output is correct
57 Correct 73 ms 24260 KB Output is correct
58 Correct 85 ms 16336 KB Output is correct
59 Correct 107 ms 16876 KB Output is correct
60 Correct 139 ms 17096 KB Output is correct
61 Correct 106 ms 16080 KB Output is correct
62 Correct 112 ms 16840 KB Output is correct
63 Correct 84 ms 11596 KB Output is correct
64 Correct 115 ms 16584 KB Output is correct
65 Correct 140 ms 17608 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 4700 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 4700 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 4700 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 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 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 4952 KB Output is correct
29 Correct 1 ms 4700 KB Output is correct
30 Correct 2 ms 4700 KB Output is correct
31 Correct 2 ms 4884 KB Output is correct
32 Correct 2 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 2 ms 4956 KB Output is correct
35 Correct 2 ms 4876 KB Output is correct
36 Correct 2 ms 4700 KB Output is correct
37 Correct 2 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 1 ms 4956 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 1 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 4876 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 2 ms 4700 KB Output is correct
49 Correct 2 ms 4888 KB Output is correct
50 Correct 3 ms 4956 KB Output is correct
51 Correct 1 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 4956 KB Output is correct
55 Correct 120 ms 14028 KB Output is correct
56 Correct 202 ms 25284 KB Output is correct
57 Correct 96 ms 25352 KB Output is correct
58 Correct 106 ms 17260 KB Output is correct
59 Correct 107 ms 16108 KB Output is correct
60 Correct 107 ms 12736 KB Output is correct
61 Correct 127 ms 17616 KB Output is correct
62 Correct 166 ms 17540 KB Output is correct
63 Correct 117 ms 17872 KB Output is correct
64 Correct 139 ms 17592 KB Output is correct
65 Correct 100 ms 11344 KB Output is correct
66 Correct 102 ms 13504 KB Output is correct
67 Correct 127 ms 17352 KB Output is correct
68 Correct 145 ms 18128 KB Output is correct
69 Correct 131 ms 18288 KB Output is correct
70 Correct 193 ms 25284 KB Output is correct
71 Correct 153 ms 24716 KB Output is correct
72 Correct 122 ms 17640 KB Output is correct
73 Correct 105 ms 17356 KB Output is correct
74 Correct 135 ms 16584 KB Output is correct
75 Correct 150 ms 18128 KB Output is correct
76 Correct 118 ms 17608 KB Output is correct
77 Correct 124 ms 17848 KB Output is correct
78 Correct 132 ms 18360 KB Output is correct
79 Correct 112 ms 11604 KB Output is correct
80 Correct 151 ms 17396 KB Output is correct
81 Correct 144 ms 17200 KB Output is correct
82 Correct 159 ms 18616 KB Output is correct
83 Correct 94 ms 13520 KB Output is correct
84 Correct 157 ms 25032 KB Output is correct
85 Correct 73 ms 24260 KB Output is correct
86 Correct 85 ms 16336 KB Output is correct
87 Correct 107 ms 16876 KB Output is correct
88 Correct 139 ms 17096 KB Output is correct
89 Correct 106 ms 16080 KB Output is correct
90 Correct 112 ms 16840 KB Output is correct
91 Correct 84 ms 11596 KB Output is correct
92 Correct 115 ms 16584 KB Output is correct
93 Correct 140 ms 17608 KB Output is correct
94 Correct 132 ms 17496 KB Output is correct
95 Correct 167 ms 26308 KB Output is correct
96 Correct 107 ms 24680 KB Output is correct
97 Correct 110 ms 17616 KB Output is correct
98 Correct 114 ms 17608 KB Output is correct
99 Correct 115 ms 14468 KB Output is correct
100 Correct 160 ms 19128 KB Output is correct
101 Correct 122 ms 16588 KB Output is correct
102 Correct 135 ms 17592 KB Output is correct
103 Correct 155 ms 19132 KB Output is correct
104 Correct 114 ms 13656 KB Output is correct
105 Correct 146 ms 18616 KB Output is correct
106 Correct 160 ms 18748 KB Output is correct
107 Correct 119 ms 25540 KB Output is correct
108 Correct 126 ms 17476 KB Output is correct
109 Correct 130 ms 18616 KB Output is correct
110 Correct 124 ms 17212 KB Output is correct
111 Correct 123 ms 17712 KB Output is correct
112 Correct 123 ms 17332 KB Output is correct
113 Correct 123 ms 18876 KB Output is correct
114 Correct 123 ms 18212 KB Output is correct
115 Correct 125 ms 17080 KB Output is correct
116 Correct 124 ms 17592 KB Output is correct