#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[1000005];
int rev[1000005];
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 < 1000005; 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 pair<int,int> rmq(const int& l, const int& r){
int lg = 31 - __builtin_clz(r-l+1);
return min(sparse[lg][l], sparse[lg][r-(1<<lg)+1]);
}
inline int lca(const int& u, const int& v){
if(st[u] > st[v]) return rmq(st[v], st[u]).second;
else return rmq(st[u], st[v]).second;
}
inline bool search(const int& a, const int& d){
int l = lca(a, d);
int dist = dep[a] + dep[d] - 2*dep[l];
int ddist = ddep[d] - ddep[a];
return dist == ddist;
}
tuple<int,int,int> buffer[1 << 19];
int bsz;
const int THRESH = 950;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
198596 KB |
Output is correct |
2 |
Correct |
57 ms |
199024 KB |
Output is correct |
3 |
Correct |
59 ms |
199276 KB |
Output is correct |
4 |
Correct |
58 ms |
199132 KB |
Output is correct |
5 |
Correct |
58 ms |
199428 KB |
Output is correct |
6 |
Correct |
58 ms |
199680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
198596 KB |
Output is correct |
2 |
Correct |
57 ms |
199024 KB |
Output is correct |
3 |
Correct |
59 ms |
199276 KB |
Output is correct |
4 |
Correct |
58 ms |
199132 KB |
Output is correct |
5 |
Correct |
58 ms |
199428 KB |
Output is correct |
6 |
Correct |
58 ms |
199680 KB |
Output is correct |
7 |
Correct |
55 ms |
198600 KB |
Output is correct |
8 |
Correct |
656 ms |
199120 KB |
Output is correct |
9 |
Correct |
390 ms |
199176 KB |
Output is correct |
10 |
Correct |
621 ms |
199204 KB |
Output is correct |
11 |
Correct |
307 ms |
199768 KB |
Output is correct |
12 |
Correct |
183 ms |
199684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
198596 KB |
Output is correct |
2 |
Correct |
146 ms |
232704 KB |
Output is correct |
3 |
Correct |
112 ms |
232704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
198596 KB |
Output is correct |
2 |
Correct |
146 ms |
232704 KB |
Output is correct |
3 |
Correct |
112 ms |
232704 KB |
Output is correct |
4 |
Correct |
55 ms |
198600 KB |
Output is correct |
5 |
Correct |
719 ms |
230908 KB |
Output is correct |
6 |
Correct |
963 ms |
231188 KB |
Output is correct |
7 |
Correct |
1152 ms |
231088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
198604 KB |
Output is correct |
2 |
Correct |
153 ms |
231564 KB |
Output is correct |
3 |
Correct |
141 ms |
231684 KB |
Output is correct |
4 |
Correct |
106 ms |
232708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
198604 KB |
Output is correct |
2 |
Correct |
153 ms |
231564 KB |
Output is correct |
3 |
Correct |
141 ms |
231684 KB |
Output is correct |
4 |
Correct |
106 ms |
232708 KB |
Output is correct |
5 |
Correct |
59 ms |
198600 KB |
Output is correct |
6 |
Correct |
2646 ms |
229580 KB |
Output is correct |
7 |
Correct |
637 ms |
230912 KB |
Output is correct |
8 |
Correct |
3399 ms |
229376 KB |
Output is correct |
9 |
Correct |
3352 ms |
229448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
198600 KB |
Output is correct |
2 |
Correct |
104 ms |
220928 KB |
Output is correct |
3 |
Correct |
135 ms |
220560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
198600 KB |
Output is correct |
2 |
Correct |
104 ms |
220928 KB |
Output is correct |
3 |
Correct |
135 ms |
220560 KB |
Output is correct |
4 |
Correct |
56 ms |
198596 KB |
Output is correct |
5 |
Correct |
676 ms |
219276 KB |
Output is correct |
6 |
Correct |
1585 ms |
220680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
198596 KB |
Output is correct |
2 |
Correct |
141 ms |
231428 KB |
Output is correct |
3 |
Correct |
143 ms |
231836 KB |
Output is correct |
4 |
Correct |
106 ms |
232704 KB |
Output is correct |
5 |
Correct |
54 ms |
198600 KB |
Output is correct |
6 |
Correct |
101 ms |
220928 KB |
Output is correct |
7 |
Correct |
129 ms |
220412 KB |
Output is correct |
8 |
Correct |
132 ms |
220952 KB |
Output is correct |
9 |
Correct |
128 ms |
221184 KB |
Output is correct |
10 |
Correct |
120 ms |
225536 KB |
Output is correct |
11 |
Correct |
120 ms |
224768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
198596 KB |
Output is correct |
2 |
Correct |
141 ms |
231428 KB |
Output is correct |
3 |
Correct |
143 ms |
231836 KB |
Output is correct |
4 |
Correct |
106 ms |
232704 KB |
Output is correct |
5 |
Correct |
54 ms |
198600 KB |
Output is correct |
6 |
Correct |
101 ms |
220928 KB |
Output is correct |
7 |
Correct |
129 ms |
220412 KB |
Output is correct |
8 |
Correct |
132 ms |
220952 KB |
Output is correct |
9 |
Correct |
128 ms |
221184 KB |
Output is correct |
10 |
Correct |
120 ms |
225536 KB |
Output is correct |
11 |
Correct |
120 ms |
224768 KB |
Output is correct |
12 |
Correct |
54 ms |
198596 KB |
Output is correct |
13 |
Correct |
2686 ms |
229740 KB |
Output is correct |
14 |
Correct |
641 ms |
231172 KB |
Output is correct |
15 |
Correct |
3409 ms |
229416 KB |
Output is correct |
16 |
Correct |
3334 ms |
229376 KB |
Output is correct |
17 |
Correct |
56 ms |
198656 KB |
Output is correct |
18 |
Correct |
668 ms |
219212 KB |
Output is correct |
19 |
Correct |
1652 ms |
220648 KB |
Output is correct |
20 |
Correct |
2840 ms |
218984 KB |
Output is correct |
21 |
Correct |
2463 ms |
218940 KB |
Output is correct |
22 |
Correct |
1904 ms |
222736 KB |
Output is correct |
23 |
Correct |
1246 ms |
224820 KB |
Output is correct |
24 |
Correct |
507 ms |
225536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
198596 KB |
Output is correct |
2 |
Correct |
59 ms |
199132 KB |
Output is correct |
3 |
Correct |
57 ms |
199204 KB |
Output is correct |
4 |
Correct |
57 ms |
199132 KB |
Output is correct |
5 |
Correct |
60 ms |
199600 KB |
Output is correct |
6 |
Correct |
59 ms |
199508 KB |
Output is correct |
7 |
Correct |
59 ms |
198604 KB |
Output is correct |
8 |
Correct |
111 ms |
232700 KB |
Output is correct |
9 |
Correct |
109 ms |
232816 KB |
Output is correct |
10 |
Correct |
50 ms |
198596 KB |
Output is correct |
11 |
Correct |
141 ms |
231684 KB |
Output is correct |
12 |
Correct |
137 ms |
231684 KB |
Output is correct |
13 |
Correct |
104 ms |
232680 KB |
Output is correct |
14 |
Correct |
53 ms |
198664 KB |
Output is correct |
15 |
Correct |
114 ms |
220932 KB |
Output is correct |
16 |
Correct |
130 ms |
220412 KB |
Output is correct |
17 |
Correct |
130 ms |
220932 KB |
Output is correct |
18 |
Correct |
132 ms |
220816 KB |
Output is correct |
19 |
Correct |
122 ms |
225796 KB |
Output is correct |
20 |
Correct |
126 ms |
224936 KB |
Output is correct |
21 |
Correct |
112 ms |
225276 KB |
Output is correct |
22 |
Correct |
119 ms |
225792 KB |
Output is correct |
23 |
Correct |
138 ms |
221932 KB |
Output is correct |
24 |
Correct |
155 ms |
222208 KB |
Output is correct |
25 |
Correct |
122 ms |
227588 KB |
Output is correct |
26 |
Correct |
125 ms |
221188 KB |
Output is correct |
27 |
Correct |
138 ms |
221428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
198596 KB |
Output is correct |
2 |
Correct |
59 ms |
199132 KB |
Output is correct |
3 |
Correct |
57 ms |
199204 KB |
Output is correct |
4 |
Correct |
57 ms |
199132 KB |
Output is correct |
5 |
Correct |
60 ms |
199600 KB |
Output is correct |
6 |
Correct |
59 ms |
199508 KB |
Output is correct |
7 |
Correct |
59 ms |
198604 KB |
Output is correct |
8 |
Correct |
111 ms |
232700 KB |
Output is correct |
9 |
Correct |
109 ms |
232816 KB |
Output is correct |
10 |
Correct |
50 ms |
198596 KB |
Output is correct |
11 |
Correct |
141 ms |
231684 KB |
Output is correct |
12 |
Correct |
137 ms |
231684 KB |
Output is correct |
13 |
Correct |
104 ms |
232680 KB |
Output is correct |
14 |
Correct |
53 ms |
198664 KB |
Output is correct |
15 |
Correct |
114 ms |
220932 KB |
Output is correct |
16 |
Correct |
130 ms |
220412 KB |
Output is correct |
17 |
Correct |
130 ms |
220932 KB |
Output is correct |
18 |
Correct |
132 ms |
220816 KB |
Output is correct |
19 |
Correct |
122 ms |
225796 KB |
Output is correct |
20 |
Correct |
126 ms |
224936 KB |
Output is correct |
21 |
Correct |
112 ms |
225276 KB |
Output is correct |
22 |
Correct |
119 ms |
225792 KB |
Output is correct |
23 |
Correct |
138 ms |
221932 KB |
Output is correct |
24 |
Correct |
155 ms |
222208 KB |
Output is correct |
25 |
Correct |
122 ms |
227588 KB |
Output is correct |
26 |
Correct |
125 ms |
221188 KB |
Output is correct |
27 |
Correct |
138 ms |
221428 KB |
Output is correct |
28 |
Correct |
59 ms |
198596 KB |
Output is correct |
29 |
Correct |
679 ms |
199092 KB |
Output is correct |
30 |
Correct |
445 ms |
199180 KB |
Output is correct |
31 |
Correct |
653 ms |
199136 KB |
Output is correct |
32 |
Correct |
316 ms |
199588 KB |
Output is correct |
33 |
Correct |
202 ms |
199664 KB |
Output is correct |
34 |
Correct |
68 ms |
198996 KB |
Output is correct |
35 |
Correct |
845 ms |
230784 KB |
Output is correct |
36 |
Correct |
1109 ms |
231184 KB |
Output is correct |
37 |
Correct |
1361 ms |
231020 KB |
Output is correct |
38 |
Correct |
63 ms |
198604 KB |
Output is correct |
39 |
Correct |
3407 ms |
229604 KB |
Output is correct |
40 |
Correct |
771 ms |
231024 KB |
Output is correct |
41 |
Execution timed out |
3590 ms |
229376 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |