#include<bits/stdc++.h>
#define int ll
#define lc (c[rt].l)
#define rc (c[rt].r)
#define mid ((l+r)>>1)
#define rg register
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define prq priority_queue
#define fi first
#define se second
#define DEBUG printf("Debug on line %d\n", __LINE__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 2.4e5 + 5;
const int INF = 0x3f3f3f3f;
inline int read(){
int n=0, f=1; char ch = getchar();
for(; !isdigit(ch); ch=getchar()) if(ch == '-') f=-1;
for(; isdigit(ch); ch=getchar()) n=n*10+ch-'0';
return n*f;
}
int n, q;
int rt[N];
char opt[N]; int a[N], b[N], ans[N];
namespace dgb_SEG{
int tp = 0;
struct node{
int s;
int l, r;
}c[N*300];
void clear(){
for(int i=1; i<=tp; i++){
c[i].l = c[i].r = c[i].s = 0;
}
tp = 0;
}
int newnode(){
++tp;
c[tp].l = c[tp].r = c[tp].s = 0;
return tp;
}
int clone(int x){
c[++tp] = c[x];
return tp;
}
void pushup(int rt){
c[rt].s = c[lc].s + c[rc].s;
}
int merge(int x, int y, int l, int r){
if(!x || !y) return x|y;
int rt = newnode();
if(l == r){
c[rt].s = c[x].s + c[y].s;
return rt;
}
lc = merge(c[x].l, c[y].l, l, mid);
rc = merge(c[x].r, c[y].r, mid+1, r);
pushup(rt);
return rt;
}
int modify(int rt, int l, int r, int x, int v){
rt = clone(rt);
if(l == r){
c[rt].s += v;
return rt;
}
if(x <= mid) lc = modify(lc, l, mid, x, v);
else rc = modify(rc, mid+1, r, x, v);
pushup(rt);
return rt;
}
int multiQuery(int rt, int l, int r, int ql, int qr){
if(!rt || ql>qr || l>qr || r<ql) return 0;
if(l>=ql && r<=qr) return c[rt].s;
return multiQuery(lc, l, mid, ql, qr) + multiQuery(rc, mid+1, r, ql, qr);
}
int singleQuery(int rt, int l, int r, int x){
if(l == r) return c[rt].s;
if(x <= mid) return singleQuery(lc, l, mid, x);
else return singleQuery(rc, mid+1, r, x);
}
}
/*
6 4
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
Q 5 1
Q 1 5
C 1
*/
signed main(){
// printf("%lld\n", N);
n = read(), q = read();
q += n-1;
for(int i=1; i<=n; i++){
rt[i] = dgb_SEG::modify(rt[0], 1, n, i, 1);
}
for(int i=1; i<=q; i++){
cin >> opt[i];
if(opt[i] == 'S'){
a[i] = read(), b[i] = read();
rt[a[i]] = rt[b[i]] = dgb_SEG::merge(rt[a[i]], rt[b[i]], 1, n);
} else if(opt[i] == 'Q'){
a[i] = read(), b[i] = read();
ans[i] = dgb_SEG::singleQuery(rt[a[i]], 1, n, b[i]);
} else {
a[i] = read();
}
}
dgb_SEG::clear();
for(int i=1; i<=n; i++) {
rt[i] = ++dgb_SEG::tp;
}
for(int i=q; i>=1; i--){
if(opt[i] == 'S'){
rt[a[i]] = dgb_SEG::modify(rt[a[i]], 1, q, i, 1);
rt[a[i]] = rt[b[i]] = dgb_SEG::merge(rt[a[i]], rt[b[i]], 1, q);
}
}
// for(int i=1; i<=q; i++){
// printf("%lld\n", dgb_SEG::multiQuery(rt[i], 1, q, 1, q)+1);
// }
for(int i=1; i<=q; i++){
// printf("%d: ", i);
if(opt[i] == 'Q'){
printf(ans[i] ? "yes\n" : "no\n");
} else if(opt[i] == 'C'){
printf("%lld\n", dgb_SEG::multiQuery(rt[a[i]], 1, q, 1, i)+1);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4444 KB |
Output is correct |
2 |
Correct |
21 ms |
7216 KB |
Output is correct |
3 |
Correct |
24 ms |
7672 KB |
Output is correct |
4 |
Correct |
21 ms |
7248 KB |
Output is correct |
5 |
Correct |
25 ms |
7484 KB |
Output is correct |
6 |
Correct |
31 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4444 KB |
Output is correct |
2 |
Correct |
21 ms |
7216 KB |
Output is correct |
3 |
Correct |
24 ms |
7672 KB |
Output is correct |
4 |
Correct |
21 ms |
7248 KB |
Output is correct |
5 |
Correct |
25 ms |
7484 KB |
Output is correct |
6 |
Correct |
31 ms |
7516 KB |
Output is correct |
7 |
Correct |
11 ms |
4440 KB |
Output is correct |
8 |
Correct |
25 ms |
6936 KB |
Output is correct |
9 |
Correct |
26 ms |
7248 KB |
Output is correct |
10 |
Correct |
26 ms |
6756 KB |
Output is correct |
11 |
Correct |
23 ms |
7148 KB |
Output is correct |
12 |
Correct |
26 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4696 KB |
Output is correct |
2 |
Correct |
278 ms |
105552 KB |
Output is correct |
3 |
Correct |
262 ms |
105684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4696 KB |
Output is correct |
2 |
Correct |
278 ms |
105552 KB |
Output is correct |
3 |
Correct |
262 ms |
105684 KB |
Output is correct |
4 |
Correct |
11 ms |
4444 KB |
Output is correct |
5 |
Correct |
275 ms |
105524 KB |
Output is correct |
6 |
Correct |
212 ms |
102996 KB |
Output is correct |
7 |
Correct |
222 ms |
104688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4440 KB |
Output is correct |
2 |
Correct |
226 ms |
106064 KB |
Output is correct |
3 |
Correct |
217 ms |
106096 KB |
Output is correct |
4 |
Correct |
157 ms |
106044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4440 KB |
Output is correct |
2 |
Correct |
226 ms |
106064 KB |
Output is correct |
3 |
Correct |
217 ms |
106096 KB |
Output is correct |
4 |
Correct |
157 ms |
106044 KB |
Output is correct |
5 |
Correct |
11 ms |
4440 KB |
Output is correct |
6 |
Correct |
225 ms |
105812 KB |
Output is correct |
7 |
Correct |
171 ms |
105808 KB |
Output is correct |
8 |
Correct |
230 ms |
105400 KB |
Output is correct |
9 |
Correct |
226 ms |
105300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4696 KB |
Output is correct |
2 |
Correct |
196 ms |
99156 KB |
Output is correct |
3 |
Correct |
261 ms |
91960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4696 KB |
Output is correct |
2 |
Correct |
196 ms |
99156 KB |
Output is correct |
3 |
Correct |
261 ms |
91960 KB |
Output is correct |
4 |
Correct |
15 ms |
4444 KB |
Output is correct |
5 |
Correct |
210 ms |
98832 KB |
Output is correct |
6 |
Correct |
254 ms |
91344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
269 ms |
106068 KB |
Output is correct |
3 |
Correct |
256 ms |
106060 KB |
Output is correct |
4 |
Correct |
156 ms |
106008 KB |
Output is correct |
5 |
Correct |
13 ms |
4696 KB |
Output is correct |
6 |
Correct |
179 ms |
99152 KB |
Output is correct |
7 |
Correct |
250 ms |
91912 KB |
Output is correct |
8 |
Correct |
199 ms |
76692 KB |
Output is correct |
9 |
Correct |
209 ms |
75396 KB |
Output is correct |
10 |
Correct |
253 ms |
106324 KB |
Output is correct |
11 |
Correct |
268 ms |
106068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
269 ms |
106068 KB |
Output is correct |
3 |
Correct |
256 ms |
106060 KB |
Output is correct |
4 |
Correct |
156 ms |
106008 KB |
Output is correct |
5 |
Correct |
13 ms |
4696 KB |
Output is correct |
6 |
Correct |
179 ms |
99152 KB |
Output is correct |
7 |
Correct |
250 ms |
91912 KB |
Output is correct |
8 |
Correct |
199 ms |
76692 KB |
Output is correct |
9 |
Correct |
209 ms |
75396 KB |
Output is correct |
10 |
Correct |
253 ms |
106324 KB |
Output is correct |
11 |
Correct |
268 ms |
106068 KB |
Output is correct |
12 |
Correct |
12 ms |
4444 KB |
Output is correct |
13 |
Correct |
262 ms |
105812 KB |
Output is correct |
14 |
Correct |
175 ms |
105820 KB |
Output is correct |
15 |
Correct |
222 ms |
105300 KB |
Output is correct |
16 |
Correct |
237 ms |
105400 KB |
Output is correct |
17 |
Correct |
12 ms |
4444 KB |
Output is correct |
18 |
Correct |
181 ms |
98988 KB |
Output is correct |
19 |
Correct |
263 ms |
91344 KB |
Output is correct |
20 |
Correct |
221 ms |
76104 KB |
Output is correct |
21 |
Correct |
222 ms |
75152 KB |
Output is correct |
22 |
Correct |
242 ms |
105552 KB |
Output is correct |
23 |
Correct |
307 ms |
102420 KB |
Output is correct |
24 |
Correct |
305 ms |
103968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
21 ms |
7260 KB |
Output is correct |
3 |
Correct |
23 ms |
7516 KB |
Output is correct |
4 |
Correct |
21 ms |
7252 KB |
Output is correct |
5 |
Correct |
21 ms |
7524 KB |
Output is correct |
6 |
Correct |
28 ms |
7520 KB |
Output is correct |
7 |
Correct |
14 ms |
4608 KB |
Output is correct |
8 |
Correct |
266 ms |
105568 KB |
Output is correct |
9 |
Correct |
264 ms |
105644 KB |
Output is correct |
10 |
Correct |
16 ms |
4444 KB |
Output is correct |
11 |
Correct |
216 ms |
106080 KB |
Output is correct |
12 |
Correct |
204 ms |
106068 KB |
Output is correct |
13 |
Correct |
157 ms |
106064 KB |
Output is correct |
14 |
Correct |
11 ms |
4444 KB |
Output is correct |
15 |
Correct |
199 ms |
99156 KB |
Output is correct |
16 |
Correct |
240 ms |
91988 KB |
Output is correct |
17 |
Correct |
193 ms |
76624 KB |
Output is correct |
18 |
Correct |
197 ms |
75580 KB |
Output is correct |
19 |
Correct |
253 ms |
106324 KB |
Output is correct |
20 |
Correct |
284 ms |
106068 KB |
Output is correct |
21 |
Correct |
302 ms |
105812 KB |
Output is correct |
22 |
Correct |
331 ms |
106068 KB |
Output is correct |
23 |
Correct |
258 ms |
90932 KB |
Output is correct |
24 |
Correct |
264 ms |
90784 KB |
Output is correct |
25 |
Correct |
223 ms |
87064 KB |
Output is correct |
26 |
Correct |
254 ms |
101048 KB |
Output is correct |
27 |
Correct |
247 ms |
105044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
21 ms |
7260 KB |
Output is correct |
3 |
Correct |
23 ms |
7516 KB |
Output is correct |
4 |
Correct |
21 ms |
7252 KB |
Output is correct |
5 |
Correct |
21 ms |
7524 KB |
Output is correct |
6 |
Correct |
28 ms |
7520 KB |
Output is correct |
7 |
Correct |
14 ms |
4608 KB |
Output is correct |
8 |
Correct |
266 ms |
105568 KB |
Output is correct |
9 |
Correct |
264 ms |
105644 KB |
Output is correct |
10 |
Correct |
16 ms |
4444 KB |
Output is correct |
11 |
Correct |
216 ms |
106080 KB |
Output is correct |
12 |
Correct |
204 ms |
106068 KB |
Output is correct |
13 |
Correct |
157 ms |
106064 KB |
Output is correct |
14 |
Correct |
11 ms |
4444 KB |
Output is correct |
15 |
Correct |
199 ms |
99156 KB |
Output is correct |
16 |
Correct |
240 ms |
91988 KB |
Output is correct |
17 |
Correct |
193 ms |
76624 KB |
Output is correct |
18 |
Correct |
197 ms |
75580 KB |
Output is correct |
19 |
Correct |
253 ms |
106324 KB |
Output is correct |
20 |
Correct |
284 ms |
106068 KB |
Output is correct |
21 |
Correct |
302 ms |
105812 KB |
Output is correct |
22 |
Correct |
331 ms |
106068 KB |
Output is correct |
23 |
Correct |
258 ms |
90932 KB |
Output is correct |
24 |
Correct |
264 ms |
90784 KB |
Output is correct |
25 |
Correct |
223 ms |
87064 KB |
Output is correct |
26 |
Correct |
254 ms |
101048 KB |
Output is correct |
27 |
Correct |
247 ms |
105044 KB |
Output is correct |
28 |
Correct |
12 ms |
4444 KB |
Output is correct |
29 |
Correct |
27 ms |
6740 KB |
Output is correct |
30 |
Correct |
26 ms |
7384 KB |
Output is correct |
31 |
Correct |
25 ms |
6736 KB |
Output is correct |
32 |
Correct |
21 ms |
6996 KB |
Output is correct |
33 |
Correct |
37 ms |
7496 KB |
Output is correct |
34 |
Correct |
11 ms |
4700 KB |
Output is correct |
35 |
Correct |
290 ms |
105460 KB |
Output is correct |
36 |
Correct |
228 ms |
102904 KB |
Output is correct |
37 |
Correct |
217 ms |
104784 KB |
Output is correct |
38 |
Correct |
11 ms |
4444 KB |
Output is correct |
39 |
Correct |
233 ms |
105780 KB |
Output is correct |
40 |
Correct |
199 ms |
105876 KB |
Output is correct |
41 |
Correct |
230 ms |
105300 KB |
Output is correct |
42 |
Correct |
226 ms |
105296 KB |
Output is correct |
43 |
Correct |
11 ms |
4444 KB |
Output is correct |
44 |
Correct |
214 ms |
98992 KB |
Output is correct |
45 |
Correct |
237 ms |
91564 KB |
Output is correct |
46 |
Correct |
224 ms |
76152 KB |
Output is correct |
47 |
Correct |
226 ms |
75088 KB |
Output is correct |
48 |
Correct |
255 ms |
105468 KB |
Output is correct |
49 |
Correct |
306 ms |
102412 KB |
Output is correct |
50 |
Correct |
300 ms |
104144 KB |
Output is correct |
51 |
Correct |
234 ms |
104652 KB |
Output is correct |
52 |
Correct |
233 ms |
105692 KB |
Output is correct |
53 |
Correct |
218 ms |
103432 KB |
Output is correct |
54 |
Correct |
253 ms |
104016 KB |
Output is correct |
55 |
Correct |
244 ms |
103760 KB |
Output is correct |
56 |
Correct |
295 ms |
90332 KB |
Output is correct |
57 |
Correct |
223 ms |
86608 KB |
Output is correct |
58 |
Correct |
269 ms |
94028 KB |
Output is correct |
59 |
Correct |
256 ms |
104952 KB |
Output is correct |