#include <bits/stdc++.h>
using namespace std;
vector<int> g[1 << 19];
vector<int> rg[1 << 19];
int st[1 << 19];
int dep[1 << 19];
int ddep[1 << 19];
int t;
int ett[1048576];
int rev[1048576];
void dfs(int u, int p = 0, int d = 0){
t++;
ddep[u] = d;
ett[t] = dep[u] = dep[p]+1;
st[u] = t;
rev[t] = u;
for(int v : g[u]){
if(v == p) continue;
dfs(v, u, d+1);
t++;
ett[t] = dep[u];
rev[t] = u;
}
for(int v : rg[u]){
if(v == p) continue;
dfs(v, u, d-1);
t++;
ett[t] = dep[u];
rev[t] = u;
}
}
pair<int, int> sparse[21][1048576];
void build(){
for(int i = 0; i < 1048576; i++) sparse[0][i] = {ett[i], rev[i]};
for(int i = 1; i < 21; i++){
for(int j = 0; j + (1 << i) < 1048576; j++){
sparse[i][j] = min(sparse[i-1][j], sparse[i-1][j+(1<<(i-1))]);
}
}
}
inline bool search(const int& a, const int& d){
int l, r;
tie(l, r) = minmax(st[a], st[d]);
int lg = 31 - __builtin_clz(r-l+1);
return dep[a] + dep[d] - 2*dep[min(sparse[lg][l], sparse[lg][r-(1<<lg)+1]).second] == ddep[d] - ddep[a];
}
tuple<int,int,int> buffer[1 << 19];
int bsz;
const int THRESH = 1100;
int qs[1 << 19];
int evp[1 << 19];
int update(int u, int p = 0, int d = 0){
for(int v : rg[u]){
if(v == p) continue;
d += update(v, u, 0);
}
d += evp[u];
qs[u] += d;
for(int v : g[u]){
if(v == p) continue;
update(v, u, d);
}
return d;
}
int count(int d){
if(bsz >= THRESH){
for(int i = 0; i < bsz; i++){
evp[get<0>(buffer[i])]--;
evp[get<1>(buffer[i])]--;
evp[get<2>(buffer[i])] += 2;
}
update(1);
for(int i = 0; i < bsz; i++){
evp[get<0>(buffer[i])]++;
evp[get<1>(buffer[i])]++;
evp[get<2>(buffer[i])] -= 2;
}
bsz = 0;
}
int ret = qs[d];
for(int i = 0; i < bsz; i++){
if(search(get<0>(buffer[i]), d) || search(get<1>(buffer[i]), d)){
ret++;
}else if(search(get<2>(buffer[i]), d)){
ret += 2;
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
k += n-1;
vector<tuple<int,int,int>> cmds;
vector<int> ids(n+1);
for(int i = 1; i <= n; i++){
ids[i] = i;
}
int last = n;
for(int i = 0; i < k; i++){
string cmd;
cin >> cmd;
int a, b, d;
switch(cmd[0]){
case 'S':
cin >> a >> b;
last++;
g[last].push_back(ids[a]);
rg[ids[a]].push_back(last);
g[last].push_back(ids[b]);
rg[ids[b]].push_back(last);
ids[a] = ids[b] = last;
cmds.push_back({0, a, b});
break;
case 'Q':
cin >> a >> d;
cmds.push_back({1, a, d});
break;
case 'C':
cin >> d;
cmds.push_back({2, d, 0});
break;
}
}
dfs(1);
build();
for(int i = 1; i <= n; i++){
ids[i] = i;
qs[i]++;
}
last = n;
for(int i = 0; i < k; i++){
int a, b, d;
switch(get<0>(cmds[i])){
case 0:
a = get<1>(cmds[i]);
b = get<2>(cmds[i]);
last++;
buffer[bsz++] = {ids[a], ids[b], last};
ids[a] = ids[b] = last;
break;
case 1:
a = get<1>(cmds[i]);
d = get<2>(cmds[i]);
if(search(ids[a], d)) cout << "yes\n";
else cout << "no\n";
break;
case 2:
d = get<1>(cmds[i]);
cout << count(d) << "\n";
break;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
199084 KB |
Output is correct |
2 |
Correct |
88 ms |
199548 KB |
Output is correct |
3 |
Correct |
66 ms |
199632 KB |
Output is correct |
4 |
Correct |
64 ms |
199620 KB |
Output is correct |
5 |
Correct |
81 ms |
199984 KB |
Output is correct |
6 |
Correct |
64 ms |
199888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
199084 KB |
Output is correct |
2 |
Correct |
88 ms |
199548 KB |
Output is correct |
3 |
Correct |
66 ms |
199632 KB |
Output is correct |
4 |
Correct |
64 ms |
199620 KB |
Output is correct |
5 |
Correct |
81 ms |
199984 KB |
Output is correct |
6 |
Correct |
64 ms |
199888 KB |
Output is correct |
7 |
Correct |
70 ms |
199072 KB |
Output is correct |
8 |
Correct |
757 ms |
199648 KB |
Output is correct |
9 |
Correct |
512 ms |
199684 KB |
Output is correct |
10 |
Correct |
689 ms |
199644 KB |
Output is correct |
11 |
Correct |
731 ms |
199820 KB |
Output is correct |
12 |
Correct |
312 ms |
199936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
199108 KB |
Output is correct |
2 |
Correct |
130 ms |
232832 KB |
Output is correct |
3 |
Correct |
118 ms |
232700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
199108 KB |
Output is correct |
2 |
Correct |
130 ms |
232832 KB |
Output is correct |
3 |
Correct |
118 ms |
232700 KB |
Output is correct |
4 |
Correct |
58 ms |
199108 KB |
Output is correct |
5 |
Correct |
779 ms |
230884 KB |
Output is correct |
6 |
Correct |
1042 ms |
231120 KB |
Output is correct |
7 |
Correct |
1312 ms |
230888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
199156 KB |
Output is correct |
2 |
Correct |
151 ms |
231684 KB |
Output is correct |
3 |
Correct |
144 ms |
231608 KB |
Output is correct |
4 |
Correct |
106 ms |
232644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
199156 KB |
Output is correct |
2 |
Correct |
151 ms |
231684 KB |
Output is correct |
3 |
Correct |
144 ms |
231608 KB |
Output is correct |
4 |
Correct |
106 ms |
232644 KB |
Output is correct |
5 |
Correct |
80 ms |
199052 KB |
Output is correct |
6 |
Correct |
3142 ms |
229488 KB |
Output is correct |
7 |
Correct |
667 ms |
230776 KB |
Output is correct |
8 |
Correct |
3324 ms |
229424 KB |
Output is correct |
9 |
Correct |
3321 ms |
229408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
199108 KB |
Output is correct |
2 |
Correct |
111 ms |
220928 KB |
Output is correct |
3 |
Correct |
124 ms |
220416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
199108 KB |
Output is correct |
2 |
Correct |
111 ms |
220928 KB |
Output is correct |
3 |
Correct |
124 ms |
220416 KB |
Output is correct |
4 |
Correct |
57 ms |
199108 KB |
Output is correct |
5 |
Correct |
833 ms |
219104 KB |
Output is correct |
6 |
Correct |
1655 ms |
220680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
199112 KB |
Output is correct |
2 |
Correct |
140 ms |
231472 KB |
Output is correct |
3 |
Correct |
132 ms |
231428 KB |
Output is correct |
4 |
Correct |
104 ms |
232756 KB |
Output is correct |
5 |
Correct |
50 ms |
199092 KB |
Output is correct |
6 |
Correct |
103 ms |
220928 KB |
Output is correct |
7 |
Correct |
127 ms |
220568 KB |
Output is correct |
8 |
Correct |
130 ms |
220924 KB |
Output is correct |
9 |
Correct |
130 ms |
220924 KB |
Output is correct |
10 |
Correct |
117 ms |
225532 KB |
Output is correct |
11 |
Correct |
114 ms |
224768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
199112 KB |
Output is correct |
2 |
Correct |
140 ms |
231472 KB |
Output is correct |
3 |
Correct |
132 ms |
231428 KB |
Output is correct |
4 |
Correct |
104 ms |
232756 KB |
Output is correct |
5 |
Correct |
50 ms |
199092 KB |
Output is correct |
6 |
Correct |
103 ms |
220928 KB |
Output is correct |
7 |
Correct |
127 ms |
220568 KB |
Output is correct |
8 |
Correct |
130 ms |
220924 KB |
Output is correct |
9 |
Correct |
130 ms |
220924 KB |
Output is correct |
10 |
Correct |
117 ms |
225532 KB |
Output is correct |
11 |
Correct |
114 ms |
224768 KB |
Output is correct |
12 |
Correct |
53 ms |
199160 KB |
Output is correct |
13 |
Correct |
3046 ms |
229544 KB |
Output is correct |
14 |
Correct |
695 ms |
231016 KB |
Output is correct |
15 |
Execution timed out |
3588 ms |
229468 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
199108 KB |
Output is correct |
2 |
Correct |
61 ms |
199380 KB |
Output is correct |
3 |
Correct |
71 ms |
199684 KB |
Output is correct |
4 |
Correct |
61 ms |
199636 KB |
Output is correct |
5 |
Correct |
67 ms |
199852 KB |
Output is correct |
6 |
Correct |
64 ms |
199812 KB |
Output is correct |
7 |
Correct |
59 ms |
199068 KB |
Output is correct |
8 |
Correct |
120 ms |
232748 KB |
Output is correct |
9 |
Correct |
123 ms |
232716 KB |
Output is correct |
10 |
Correct |
58 ms |
199116 KB |
Output is correct |
11 |
Correct |
154 ms |
231424 KB |
Output is correct |
12 |
Correct |
157 ms |
231632 KB |
Output is correct |
13 |
Correct |
116 ms |
232832 KB |
Output is correct |
14 |
Correct |
62 ms |
199212 KB |
Output is correct |
15 |
Correct |
120 ms |
221148 KB |
Output is correct |
16 |
Correct |
134 ms |
220624 KB |
Output is correct |
17 |
Correct |
155 ms |
220936 KB |
Output is correct |
18 |
Correct |
153 ms |
220932 KB |
Output is correct |
19 |
Correct |
135 ms |
225540 KB |
Output is correct |
20 |
Correct |
146 ms |
224936 KB |
Output is correct |
21 |
Correct |
131 ms |
225364 KB |
Output is correct |
22 |
Correct |
144 ms |
225560 KB |
Output is correct |
23 |
Correct |
161 ms |
221864 KB |
Output is correct |
24 |
Correct |
177 ms |
222000 KB |
Output is correct |
25 |
Correct |
143 ms |
227412 KB |
Output is correct |
26 |
Correct |
142 ms |
221184 KB |
Output is correct |
27 |
Correct |
138 ms |
221592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
199108 KB |
Output is correct |
2 |
Correct |
61 ms |
199380 KB |
Output is correct |
3 |
Correct |
71 ms |
199684 KB |
Output is correct |
4 |
Correct |
61 ms |
199636 KB |
Output is correct |
5 |
Correct |
67 ms |
199852 KB |
Output is correct |
6 |
Correct |
64 ms |
199812 KB |
Output is correct |
7 |
Correct |
59 ms |
199068 KB |
Output is correct |
8 |
Correct |
120 ms |
232748 KB |
Output is correct |
9 |
Correct |
123 ms |
232716 KB |
Output is correct |
10 |
Correct |
58 ms |
199116 KB |
Output is correct |
11 |
Correct |
154 ms |
231424 KB |
Output is correct |
12 |
Correct |
157 ms |
231632 KB |
Output is correct |
13 |
Correct |
116 ms |
232832 KB |
Output is correct |
14 |
Correct |
62 ms |
199212 KB |
Output is correct |
15 |
Correct |
120 ms |
221148 KB |
Output is correct |
16 |
Correct |
134 ms |
220624 KB |
Output is correct |
17 |
Correct |
155 ms |
220936 KB |
Output is correct |
18 |
Correct |
153 ms |
220932 KB |
Output is correct |
19 |
Correct |
135 ms |
225540 KB |
Output is correct |
20 |
Correct |
146 ms |
224936 KB |
Output is correct |
21 |
Correct |
131 ms |
225364 KB |
Output is correct |
22 |
Correct |
144 ms |
225560 KB |
Output is correct |
23 |
Correct |
161 ms |
221864 KB |
Output is correct |
24 |
Correct |
177 ms |
222000 KB |
Output is correct |
25 |
Correct |
143 ms |
227412 KB |
Output is correct |
26 |
Correct |
142 ms |
221184 KB |
Output is correct |
27 |
Correct |
138 ms |
221592 KB |
Output is correct |
28 |
Correct |
59 ms |
199200 KB |
Output is correct |
29 |
Correct |
763 ms |
199452 KB |
Output is correct |
30 |
Correct |
458 ms |
199636 KB |
Output is correct |
31 |
Correct |
723 ms |
199648 KB |
Output is correct |
32 |
Correct |
741 ms |
199824 KB |
Output is correct |
33 |
Correct |
343 ms |
199992 KB |
Output is correct |
34 |
Correct |
60 ms |
199096 KB |
Output is correct |
35 |
Correct |
908 ms |
230824 KB |
Output is correct |
36 |
Correct |
1110 ms |
231176 KB |
Output is correct |
37 |
Correct |
1245 ms |
231052 KB |
Output is correct |
38 |
Correct |
57 ms |
199136 KB |
Output is correct |
39 |
Correct |
3408 ms |
229608 KB |
Output is correct |
40 |
Correct |
684 ms |
230864 KB |
Output is correct |
41 |
Execution timed out |
3589 ms |
229512 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |