Submission #254788

# Submission time Handle Problem Language Result Execution time Memory
254788 2020-07-30T15:21:27 Z model_code Joker (BOI20_joker) C++11
100 / 100
775 ms 87520 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]
    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]
    scanf("%d %d", l+i, r+i);
    ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 3 ms 1024 KB Output is correct
31 Correct 3 ms 896 KB Output is correct
32 Correct 2 ms 768 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 1024 KB Output is correct
35 Correct 4 ms 1024 KB Output is correct
36 Correct 3 ms 768 KB Output is correct
37 Correct 4 ms 1152 KB Output is correct
38 Correct 3 ms 512 KB Output is correct
39 Correct 4 ms 1024 KB Output is correct
40 Correct 3 ms 768 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 2 ms 768 KB Output is correct
43 Correct 2 ms 768 KB Output is correct
44 Correct 2 ms 768 KB Output is correct
45 Correct 3 ms 896 KB Output is correct
46 Correct 4 ms 1024 KB Output is correct
47 Correct 3 ms 768 KB Output is correct
48 Correct 2 ms 768 KB Output is correct
49 Correct 4 ms 896 KB Output is correct
50 Correct 4 ms 1024 KB Output is correct
51 Correct 3 ms 768 KB Output is correct
52 Correct 2 ms 768 KB Output is correct
53 Correct 4 ms 896 KB Output is correct
54 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 348 ms 35440 KB Output is correct
4 Correct 153 ms 11984 KB Output is correct
5 Correct 452 ms 68556 KB Output is correct
6 Correct 328 ms 40552 KB Output is correct
7 Correct 303 ms 40944 KB Output is correct
8 Correct 254 ms 26480 KB Output is correct
9 Correct 359 ms 39940 KB Output is correct
10 Correct 676 ms 72372 KB Output is correct
11 Correct 526 ms 39404 KB Output is correct
12 Correct 664 ms 69740 KB Output is correct
13 Correct 195 ms 12788 KB Output is correct
14 Correct 261 ms 26364 KB Output is correct
15 Correct 518 ms 52332 KB Output is correct
16 Correct 605 ms 72296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 348 ms 35440 KB Output is correct
30 Correct 153 ms 11984 KB Output is correct
31 Correct 452 ms 68556 KB Output is correct
32 Correct 328 ms 40552 KB Output is correct
33 Correct 303 ms 40944 KB Output is correct
34 Correct 254 ms 26480 KB Output is correct
35 Correct 359 ms 39940 KB Output is correct
36 Correct 676 ms 72372 KB Output is correct
37 Correct 526 ms 39404 KB Output is correct
38 Correct 664 ms 69740 KB Output is correct
39 Correct 195 ms 12788 KB Output is correct
40 Correct 261 ms 26364 KB Output is correct
41 Correct 518 ms 52332 KB Output is correct
42 Correct 605 ms 72296 KB Output is correct
43 Correct 376 ms 35564 KB Output is correct
44 Correct 143 ms 12004 KB Output is correct
45 Correct 775 ms 87520 KB Output is correct
46 Correct 283 ms 41768 KB Output is correct
47 Correct 275 ms 40552 KB Output is correct
48 Correct 336 ms 39404 KB Output is correct
49 Correct 583 ms 73192 KB Output is correct
50 Correct 327 ms 33924 KB Output is correct
51 Correct 387 ms 52508 KB Output is correct
52 Correct 570 ms 71976 KB Output is correct
53 Correct 208 ms 12660 KB Output is correct
54 Correct 325 ms 34152 KB Output is correct
55 Correct 426 ms 52204 KB Output is correct
56 Correct 529 ms 73028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 3 ms 1024 KB Output is correct
31 Correct 3 ms 896 KB Output is correct
32 Correct 2 ms 768 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 1024 KB Output is correct
35 Correct 4 ms 1024 KB Output is correct
36 Correct 3 ms 768 KB Output is correct
37 Correct 4 ms 1152 KB Output is correct
38 Correct 3 ms 512 KB Output is correct
39 Correct 4 ms 1024 KB Output is correct
40 Correct 3 ms 768 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 2 ms 768 KB Output is correct
43 Correct 2 ms 768 KB Output is correct
44 Correct 2 ms 768 KB Output is correct
45 Correct 3 ms 896 KB Output is correct
46 Correct 4 ms 1024 KB Output is correct
47 Correct 3 ms 768 KB Output is correct
48 Correct 2 ms 768 KB Output is correct
49 Correct 4 ms 896 KB Output is correct
50 Correct 4 ms 1024 KB Output is correct
51 Correct 3 ms 768 KB Output is correct
52 Correct 2 ms 768 KB Output is correct
53 Correct 4 ms 896 KB Output is correct
54 Correct 4 ms 1024 KB Output is correct
55 Correct 332 ms 33260 KB Output is correct
56 Correct 86 ms 10348 KB Output is correct
57 Correct 375 ms 67428 KB Output is correct
58 Correct 222 ms 39272 KB Output is correct
59 Correct 284 ms 37228 KB Output is correct
60 Correct 421 ms 56480 KB Output is correct
61 Correct 325 ms 39020 KB Output is correct
62 Correct 502 ms 65856 KB Output is correct
63 Correct 169 ms 17648 KB Output is correct
64 Correct 310 ms 43244 KB Output is correct
65 Correct 452 ms 62528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 452 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 3 ms 1024 KB Output is correct
31 Correct 3 ms 896 KB Output is correct
32 Correct 2 ms 768 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 3 ms 1024 KB Output is correct
35 Correct 4 ms 1024 KB Output is correct
36 Correct 3 ms 768 KB Output is correct
37 Correct 4 ms 1152 KB Output is correct
38 Correct 3 ms 512 KB Output is correct
39 Correct 4 ms 1024 KB Output is correct
40 Correct 3 ms 768 KB Output is correct
41 Correct 4 ms 768 KB Output is correct
42 Correct 2 ms 768 KB Output is correct
43 Correct 2 ms 768 KB Output is correct
44 Correct 2 ms 768 KB Output is correct
45 Correct 3 ms 896 KB Output is correct
46 Correct 4 ms 1024 KB Output is correct
47 Correct 3 ms 768 KB Output is correct
48 Correct 2 ms 768 KB Output is correct
49 Correct 4 ms 896 KB Output is correct
50 Correct 4 ms 1024 KB Output is correct
51 Correct 3 ms 768 KB Output is correct
52 Correct 2 ms 768 KB Output is correct
53 Correct 4 ms 896 KB Output is correct
54 Correct 4 ms 1024 KB Output is correct
55 Correct 348 ms 35440 KB Output is correct
56 Correct 153 ms 11984 KB Output is correct
57 Correct 452 ms 68556 KB Output is correct
58 Correct 328 ms 40552 KB Output is correct
59 Correct 303 ms 40944 KB Output is correct
60 Correct 254 ms 26480 KB Output is correct
61 Correct 359 ms 39940 KB Output is correct
62 Correct 676 ms 72372 KB Output is correct
63 Correct 526 ms 39404 KB Output is correct
64 Correct 664 ms 69740 KB Output is correct
65 Correct 195 ms 12788 KB Output is correct
66 Correct 261 ms 26364 KB Output is correct
67 Correct 518 ms 52332 KB Output is correct
68 Correct 605 ms 72296 KB Output is correct
69 Correct 376 ms 35564 KB Output is correct
70 Correct 143 ms 12004 KB Output is correct
71 Correct 775 ms 87520 KB Output is correct
72 Correct 283 ms 41768 KB Output is correct
73 Correct 275 ms 40552 KB Output is correct
74 Correct 336 ms 39404 KB Output is correct
75 Correct 583 ms 73192 KB Output is correct
76 Correct 327 ms 33924 KB Output is correct
77 Correct 387 ms 52508 KB Output is correct
78 Correct 570 ms 71976 KB Output is correct
79 Correct 208 ms 12660 KB Output is correct
80 Correct 325 ms 34152 KB Output is correct
81 Correct 426 ms 52204 KB Output is correct
82 Correct 529 ms 73028 KB Output is correct
83 Correct 332 ms 33260 KB Output is correct
84 Correct 86 ms 10348 KB Output is correct
85 Correct 375 ms 67428 KB Output is correct
86 Correct 222 ms 39272 KB Output is correct
87 Correct 284 ms 37228 KB Output is correct
88 Correct 421 ms 56480 KB Output is correct
89 Correct 325 ms 39020 KB Output is correct
90 Correct 502 ms 65856 KB Output is correct
91 Correct 169 ms 17648 KB Output is correct
92 Correct 310 ms 43244 KB Output is correct
93 Correct 452 ms 62528 KB Output is correct
94 Correct 394 ms 35520 KB Output is correct
95 Correct 631 ms 76900 KB Output is correct
96 Correct 468 ms 69000 KB Output is correct
97 Correct 311 ms 41464 KB Output is correct
98 Correct 325 ms 43368 KB Output is correct
99 Correct 267 ms 26864 KB Output is correct
100 Correct 463 ms 76820 KB Output is correct
101 Correct 301 ms 34028 KB Output is correct
102 Correct 396 ms 52692 KB Output is correct
103 Correct 509 ms 72428 KB Output is correct
104 Correct 234 ms 19824 KB Output is correct
105 Correct 387 ms 46184 KB Output is correct
106 Correct 509 ms 64620 KB Output is correct
107 Correct 235 ms 66804 KB Output is correct
108 Correct 352 ms 35436 KB Output is correct
109 Correct 360 ms 35456 KB Output is correct
110 Correct 379 ms 35352 KB Output is correct
111 Correct 358 ms 35436 KB Output is correct
112 Correct 399 ms 35432 KB Output is correct
113 Correct 354 ms 35432 KB Output is correct
114 Correct 339 ms 35432 KB Output is correct
115 Correct 350 ms 35436 KB Output is correct
116 Correct 347 ms 35432 KB Output is correct