답안 #1102068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102068 2024-10-17T11:30:35 Z epicci23 Joker (BOI20_joker) C++17
100 / 100
352 ms 101276 KB
#include <iostream>
#include <fstream>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <functional>
#include <limits>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef vector<vector<int> > graph;

const double pi = acos(-1.0);

#define oned(a, x1, x2) { cout << #a << ":"; for(int _i = (x1); _i < (x2); _i++){ cout << " " << a[_i]; } cout << endl; }
#define twod(a, x1, x2, y1, y2) { cout << #a << ":" << endl; for(int _i = (x1); _i < (x2); _i++){ for(int _j = (y1); _j < (y2); _j++){ cout << (_j > y1 ? " " : "") << a[_i][_j]; } cout << endl; } }

#define mp make_pair
#define pb push_back
#define fst first
#define snd second

const int MAXN = 200005;

int parent[MAXN<<1];
int Size[MAXN<<1];

int depth = 0;
int par[50][MAXN<<1];
int sz[50][MAXN<<1];
vector<int> mem[50];

void setParent(int v, int p) {
	if(par[depth][v]==0) {
		par[depth][v] = parent[v];
		sz[depth][v] = Size[v];
		mem[depth].pb(v);
	}
	parent[v] = p;
}

int getParent(int v) {
	while(parent[v] != v) {
		v = parent[v];
	}
	return v;
}

bool mergeSets(int u, int v) {
	int pu = getParent(u<<1);
	int pv = getParent(v<<1);
	bool res;
	if((pu>>1) == (pv>>1)) {
		res = pu^pv;
	} else {
		if(Size[pu] > Size[pv]) {
			swap(pu,pv);
		}
		setParent(pu, pv^1);
		Size[pv^1] += Size[pu];
		setParent(pu^1, pv);
		Size[pv] += Size[pu^1];
		res = true;
	}
	return res;
}

void record() {
	depth++;
}

void rollback() {
	for(int i = 0; i < (int)mem[depth].size(); i++) {
		int v = mem[depth][i];
		Size[parent[v]] -= sz[depth][v];
		parent[v] = par[depth][v];
		Size[v] = sz[depth][v];
		par[depth][v] = 0;
	}
	mem[depth].clear();
	depth--;
}

int n, m, q, a[MAXN], b[MAXN], l[MAXN], r[MAXN];

void init() {
	for(int i = 2; i <= 2*n+1; i++) {
		parent[i] = i;
		Size[i] = 1;
	}
}

int last[MAXN];

void rec(int L1, int L2, int R1, int R2) {
	//cerr << L1 << ' ' << L2 << ' ' << R1 << ' ' << R2 << endl;
	
	record();
	
	int LM = (L1+L2)/2;
	for(int i = L1; i < LM; i++) {
		mergeSets(a[i],b[i]);
	}
	
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	
	record();
	
	for(int i = R2; i >= LM; i--) {
		if(!mergeSets(a[i],b[i])) {
			last[LM] = i;
			break;
		}
	}
	
	rollback();
	
	//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
	
	if(LM < L2) {
		mergeSets(a[LM],b[LM]);
		rec(LM+1,L2,max(LM+1,last[LM]),R2);
	}
	
	rollback();
	
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	
	if(L1 < LM) {
		record();
		
		for(int i = R2; i > last[LM]; i--) {
			mergeSets(a[i],b[i]);
		}
		
		rec(L1,LM-1,R1,last[LM]);
		
		rollback();
	}
	
	//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
}

void solve() {
	init();
	
	int X = 1;
	while(X <= m) {
		if(!mergeSets(a[X],b[X])) {
			break;
		}
		X++;
	}
	
	init();
	
	for(int i = X+1; i <= m; i++) {
		last[i] = m+1;
	}
	
	if(X <= m) {
		rec(1,X,1,m);
	}
	
	//oned(last,1,m+1);
	for(int i = 1; i <= m; i++) {
	//	printf("%d\n", last[i]);
	}
	
	for(int i = 1; i <= q; i++) {
		printf(last[l[i]] <= r[i] ? "NO\n" : "YES\n");
	}
}

