답안 #291899

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
291899 2020-09-06T01:02:54 Z Berted Joker (BOI20_joker) C++14
100 / 100
405 ms 18668 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
#define vi vector<int>
#define pii pair<int, int>
#define fst first
#define vpi vector<pii>
#define ppi pair<pii, int>
#define snd second

using namespace std;

/*
	Idea : 
	Define R_i as the right-most index such that Query(i .. R[i]) = "YES"
	Observe that R[i - 1] <= R[i] for all i
	Therefore, we can do DnC
	:D 
*/

int n, m, q;

pii edge[200001];
int ans[200001], sz[200001];
pair<int, bool> par[200001];

pair<int, bool> fn(int u)
{
	if (u == par[u].fst) return par[u];
	else
	{
		pair<int, bool> ret = fn(par[u].fst);
		ret.snd ^= par[u].snd;
		return ret;
	}
}

inline bool jn(int a, int b, vpi& rb)
{
	pair<int, bool> parA = fn(a), parB = fn(b);
	// cout << "JOIN " << a << " - " << b << " " << parA.snd << " " << parB.snd << " , " << parA.fst << " - " << parB.fst << "\n";
	if (parA.fst != parB.fst)
	{
		if (sz[parA.fst] < sz[parB.fst]) {swap(parA, parB);}
		rb.push_back({parB.fst, sz[parB.fst]});
		sz[parA.fst] += sz[parB.fst];
		par[parB.fst] = {parA.fst, parA.snd == parB.snd};
		return 1;
	}
	return parA.snd != parB.snd;
}

inline void rollbackOp(vpi& rb)
{
	for (int i = rb.size() - 1; i >= 0; i--)
	{
		sz[par[rb[i].fst].fst] -= rb[i].snd;
		par[rb[i].fst] = {rb[i].fst, 0};
	}
	rb.clear();
}

void solve(int L, int R, int lb, int ub)
{
	if (L <= R)
	{
		int M = L + R >> 1; int early = ub + 1; vpi rbVec;
		// cout << "SOLVE " << L << " " << R << " " << M << " " << lb << " " << ub << "\n";
		for (int i = L; i < M; i++)
		{
			if (jn(edge[i].fst, edge[i].snd, rbVec) == 0) {early = i; break;}
		}
		if (early > ub)
		{
			for (int i = ub; i >= max(M, lb); i--)
			{
				if (jn(edge[i].fst, edge[i].snd, rbVec) == 0) {early = i; break;}
			}
		}
		rollbackOp(rbVec);
		if (early < M)
		{
			for (int i = early + 1; i <= R; i++) 
			{
				ans[i] = m;
				// cout << i << " -> " << ans[i] << "\n";
			}
			solve(L, early, lb, ub);
		}
		else if (early > ub)
		{
			for (int i = L; i <= R; i++) {ans[i] = i - 1;}
		}
		else
		{
			ans[M] = early - 1;
			// cout << M << " -> " << ans[M] << "\n";

			for (int i = ub; i > early; i--) {jn(edge[i].fst, edge[i].snd, rbVec);}
			solve(L, M - 1, lb, early);
			rollbackOp(rbVec);

			bool b = 1;
			for (int i = L; i <= M; i++) {b &= jn(edge[i].fst, edge[i].snd, rbVec);}
			if (b) solve(M + 1, R, early, ub);
			else for (int i = M + 1; i <= R; i++) {ans[i] = m;}
			rollbackOp(rbVec);
		}	
	}
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m >> q;
	for (int i = 0; i < n; i++) {par[i] = {i, 0}; sz[i] = 1;}
	for (int i = 0; i < m; i++)
	{
		int u, v; cin >> u >> v;
		edge[i] = {u - 1, v - 1};
	}
	solve(0, m - 1, 0, m - 1);
	for (int i = 0; i < q; i++)
	{
		int l, r; cin >> l >> r; l--; r--;
		if (r > ans[l]) {cout << "NO\n";}
		else {cout << "YES\n";}
	}
	return 0;
}

Compilation message

Joker.cpp: In function 'void solve(int, int, int, int)':
Joker.cpp:68:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |   int M = L + R >> 1; int early = ub + 1; vpi rbVec;
      |           ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 248 ms 8728 KB Output is correct
