답안 #542748

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542748 2022-03-27T21:05:41 Z skittles1412 Joker (BOI20_joker) C++17
100 / 100
244 ms 16560 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

const int maxn = 2e5 + 5;

struct DSU {
	struct Change {
		bool prevb;
		int u, v, prevpu;
	};

	bool b;
	vector<int> p;
	vector<bool> parity;
	vector<Change> changes;

	DSU(int n) : b(true), p(n, -1), parity(n), changes {} {}

	pair<int, bool> find(int u) const {
		if (p[u] < 0) {
			return {u, false};
		} else {
			auto ans = find(p[u]);
			ans.second ^= parity[u];
			return ans;
		}
	}

	void merge(int qu, int qv) {
		auto [u, pu] = find(qu);
		auto [v, pv] = find(qv);
		if (u == v) {
			changes.push_back({b, -1, -1, -1});
			if (pu == pv) {
				b = false;
			}
			return;
		}
		if (p[u] < p[v]) {
			swap(u, v);
			swap(pu, pv);
		}
		changes.push_back({b, u, v, p[u]});
		p[v] += p[u];
		p[u] = v;
		parity[u] = !(pu ^ pv);
	}

	void rollback() {
		auto [prevb, u, v, prevpu] = changes.back();
		changes.pop_back();
		b = prevb;
		if (u == -1) {
			return;
		}
		parity[u] = false;
		p[u] = prevpu;
		p[v] -= p[u];
	}
};

DSU dsu(maxn);
int ans[maxn], edges[maxn][2];

void push(int i) {
	auto &[u, v] = edges[i];
	dsu.merge(u, v);
}

void pop() {
	dsu.rollback();
}

void dfs(int l, int r, int ql, int qr) {
	if (l > r) {
		return;
	}
	assert(ql <= qr);
	int mid = (l + r) / 2;
	for (int i = l; i < mid; i++) {
		push(i);
	}
	int cr;
	for (cr = qr; cr >= ql; cr--) {
		if (!dsu.b) {
			break;
		}
		push(cr);
	}
	assert(!dsu.b);
	ans[mid] = cr;
	for (int i = cr + 1; i <= qr; i++) {
		pop();
	}
	push(mid);
	dfs(mid + 1, r, cr, qr);
	for (int i = l; i <= mid; i++) {
		pop();
	}
	for (int i = cr + 1; i <= qr; i++) {
		push(i);
	}
	dfs(l, mid - 1, ql, cr);
	for (int i = cr + 1; i <= qr; i++) {
		pop();
	}
}