int main() {
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	while(scanf("%d %d %d", &n, &m, &q)==3) {
		for(int i = 1; i <= m; i++) {
			scanf("%d %d", a+i, b+i);
		}
		for(int i = 1; i <= q; i++) {
			scanf("%d %d", l+i, r+i);
		}
		solve();
	}
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:196:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  196 |    scanf("%d %d", a+i, b+i);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
Joker.cpp:199:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  199 |    scanf("%d %d", l+i, r+i);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 2 ms 12624 KB Output is correct
5 Correct 2 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 1 ms 6596 KB Output is correct
8 Correct 3 ms 18768 KB Output is correct
9 Correct 3 ms 18860 KB Output is correct
10 Correct 3 ms 18772 KB Output is correct
11 Correct 2 ms 18772 KB Output is correct
12 Correct 3 ms 21240 KB Output is correct
13 Correct 3 ms 18772 KB Output is correct
14 Correct 1 ms 6484 KB Output is correct
15 Correct 3 ms 20820 KB Output is correct
16 Correct 3 ms 20820 KB Output is correct
17 Correct 3 ms 18772 KB Output is correct
18 Correct 2 ms 18772 KB Output is correct
19 Correct 3 ms 18772 KB Output is correct
20 Correct 3 ms 18772 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 2 ms 18772 KB Output is correct
23 Correct 3 ms 18772 KB Output is correct
24 Correct 3 ms 18772 KB Output is correct
25 Correct 3 ms 18772 KB Output is correct
26 Correct 3 ms 18908 KB Output is correct
27 Correct 3 ms 18772 KB Output is correct
28 Correct 3 ms 18772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 2 ms 12624 KB Output is correct
5 Correct 2 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 1 ms 6596 KB Output is correct
8 Correct 3 ms 18768 KB Output is correct
9 Correct 3 ms 18860 KB Output is correct
10 Correct 3 ms 18772 KB Output is correct
11 Correct 2 ms 18772 KB Output is correct
12 Correct 3 ms 21240 KB Output is correct
13 Correct 3 ms 18772 KB Output is correct
14 Correct 1 ms 6484 KB Output is correct
15 Correct 3 ms 20820 KB Output is correct
16 Correct 3 ms 20820 KB Output is correct
17 Correct 3 ms 18772 KB Output is correct
18 Correct 2 ms 18772 KB Output is correct
19 Correct 3 ms 18772 KB Output is correct
20 Correct 3 ms 18772 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 2 ms 18772 KB Output is correct
23 Correct 3 ms 18772 KB Output is correct
24 Correct 3 ms 18772 KB Output is correct
25 Correct 3 ms 18772 KB Output is correct
26 Correct 3 ms 18908 KB Output is correct
27 Correct 3 ms 18772 KB Output is correct
28 Correct 3 ms 18772 KB Output is correct
29 Correct 2 ms 6484 KB Output is correct
30 Correct 5 ms 25320 KB Output is correct
31 Correct 4 ms 25172 KB Output is correct
32 Correct 4 ms 25172 KB Output is correct
33 Correct 4 ms 23288 KB Output is correct
34 Correct 5 ms 25428 KB Output is correct
35 Correct 4 ms 25428 KB Output is correct
36 Correct 4 ms 25336 KB Output is correct
37 Correct 5 ms 25592 KB Output is correct
38 Correct 2 ms 6484 KB Output is correct
39 Correct 4 ms 25276 KB Output is correct
40 Correct 5 ms 25172 KB Output is correct
41 Correct 5 ms 25172 KB Output is correct
42 Correct 5 ms 25172 KB Output is correct
43 Correct 4 ms 25172 KB Output is correct
44 Correct 4 ms 25172 KB Output is correct
45 Correct 4 ms 25172 KB Output is correct
46 Correct 5 ms 25440 KB Output is correct
47 Correct 4 ms 25432 KB Output is correct
48 Correct 4 ms 25184 KB Output is correct
49 Correct 6 ms 25184 KB Output is correct
50 Correct 5 ms 25592 KB Output is correct
51 Correct 4 ms 25172 KB Output is correct
52 Correct 5 ms 25316 KB Output is correct
53 Correct 4 ms 25172 KB Output is correct
54 Correct 5 ms 25428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 165 ms 52160 KB Output is correct
4 Correct 54 ms 14524 KB Output is correct
5 Correct 214 ms 78748 KB Output is correct
6 Correct 134 ms 59768 KB Output is correct
7 Correct 133 ms 60096 KB Output is correct
8 Correct 117 ms 51304 KB Output is correct
9 Correct 160 ms 58536 KB Output is correct
10 Correct 271 ms 77600 KB Output is correct
11 Correct 194 ms 58560 KB Output is correct
12 Correct 257 ms 78528 KB Output is correct
13 Correct 110 ms 44492 KB Output is correct
14 Correct 124 ms 51752 KB Output is correct
15 Correct 223 ms 67492 KB Output is correct
16 Correct 284 ms 77984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 2 ms 12624 KB Output is correct
5 Correct 2 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 1 ms 6596 KB Output is correct
8 Correct 3 ms 18768 KB Output is correct
9 Correct 3 ms 18860 KB Output is correct
10 Correct 3 ms 18772 KB Output is correct
11 Correct 2 ms 18772 KB Output is correct
12 Correct 3 ms 21240 KB Output is correct
13 Correct 3 ms 18772 KB Output is correct
14 Correct 1 ms 6484 KB Output is correct
15 Correct 3 ms 20820 KB Output is correct
16 Correct 3 ms 20820 KB Output is correct
17 Correct 3 ms 18772 KB Output is correct
18 Correct 2 ms 18772 KB Output is correct
19 Correct 3 ms 18772 KB Output is correct
20 Correct 3 ms 18772 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 2 ms 18772 KB Output is correct
23 Correct 3 ms 18772 KB Output is correct
24 Correct 3 ms 18772 KB Output is correct
25 Correct 3 ms 18772 KB Output is correct
26 Correct 3 ms 18908 KB Output is correct
27 Correct 3 ms 18772 KB Output is correct
28 Correct 3 ms 18772 KB Output is correct
29 Correct 165 ms 52160 KB Output is correct
30 Correct 54 ms 14524 KB Output is correct
31 Correct 214 ms 78748 KB Output is correct
32 Correct 134 ms 59768 KB Output is correct
33 Correct 133 ms 60096 KB Output is correct
34 Correct 117 ms 51304 KB Output is correct
35 Correct 160 ms 58536 KB Output is correct
36 Correct 271 ms 77600 KB Output is correct
37 Correct 194 ms 58560 KB Output is correct
38 Correct 257 ms 78528 KB Output is correct
39 Correct 110 ms 44492 KB Output is correct
40 Correct 124 ms 51752 KB Output is correct
41 Correct 223 ms 67492 KB Output is correct
42 Correct 284 ms 77984 KB Output is correct
43 Correct 177 ms 56464 KB Output is correct
44 Correct 70 ms 18716 KB Output is correct
45 Correct 337 ms 101276 KB Output is correct
46 Correct 127 ms 61288 KB Output is correct
47 Correct 126 ms 59808 KB Output is correct
48 Correct 168 ms 58304 KB Output is correct
49 Correct 258 ms 79040 KB Output is correct
50 Correct 149 ms 56512 KB Output is correct
51 Correct 187 ms 67776 KB Output is correct
52 Correct 229 ms 78084 KB Output is correct
53 Correct 106 ms 44492 KB Output is correct
54 Correct 162 ms 56596 KB Output is correct
55 Correct 236 ms 67788 KB Output is correct
56 Correct 275 ms 79040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 2 ms 12624 KB Output is correct
5 Correct 2 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 1 ms 6596 KB Output is correct
8 Correct 3 ms 18768 KB Output is correct
9 Correct 3 ms 18860 KB Output is correct
10 Correct 3 ms 18772 KB Output is correct
11 Correct 2 ms 18772 KB Output is correct
12 Correct 3 ms 21240 KB Output is correct
13 Correct 3 ms 18772 KB Output is correct
14 Correct 1 ms 6484 KB Output is correct
15 Correct 3 ms 20820 KB Output is correct
16 Correct 3 ms 20820 KB Output is correct
17 Correct 3 ms 18772 KB Output is correct
18 Correct 2 ms 18772 KB Output is correct
19 Correct 3 ms 18772 KB Output is correct
20 Correct 3 ms 18772 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 2 ms 18772 KB Output is correct
23 Correct 3 ms 18772 KB Output is correct
24 Correct 3 ms 18772 KB Output is correct
25 Correct 3 ms 18772 KB Output is correct
26 Correct 3 ms 18908 KB Output is correct
27 Correct 3 ms 18772 KB Output is correct
28 Correct 3 ms 18772 KB Output is correct
29 Correct 2 ms 6484 KB Output is correct
30 Correct 5 ms 25320 KB Output is correct
31 Correct 4 ms 25172 KB Output is correct
32 Correct 4 ms 25172 KB Output is correct
33 Correct 4 ms 23288 KB Output is correct
34 Correct 5 ms 25428 KB Output is correct
35 Correct 4 ms 25428 KB Output is correct
36 Correct 4 ms 25336 KB Output is correct
37 Correct 5 ms 25592 KB Output is correct
38 Correct 2 ms 6484 KB Output is correct
39 Correct 4 ms 25276 KB Output is correct
40 Correct 5 ms 25172 KB Output is correct
41 Correct 5 ms 25172 KB Output is correct
42 Correct 5 ms 25172 KB Output is correct
43 Correct 4 ms 25172 KB Output is correct
44 Correct 4 ms 25172 KB Output is correct
45 Correct 4 ms 25172 KB Output is correct
46 Correct 5 ms 25440 KB Output is correct
47 Correct 4 ms 25432 KB Output is correct
48 Correct 4 ms 25184 KB Output is correct
49 Correct 6 ms 25184 KB Output is correct
50 Correct 5 ms 25592 KB Output is correct
51 Correct 4 ms 25172 KB Output is correct
52 Correct 5 ms 25316 KB Output is correct
53 Correct 4 ms 25172 KB Output is correct
54 Correct 5 ms 25428 KB Output is correct
55 Correct 169 ms 53696 KB Output is correct
56 Correct 37 ms 17088 KB Output is correct
57 Correct 202 ms 77552 KB Output is correct
58 Correct 116 ms 58304 KB Output is correct
59 Correct 149 ms 56260 KB Output is correct
60 Correct 230 ms 68288 KB Output is correct
61 Correct 167 ms 58392 KB Output is correct
62 Correct 239 ms 74688 KB Output is correct
63 Correct 87 ms 46116 KB Output is correct
64 Correct 194 ms 61352 KB Output is correct
65 Correct 260 ms 71104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6480 KB Output is correct
2 Correct 1 ms 6480 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 2 ms 12624 KB Output is correct
5 Correct 2 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 1 ms 6596 KB Output is correct
8 Correct 3 ms 18768 KB Output is correct
9 Correct 3 ms 18860 KB Output is correct
10 Correct 3 ms 18772 KB Output is correct
11 Correct 2 ms 18772 KB Output is correct
12 Correct 3 ms 21240 KB Output is correct
13 Correct 3 ms 18772 KB Output is correct
14 Correct 1 ms 6484 KB Output is correct
15 Correct 3 ms 20820 KB Output is correct
16 Correct 3 ms 20820 KB Output is correct
17 Correct 3 ms 18772 KB Output is correct
18 Correct 2 ms 18772 KB Output is correct
19 Correct 3 ms 18772 KB Output is correct
20 Correct 3 ms 18772 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 2 ms 18772 KB Output is correct
23 Correct 3 ms 18772 KB Output is correct
24 Correct 3 ms 18772 KB Output is correct
25 Correct 3 ms 18772 KB Output is correct
26 Correct 3 ms 18908 KB Output is correct
27 Correct 3 ms 18772 KB Output is correct
28 Correct 3 ms 18772 KB Output is correct
29 Correct 2 ms 6484 KB Output is correct
30 Correct 5 ms 25320 KB Output is correct
31 Correct 4 ms 25172 KB Output is correct
32 Correct 4 ms 25172 KB Output is correct
33 Correct 4 ms 23288 KB Output is correct
34 Correct 5 ms 25428 KB Output is correct
35 Correct 4 ms 25428 KB Output is correct
36 Correct 4 ms 25336 KB Output is correct
37 Correct 5 ms 25592 KB Output is correct
38 Correct 2 ms 6484 KB Output is correct
39 Correct 4 ms 25276 KB Output is correct
40 Correct 5 ms 25172 KB Output is correct
41 Correct 5 ms 25172 KB Output is correct
42 Correct 5 ms 25172 KB Output is correct
43 Correct 4 ms 25172 KB Output is correct
44 Correct 4 ms 25172 KB Output is correct
45 Correct 4 ms 25172 KB Output is correct
46 Correct 5 ms 25440 KB Output is correct
47 Correct 4 ms 25432 KB Output is correct
48 Correct 4 ms 25184 KB Output is correct
49 Correct 6 ms 25184 KB Output is correct
50 Correct 5 ms 25592 KB Output is correct
51 Correct 4 ms 25172 KB Output is correct
52 Correct 5 ms 25316 KB Output is correct
53 Correct 4 ms 25172 KB Output is correct
54 Correct 5 ms 25428 KB Output is correct
55 Correct 165 ms 52160 KB Output is correct
56 Correct 54 ms 14524 KB Output is correct
57 Correct 214 ms 78748 KB Output is correct
58 Correct 134 ms 59768 KB Output is correct
59 Correct 133 ms 60096 KB Output is correct
60 Correct 117 ms 51304 KB Output is correct
61 Correct 160 ms 58536 KB Output is correct
62 Correct 271 ms 77600 KB Output is correct
63 Correct 194 ms 58560 KB Output is correct
64 Correct 257 ms 78528 KB Output is correct
65 Correct 110 ms 44492 KB Output is correct
66 Correct 124 ms 51752 KB Output is correct
67 Correct 223 ms 67492 KB Output is correct
68 Correct 284 ms 77984 KB Output is correct
69 Correct 177 ms 56464 KB Output is correct
70 Correct 70 ms 18716 KB Output is correct
71 Correct 337 ms 101276 KB Output is correct
72 Correct 127 ms 61288 KB Output is correct
73 Correct 126 ms 59808 KB Output is correct
74 Correct 168 ms 58304 KB Output is correct
75 Correct 258 ms 79040 KB Output is correct
76 Correct 149 ms 56512 KB Output is correct
77 Correct 187 ms 67776 KB Output is correct
78 Correct 229 ms 78084 KB Output is correct
79 Correct 106 ms 44492 KB Output is correct
80 Correct 162 ms 56596 KB Output is correct
81 Correct 236 ms 67788 KB Output is correct
82 Correct 275 ms 79040 KB Output is correct
83 Correct 169 ms 53696 KB Output is correct
84 Correct 37 ms 17088 KB Output is correct
85 Correct 202 ms 77552 KB Output is correct
86 Correct 116 ms 58304 KB Output is correct
87 Correct 149 ms 56260 KB Output is correct
88 Correct 230 ms 68288 KB Output is correct
89 Correct 167 ms 58392 KB Output is correct
90 Correct 239 ms 74688 KB Output is correct
91 Correct 87 ms 46116 KB Output is correct
92 Correct 194 ms 61352 KB Output is correct
93 Correct 260 ms 71104 KB Output is correct
94 Correct 185 ms 57024 KB Output is correct
95 Correct 352 ms 84412 KB Output is correct
96 Correct 231 ms 79804 KB Output is correct
97 Correct 150 ms 61480 KB Output is correct
98 Correct 167 ms 63168 KB Output is correct
99 Correct 144 ms 53160 KB Output is correct
100 Correct 257 ms 83396 KB Output is correct
101 Correct 177 ms 57024 KB Output is correct
102 Correct 231 ms 68544 KB Output is correct
103 Correct 276 ms 78884 KB Output is correct
104 Correct 152 ms 49224 KB Output is correct
105 Correct 225 ms 65236 KB Output is correct
106 Correct 291 ms 74344 KB Output is correct
107 Correct 129 ms 81728 KB Output is correct
108 Correct 177 ms 56768 KB Output is correct
109 Correct 155 ms 56784 KB Output is correct
110 Correct 175 ms 56868 KB Output is correct
111 Correct 157 ms 56768 KB Output is correct
112 Correct 166 ms 56996 KB Output is correct
113 Correct 161 ms 57000 KB Output is correct
114 Correct 157 ms 56768 KB Output is correct
115 Correct 156 ms 56784 KB Output is correct
116 Correct 151 ms 56768 KB Output is correct