4 Correct 91 ms 9452 KB Output is correct
5 Correct 264 ms 9820 KB Output is correct
6 Correct 211 ms 8944 KB Output is correct
7 Correct 204 ms 9200 KB Output is correct
8 Correct 217 ms 7704 KB Output is correct
9 Correct 260 ms 8456 KB Output is correct
10 Correct 333 ms 10524 KB Output is correct
11 Correct 253 ms 8768 KB Output is correct
12 Correct 278 ms 12012 KB Output is correct
13 Correct 201 ms 7500 KB Output is correct
14 Correct 220 ms 8244 KB Output is correct
15 Correct 321 ms 9964 KB Output is correct
16 Correct 335 ms 10524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 248 ms 8728 KB Output is correct
30 Correct 91 ms 9452 KB Output is correct
31 Correct 264 ms 9820 KB Output is correct
32 Correct 211 ms 8944 KB Output is correct
33 Correct 204 ms 9200 KB Output is correct
34 Correct 217 ms 7704 KB Output is correct
35 Correct 260 ms 8456 KB Output is correct
36 Correct 333 ms 10524 KB Output is correct
37 Correct 253 ms 8768 KB Output is correct
38 Correct 278 ms 12012 KB Output is correct
39 Correct 201 ms 7500 KB Output is correct
40 Correct 220 ms 8244 KB Output is correct
41 Correct 321 ms 9964 KB Output is correct
42 Correct 335 ms 10524 KB Output is correct
43 Correct 245 ms 9004 KB Output is correct
44 Correct 99 ms 9788 KB Output is correct
45 Correct 405 ms 18668 KB Output is correct
46 Correct 197 ms 10164 KB Output is correct
47 Correct 206 ms 8980 KB Output is correct
48 Correct 279 ms 8752 KB Output is correct
49 Correct 338 ms 10992 KB Output is correct
50 Correct 235 ms 8752 KB Output is correct
51 Correct 264 ms 9840 KB Output is correct
52 Correct 296 ms 10608 KB Output is correct
53 Correct 204 ms 7824 KB Output is correct
54 Correct 272 ms 8832 KB Output is correct
55 Correct 318 ms 9680 KB Output is correct
56 Correct 352 ms 10988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 204 ms 8288 KB Output is correct
56 Correct 60 ms 9324 KB Output is correct
57 Correct 222 ms 9328 KB Output is correct
58 Correct 166 ms 8944 KB Output is correct
59 Correct 226 ms 8304 KB Output is correct
60 Correct 298 ms 9448 KB Output is correct
61 Correct 222 ms 9708 KB Output is correct
62 Correct 256 ms 11088 KB Output is correct
63 Correct 167 ms 6388 KB Output is correct
64 Correct 247 ms 8560 KB Output is correct
65 Correct 306 ms 9972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 2 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 1 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 3 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 3 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 2 ms 512 KB Output is correct
54 Correct 3 ms 512 KB Output is correct
55 Correct 248 ms 8728 KB Output is correct
56 Correct 91 ms 9452 KB Output is correct
57 Correct 264 ms 9820 KB Output is correct
58 Correct 211 ms 8944 KB Output is correct
59 Correct 204 ms 9200 KB Output is correct
60 Correct 217 ms 7704 KB Output is correct
61 Correct 260 ms 8456 KB Output is correct
62 Correct 333 ms 10524 KB Output is correct
63 Correct 253 ms 8768 KB Output is correct
64 Correct 278 ms 12012 KB Output is correct
65 Correct 201 ms 7500 KB Output is correct
66 Correct 220 ms 8244 KB Output is correct
67 Correct 321 ms 9964 KB Output is correct
68 Correct 335 ms 10524 KB Output is correct
69 Correct 245 ms 9004 KB Output is correct
70 Correct 99 ms 9788 KB Output is correct
71 Correct 405 ms 18668 KB Output is correct
72 Correct 197 ms 10164 KB Output is correct
73 Correct 206 ms 8980 KB Output is correct
74 Correct 279 ms 8752 KB Output is correct
75 Correct 338 ms 10992 KB Output is correct
76 Correct 235 ms 8752 KB Output is correct
77 Correct 264 ms 9840 KB Output is correct
78 Correct 296 ms 10608 KB Output is correct
79 Correct 204 ms 7824 KB Output is correct
80 Correct 272 ms 8832 KB Output is correct
81 Correct 318 ms 9680 KB Output is correct
82 Correct 352 ms 10988 KB Output is correct
83 Correct 204 ms 8288 KB Output is correct
84 Correct 60 ms 9324 KB Output is correct
85 Correct 222 ms 9328 KB Output is correct
86 Correct 166 ms 8944 KB Output is correct
87 Correct 226 ms 8304 KB Output is correct
88 Correct 298 ms 9448 KB Output is correct
89 Correct 222 ms 9708 KB Output is correct
90 Correct 256 ms 11088 KB Output is correct
91 Correct 167 ms 6388 KB Output is correct
92 Correct 247 ms 8560 KB Output is correct
93 Correct 306 ms 9972 KB Output is correct
94 Correct 248 ms 9640 KB Output is correct
95 Correct 390 ms 12648 KB Output is correct
96 Correct 276 ms 10704 KB Output is correct
97 Correct 213 ms 9704 KB Output is correct
98 Correct 221 ms 10224 KB Output is correct
99 Correct 246 ms 9224 KB Output is correct
100 Correct 309 ms 13036 KB Output is correct
101 Correct 241 ms 9388 KB Output is correct
102 Correct 281 ms 10208 KB Output is correct
103 Correct 335 ms 11760 KB Output is correct
104 Correct 231 ms 9048 KB Output is correct
105 Correct 307 ms 10004 KB Output is correct
106 Correct 341 ms 10920 KB Output is correct
107 Correct 209 ms 13552 KB Output is correct
108 Correct 251 ms 9512 KB Output is correct
109 Correct 250 ms 9544 KB Output is correct
110 Correct 252 ms 9564 KB Output is correct
111 Correct 258 ms 9384 KB Output is correct
112 Correct 248 ms 9500 KB Output is correct
113 Correct 268 ms 9384 KB Output is correct
114 Correct 248 ms 9560 KB Output is correct
115 Correct 254 ms 9384 KB Output is correct
116 Correct 246 ms 9380 KB Output is correct