void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 0; i < m; i++) {
		cin >> edges[i][0] >> edges[i][1];
		edges[i][0]--;
		edges[i][1]--;
	}
	DSU d(n);
	for (int i = 0; i < m; i++) {
		auto &[u, v] = edges[i];
		d.merge(u, v);
	}
	if (d.b) {
		while (q--) {
			cout << "NO" << endl;
		}
		return;
	}
	dfs(0, m - 1, 0, m - 1);
	while (q--) {
		int l, r;
		cin >> l >> r;
		l--;
		r--;
		if (r <= ans[l]) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
		}
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1156 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1152 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1160 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1160 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1156 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1156 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1152 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1160 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1160 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1156 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1184 KB Output is correct
30 Correct 2 ms 1192 KB Output is correct
31 Correct 2 ms 1292 KB Output is correct
32 Correct 2 ms 1292 KB Output is correct
33 Correct 2 ms 1236 KB Output is correct
34 Correct 2 ms 1320 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 2 ms 1296 KB Output is correct
37 Correct 2 ms 1292 KB Output is correct
38 Correct 1 ms 1168 KB Output is correct
39 Correct 2 ms 1236 KB Output is correct
40 Correct 2 ms 1236 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1236 KB Output is correct
43 Correct 2 ms 1236 KB Output is correct
44 Correct 2 ms 1236 KB Output is correct
45 Correct 2 ms 1236 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 2 ms 1236 KB Output is correct
49 Correct 2 ms 1236 KB Output is correct
50 Correct 3 ms 1236 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1316 KB Output is correct
53 Correct 2 ms 1236 KB Output is correct
54 Correct 3 ms 1288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 152 ms 14896 KB Output is correct
4 Correct 49 ms 10428 KB Output is correct
5 Correct 137 ms 15408 KB Output is correct
6 Correct 161 ms 14932 KB Output is correct
7 Correct 161 ms 14932 KB Output is correct
8 Correct 165 ms 14648 KB Output is correct
9 Correct 182 ms 14872 KB Output is correct
10 Correct 223 ms 15416 KB Output is correct
11 Correct 172 ms 14852 KB Output is correct
12 Correct 192 ms 15404 KB Output is correct
13 Correct 174 ms 14356 KB Output is correct
14 Correct 170 ms 14764 KB Output is correct
15 Correct 219 ms 15224 KB Output is correct
16 Correct 244 ms 15544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1156 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1152 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1160 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1160 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1156 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 152 ms 14896 KB Output is correct
30 Correct 49 ms 10428 KB Output is correct
31 Correct 137 ms 15408 KB Output is correct
32 Correct 161 ms 14932 KB Output is correct
33 Correct 161 ms 14932 KB Output is correct
34 Correct 165 ms 14648 KB Output is correct
35 Correct 182 ms 14872 KB Output is correct
36 Correct 223 ms 15416 KB Output is correct
37 Correct 172 ms 14852 KB Output is correct
38 Correct 192 ms 15404 KB Output is correct
39 Correct 174 ms 14356 KB Output is correct
40 Correct 170 ms 14764 KB Output is correct
41 Correct 219 ms 15224 KB Output is correct
42 Correct 244 ms 15544 KB Output is correct
43 Correct 154 ms 15212 KB Output is correct
44 Correct 54 ms 10392 KB Output is correct
45 Correct 195 ms 15700 KB Output is correct
46 Correct 152 ms 15168 KB Output is correct
47 Correct 160 ms 15148 KB Output is correct
48 Correct 194 ms 14856 KB Output is correct
49 Correct 230 ms 15556 KB Output is correct
50 Correct 175 ms 15040 KB Output is correct
51 Correct 188 ms 15440 KB Output is correct
52 Correct 199 ms 15704 KB Output is correct
53 Correct 194 ms 14816 KB Output is correct
54 Correct 200 ms 15180 KB Output is correct
55 Correct 219 ms 15528 KB Output is correct
56 Correct 236 ms 15680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1156 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1152 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1160 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1160 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1156 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1184 KB Output is correct
30 Correct 2 ms 1192 KB Output is correct
31 Correct 2 ms 1292 KB Output is correct
32 Correct 2 ms 1292 KB Output is correct
33 Correct 2 ms 1236 KB Output is correct
34 Correct 2 ms 1320 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 2 ms 1296 KB Output is correct
37 Correct 2 ms 1292 KB Output is correct
38 Correct 1 ms 1168 KB Output is correct
39 Correct 2 ms 1236 KB Output is correct
40 Correct 2 ms 1236 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1236 KB Output is correct
43 Correct 2 ms 1236 KB Output is correct
44 Correct 2 ms 1236 KB Output is correct
45 Correct 2 ms 1236 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 2 ms 1236 KB Output is correct
49 Correct 2 ms 1236 KB Output is correct
50 Correct 3 ms 1236 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1316 KB Output is correct
53 Correct 2 ms 1236 KB Output is correct
54 Correct 3 ms 1288 KB Output is correct
55 Correct 112 ms 14632 KB Output is correct
56 Correct 41 ms 10252 KB Output is correct
57 Correct 96 ms 15136 KB Output is correct
58 Correct 124 ms 14648 KB Output is correct
59 Correct 155 ms 14676 KB Output is correct
60 Correct 192 ms 15144 KB Output is correct
61 Correct 151 ms 14624 KB Output is correct
62 Correct 156 ms 15144 KB Output is correct
63 Correct 133 ms 14388 KB Output is correct
64 Correct 172 ms 14836 KB Output is correct
65 Correct 199 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1156 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1152 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1160 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1160 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 1 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1156 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1184 KB Output is correct
30 Correct 2 ms 1192 KB Output is correct
31 Correct 2 ms 1292 KB Output is correct
32 Correct 2 ms 1292 KB Output is correct
33 Correct 2 ms 1236 KB Output is correct
34 Correct 2 ms 1320 KB Output is correct
35 Correct 2 ms 1236 KB Output is correct
36 Correct 2 ms 1296 KB Output is correct
37 Correct 2 ms 1292 KB Output is correct
38 Correct 1 ms 1168 KB Output is correct
39 Correct 2 ms 1236 KB Output is correct
40 Correct 2 ms 1236 KB Output is correct
41 Correct 2 ms 1236 KB Output is correct
42 Correct 2 ms 1236 KB Output is correct
43 Correct 2 ms 1236 KB Output is correct
44 Correct 2 ms 1236 KB Output is correct
45 Correct 2 ms 1236 KB Output is correct
46 Correct 2 ms 1236 KB Output is correct
47 Correct 2 ms 1236 KB Output is correct
48 Correct 2 ms 1236 KB Output is correct
49 Correct 2 ms 1236 KB Output is correct
50 Correct 3 ms 1236 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1316 KB Output is correct
53 Correct 2 ms 1236 KB Output is correct
54 Correct 3 ms 1288 KB Output is correct
55 Correct 152 ms 14896 KB Output is correct
56 Correct 49 ms 10428 KB Output is correct
57 Correct 137 ms 15408 KB Output is correct
58 Correct 161 ms 14932 KB Output is correct
59 Correct 161 ms 14932 KB Output is correct
60 Correct 165 ms 14648 KB Output is correct
61 Correct 182 ms 14872 KB Output is correct
62 Correct 223 ms 15416 KB Output is correct
63 Correct 172 ms 14852 KB Output is correct
64 Correct 192 ms 15404 KB Output is correct
65 Correct 174 ms 14356 KB Output is correct
66 Correct 170 ms 14764 KB Output is correct
67 Correct 219 ms 15224 KB Output is correct
68 Correct 244 ms 15544 KB Output is correct
69 Correct 154 ms 15212 KB Output is correct
70 Correct 54 ms 10392 KB Output is correct
71 Correct 195 ms 15700 KB Output is correct
72 Correct 152 ms 15168 KB Output is correct
73 Correct 160 ms 15148 KB Output is correct
74 Correct 194 ms 14856 KB Output is correct
75 Correct 230 ms 15556 KB Output is correct
76 Correct 175 ms 15040 KB Output is correct
77 Correct 188 ms 15440 KB Output is correct
78 Correct 199 ms 15704 KB Output is correct
79 Correct 194 ms 14816 KB Output is correct
80 Correct 200 ms 15180 KB Output is correct
81 Correct 219 ms 15528 KB Output is correct
82 Correct 236 ms 15680 KB Output is correct
83 Correct 112 ms 14632 KB Output is correct
84 Correct 41 ms 10252 KB Output is correct
85 Correct 96 ms 15136 KB Output is correct
86 Correct 124 ms 14648 KB Output is correct
87 Correct 155 ms 14676 KB Output is correct
88 Correct 192 ms 15144 KB Output is correct
89 Correct 151 ms 14624 KB Output is correct
90 Correct 156 ms 15144 KB Output is correct
91 Correct 133 ms 14388 KB Output is correct
92 Correct 172 ms 14836 KB Output is correct
93 Correct 199 ms 15196 KB Output is correct
94 Correct 158 ms 15824 KB Output is correct
95 Correct 186 ms 16272 KB Output is correct
96 Correct 139 ms 16324 KB Output is correct
97 Correct 162 ms 15832 KB Output is correct
98 Correct 166 ms 15900 KB Output is correct
99 Correct 194 ms 15724 KB Output is correct
100 Correct 214 ms 16556 KB Output is correct
101 Correct 188 ms 15944 KB Output is correct
102 Correct 206 ms 16196 KB Output is correct
103 Correct 215 ms 16464 KB Output is correct
104 Correct 181 ms 15600 KB Output is correct
105 Correct 221 ms 16052 KB Output is correct
106 Correct 233 ms 16356 KB Output is correct
107 Correct 130 ms 16560 KB Output is correct
108 Correct 157 ms 15664 KB Output is correct
109 Correct 161 ms 15708 KB Output is correct
110 Correct 164 ms 15672 KB Output is correct
111 Correct 157 ms 15644 KB Output is correct
112 Correct 158 ms 15640 KB Output is correct
113 Correct 155 ms 15672 KB Output is correct
114 Correct 157 ms 15680 KB Output is correct
115 Correct 164 ms 15828 KB Output is correct
116 Correct 156 ms 15700 KB Output is correct