답안 #1056120

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1056120 2024-08-13T07:53:11 Z allin27x Joker (BOI20_joker) C++17
100 / 100
357 ms 51756 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define ll __int128
const int mod = 1e9+7;
#define forn(i,n) for(int i=0; i<n; i++)
#define readv(a,n) vector<int>a(n);forn(i,n)cin>>a[i];
#define readvv(a,n,m) vector<vector<int>> a(n,vector<int>(m));forn(i,n)forn(j,m)cin>>a[i][j];
#define all(a) a.begin(), a.end()
#define _max(x,y) x = max(x,y)
#define _min(x,y) x = min(x,y)
// #define f first
// #define s second

struct DSU{
	vector<int> par;
	vector<int> size;
	stack<int> rb;
	stack<char> tp;
	stack<int> bad;
	stack<pair<int,int>> edg;
	int nb;  int A = 0; int k;
	DSU(int n){
		k = n/2; par.resize(n); for(int i=0; i<n; ++i) par[i]=i;
		size.resize(n,1); nb = 0;
	}
	int get(int p){
		return par[p]==p? p: get(par[p]);
	}
	void unite(int a, int b){
		edg.push({a,b});
		if (get(a%k) == get(b%k)) {
			nb++; bad.push(1);
		} else bad.push(0);
		a = get(a); b = get(b);
		if (a==b){
			rb.push(-1);
			return;
		}
		if (size[a] > size[b]) swap(a,b);
		rb.push(a);
		size[b]+=size[a];
		par[a] = b;
	}
	void rollbackHelper(){
		int f = rb.top();
		rb.pop();
		nb -= bad.top(); bad.pop();
		edg.pop();
		if (f==-1) return;
		size[par[f]] -= size[f];
		par[f] = f;
	}
	void change(){
		stack<pair<int,int>> edA,edB;
		edB.push({edg.top().first, edg.top().second}); rollbackHelper();
		int a=0, b=1; tp.pop();
		while (A!=a && a!=b){
			if (tp.top()=='A'){
				a++; edA.push({edg.top().first, edg.top().second}); 
			} else {
				b++; edB.push({edg.top().first, edg.top().second}); 
			}
			tp.pop(); rollbackHelper();
		}
		while (!edB.empty()){
			unite(edB.top().first, edB.top().second);
			edB.pop();
			tp.push('B');
		}
		while (!edA.empty()){
			unite(edA.top().first, edA.top().second);
			edA.pop();
			tp.push('A'); 
		}
	}

	void rollback(){
		if (tp.top() == 'B') change();
		int f = rb.top();
		rb.pop();
		nb -= bad.top(); bad.pop();
		A--; tp.pop(); edg.pop();
		if (f==-1) return;
		size[par[f]] -= size[f];
		par[f] = f;
	}
};


const int N = 2e5+4;
int ans[N];
int edg[N][2];
int bad[N];

void solve(){
	int n,m,q; cin>>n>>m>>q;
	for (int i=0; i<m; i++){
		int a,b; cin>>a>>b; a--; b--; 
		edg[i][0] = a; edg[i][1] = b;
	}
	DSU dsu(2*n);
	for (int i=0; i<m; i++){
		dsu.tp.push('A'); dsu.tp.push('A'); dsu.A+=2;
		dsu.unite(edg[i][0], edg[i][1]+n);
		dsu.unite(edg[i][0]+n, edg[i][1]);
	}
	int l=m; 
	for (int r = m-1; r>=0; r--){
		while (l>0 && dsu.nb){
			l--;
			// cout<<l<<' '<<dsu.nb<<'\n';
			dsu.rollback(); dsu.rollback();
		}
		ans[r]  = l+1;
		// cout<<l<<' '<<r<<' '<<ans[r]<<'\n';
		if (l==r+1) {dsu.rollback(); dsu.rollback(); l--;}
		dsu.unite(edg[r][0], edg[r][1] + n);
		dsu.unite(edg[r][0] + n, edg[r][1]);
		dsu.tp.push('B'); dsu.tp.push('B');
	}
	DSU d(2*n);
	for (int i=m-1; i>=0; i--){
		d.unite(edg[i][0], edg[i][1]+n);
		d.unite(edg[i][0]+n, edg[i][1]);
		if (d.nb) bad[i] = 1;
	}
	while (q--){
		int l, r; cin>>l>>r; l--; r--;
		l = min(l, r+1);
		cout<<(bad[r+1]||ans[r]<=l? "YES\n":"NO\n");
	}
}


signed main(){ 
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	while(t--){
		solve();
	}	
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2484 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2484 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 1 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 1 ms 2908 KB Output is correct
35 Correct 1 ms 2908 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 2 ms 2908 KB Output is correct
39 Correct 2 ms 2808 KB Output is correct
40 Correct 1 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2788 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 1 ms 2912 KB Output is correct
45 Correct 1 ms 2908 KB Output is correct
46 Correct 1 ms 2908 KB Output is correct
47 Correct 1 ms 2912 KB Output is correct
48 Correct 1 ms 2780 KB Output is correct
49 Correct 1 ms 2908 KB Output is correct
50 Correct 3 ms 2908 KB Output is correct
51 Correct 1 ms 2720 KB Output is correct
52 Correct 1 ms 2908 KB Output is correct
53 Correct 1 ms 2908 KB Output is correct
54 Correct 1 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 194 ms 40276 KB Output is correct
4 Correct 357 ms 50116 KB Output is correct
5 Correct 112 ms 49516 KB Output is correct
6 Correct 111 ms 44072 KB Output is correct
7 Correct 132 ms 44240 KB Output is correct
8 Correct 133 ms 41176 KB Output is correct
9 Correct 150 ms 43592 KB Output is correct
10 Correct 171 ms 50128 KB Output is correct
11 Correct 150 ms 44004 KB Output is correct
12 Correct 137 ms 49412 KB Output is correct
13 Correct 114 ms 38992 KB Output is correct
14 Correct 135 ms 41692 KB Output is correct
15 Correct 170 ms 46752 KB Output is correct
16 Correct 184 ms 50644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2484 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
29 Correct 194 ms 40276 KB Output is correct
30 Correct 357 ms 50116 KB Output is correct
31 Correct 112 ms 49516 KB Output is correct
32 Correct 111 ms 44072 KB Output is correct
33 Correct 132 ms 44240 KB Output is correct
34 Correct 133 ms 41176 KB Output is correct
35 Correct 150 ms 43592 KB Output is correct
36 Correct 171 ms 50128 KB Output is correct
37 Correct 150 ms 44004 KB Output is correct
38 Correct 137 ms 49412 KB Output is correct
39 Correct 114 ms 38992 KB Output is correct
40 Correct 135 ms 41692 KB Output is correct
41 Correct 170 ms 46752 KB Output is correct
42 Correct 184 ms 50644 KB Output is correct
43 Correct 194 ms 44512 KB Output is correct
44 Correct 349 ms 50360 KB Output is correct
45 Correct 154 ms 50756 KB Output is correct
46 Correct 111 ms 44580 KB Output is correct
47 Correct 123 ms 44496 KB Output is correct
48 Correct 156 ms 44100 KB Output is correct
49 Correct 178 ms 50640 KB Output is correct
50 Correct 144 ms 43532 KB Output is correct
51 Correct 154 ms 47060 KB Output is correct
52 Correct 157 ms 50868 KB Output is correct
53 Correct 120 ms 39376 KB Output is correct
54 Correct 147 ms 43340 KB Output is correct
55 Correct 172 ms 47060 KB Output is correct
56 Correct 169 ms 50896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2484 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 1 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 1 ms 2908 KB Output is correct
35 Correct 1 ms 2908 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 2 ms 2908 KB Output is correct
39 Correct 2 ms 2808 KB Output is correct
40 Correct 1 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2788 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 1 ms 2912 KB Output is correct
45 Correct 1 ms 2908 KB Output is correct
46 Correct 1 ms 2908 KB Output is correct
47 Correct 1 ms 2912 KB Output is correct
48 Correct 1 ms 2780 KB Output is correct
49 Correct 1 ms 2908 KB Output is correct
50 Correct 3 ms 2908 KB Output is correct
51 Correct 1 ms 2720 KB Output is correct
52 Correct 1 ms 2908 KB Output is correct
53 Correct 1 ms 2908 KB Output is correct
54 Correct 1 ms 2908 KB Output is correct
55 Correct 177 ms 41936 KB Output is correct
56 Correct 339 ms 48308 KB Output is correct
57 Correct 101 ms 47316 KB Output is correct
58 Correct 93 ms 41936 KB Output is correct
59 Correct 137 ms 41852 KB Output is correct
60 Correct 170 ms 45776 KB Output is correct
61 Correct 139 ms 41940 KB Output is correct
62 Correct 126 ms 47052 KB Output is correct
63 Correct 106 ms 38088 KB Output is correct
64 Correct 154 ms 43268 KB Output is correct
65 Correct 181 ms 47036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2484 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 1 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 1 ms 2908 KB Output is correct
35 Correct 1 ms 2908 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 2 ms 2908 KB Output is correct
39 Correct 2 ms 2808 KB Output is correct
40 Correct 1 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2788 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 1 ms 2912 KB Output is correct
45 Correct 1 ms 2908 KB Output is correct
46 Correct 1 ms 2908 KB Output is correct
47 Correct 1 ms 2912 KB Output is correct
48 Correct 1 ms 2780 KB Output is correct
49 Correct 1 ms 2908 KB Output is correct
50 Correct 3 ms 2908 KB Output is correct
51 Correct 1 ms 2720 KB Output is correct
52 Correct 1 ms 2908 KB Output is correct
53 Correct 1 ms 2908 KB Output is correct
54 Correct 1 ms 2908 KB Output is correct
55 Correct 194 ms 40276 KB Output is correct
56 Correct 357 ms 50116 KB Output is correct
57 Correct 112 ms 49516 KB Output is correct
58 Correct 111 ms 44072 KB Output is correct
59 Correct 132 ms 44240 KB Output is correct
60 Correct 133 ms 41176 KB Output is correct
61 Correct 150 ms 43592 KB Output is correct
62 Correct 171 ms 50128 KB Output is correct
63 Correct 150 ms 44004 KB Output is correct
64 Correct 137 ms 49412 KB Output is correct
65 Correct 114 ms 38992 KB Output is correct
66 Correct 135 ms 41692 KB Output is correct
67 Correct 170 ms 46752 KB Output is correct
68 Correct 184 ms 50644 KB Output is correct
69 Correct 194 ms 44512 KB Output is correct
70 Correct 349 ms 50360 KB Output is correct
71 Correct 154 ms 50756 KB Output is correct
72 Correct 111 ms 44580 KB Output is correct
73 Correct 123 ms 44496 KB Output is correct
74 Correct 156 ms 44100 KB Output is correct
75 Correct 178 ms 50640 KB Output is correct
76 Correct 144 ms 43532 KB Output is correct
77 Correct 154 ms 47060 KB Output is correct
78 Correct 157 ms 50868 KB Output is correct
79 Correct 120 ms 39376 KB Output is correct
80 Correct 147 ms 43340 KB Output is correct
81 Correct 172 ms 47060 KB Output is correct
82 Correct 169 ms 50896 KB Output is correct
83 Correct 177 ms 41936 KB Output is correct
84 Correct 339 ms 48308 KB Output is correct
85 Correct 101 ms 47316 KB Output is correct
86 Correct 93 ms 41936 KB Output is correct
87 Correct 137 ms 41852 KB Output is correct
88 Correct 170 ms 45776 KB Output is correct
89 Correct 139 ms 41940 KB Output is correct
90 Correct 126 ms 47052 KB Output is correct
91 Correct 106 ms 38088 KB Output is correct
92 Correct 154 ms 43268 KB Output is correct
93 Correct 181 ms 47036 KB Output is correct
94 Correct 202 ms 45136 KB Output is correct
95 Correct 226 ms 51416 KB Output is correct
96 Correct 118 ms 50392 KB Output is correct
97 Correct 113 ms 45264 KB Output is correct
98 Correct 124 ms 45084 KB Output is correct
99 Correct 150 ms 42672 KB Output is correct
100 Correct 155 ms 51668 KB Output is correct
101 Correct 146 ms 43984 KB Output is correct
102 Correct 185 ms 47828 KB Output is correct
103 Correct 177 ms 51612 KB Output is correct
104 Correct 124 ms 41292 KB Output is correct
105 Correct 172 ms 46612 KB Output is correct
106 Correct 201 ms 50388 KB Output is correct
107 Correct 102 ms 51756 KB Output is correct
108 Correct 207 ms 44884 KB Output is correct
109 Correct 197 ms 44884 KB Output is correct
110 Correct 204 ms 44884 KB Output is correct
111 Correct 196 ms 44880 KB Output is correct
112 Correct 199 ms 44920 KB Output is correct
113 Correct 196 ms 44996 KB Output is correct
114 Correct 196 ms 45032 KB Output is correct
115 Correct 204 ms 44880 KB Output is correct
116 Correct 195 ms 44880 KB Output is correct