#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
vector<int> g[500005];
vector<int> rg[500005];
int st[500005];
int dep[500005];
int ddep[500005];
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 d = r-l+1;
int lg = 31 - __builtin_clz(d);
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[500005];
int bsz;
const int THRESH = 1000;
int qs[500005];
int evp[500005];
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 |
196808 KB |
Output is correct |
2 |
Correct |
56 ms |
197084 KB |
Output is correct |
3 |
Correct |
63 ms |
197376 KB |
Output is correct |
4 |
Correct |
58 ms |
197336 KB |
Output is correct |
5 |
Correct |
56 ms |
197636 KB |
Output is correct |
6 |
Correct |
57 ms |
197632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
196808 KB |
Output is correct |
2 |
Correct |
56 ms |
197084 KB |
Output is correct |
3 |
Correct |
63 ms |
197376 KB |
Output is correct |
4 |
Correct |
58 ms |
197336 KB |
Output is correct |
5 |
Correct |
56 ms |
197636 KB |
Output is correct |
6 |
Correct |
57 ms |
197632 KB |
Output is correct |
7 |
Correct |
55 ms |
196732 KB |
Output is correct |
8 |
Correct |
717 ms |
197336 KB |
Output is correct |
9 |
Correct |
410 ms |
197464 KB |
Output is correct |
10 |
Correct |
667 ms |
197352 KB |
Output is correct |
11 |
Correct |
1083 ms |
197636 KB |
Output is correct |
12 |
Correct |
405 ms |
197636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
196804 KB |
Output is correct |
2 |
Correct |
125 ms |
231124 KB |
Output is correct |
3 |
Correct |
109 ms |
231172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
196804 KB |
Output is correct |
2 |
Correct |
125 ms |
231124 KB |
Output is correct |
3 |
Correct |
109 ms |
231172 KB |
Output is correct |
4 |
Correct |
55 ms |
196804 KB |
Output is correct |
5 |
Correct |
694 ms |
229284 KB |
Output is correct |
6 |
Correct |
992 ms |
229800 KB |
Output is correct |
7 |
Correct |
1142 ms |
229592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
196804 KB |
Output is correct |
2 |
Correct |
140 ms |
229884 KB |
Output is correct |
3 |
Correct |
145 ms |
230000 KB |
Output is correct |
4 |
Correct |
106 ms |
231168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
196804 KB |
Output is correct |
2 |
Correct |
140 ms |
229884 KB |
Output is correct |
3 |
Correct |
145 ms |
230000 KB |
Output is correct |
4 |
Correct |
106 ms |
231168 KB |
Output is correct |
5 |
Correct |
56 ms |
196804 KB |
Output is correct |
6 |
Correct |
2704 ms |
228088 KB |
Output is correct |
7 |
Correct |
657 ms |
229376 KB |
Output is correct |
8 |
Correct |
3414 ms |
227840 KB |
Output is correct |
9 |
Correct |
3401 ms |
228040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
196804 KB |
Output is correct |
2 |
Correct |
102 ms |
219392 KB |
Output is correct |
3 |
Correct |
162 ms |
218876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
196804 KB |
Output is correct |
2 |
Correct |
102 ms |
219392 KB |
Output is correct |
3 |
Correct |
162 ms |
218876 KB |
Output is correct |
4 |
Correct |
59 ms |
196800 KB |
Output is correct |
5 |
Correct |
1137 ms |
217640 KB |
Output is correct |
6 |
Correct |
2340 ms |
219136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
196900 KB |
Output is correct |
2 |
Correct |
156 ms |
230060 KB |
Output is correct |
3 |
Correct |
157 ms |
230068 KB |
Output is correct |
4 |
Correct |
111 ms |
231292 KB |
Output is correct |
5 |
Correct |
61 ms |
196836 KB |
Output is correct |
6 |
Correct |
115 ms |
219436 KB |
Output is correct |
7 |
Correct |
154 ms |
219024 KB |
Output is correct |
8 |
Correct |
151 ms |
219212 KB |
Output is correct |
9 |
Correct |
157 ms |
219300 KB |
Output is correct |
10 |
Correct |
137 ms |
224208 KB |
Output is correct |
11 |
Correct |
130 ms |
223236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
196900 KB |
Output is correct |
2 |
Correct |
156 ms |
230060 KB |
Output is correct |
3 |
Correct |
157 ms |
230068 KB |
Output is correct |
4 |
Correct |
111 ms |
231292 KB |
Output is correct |
5 |
Correct |
61 ms |
196836 KB |
Output is correct |
6 |
Correct |
115 ms |
219436 KB |
Output is correct |
7 |
Correct |
154 ms |
219024 KB |
Output is correct |
8 |
Correct |
151 ms |
219212 KB |
Output is correct |
9 |
Correct |
157 ms |
219300 KB |
Output is correct |
10 |
Correct |
137 ms |
224208 KB |
Output is correct |
11 |
Correct |
130 ms |
223236 KB |
Output is correct |
12 |
Correct |
87 ms |
196772 KB |
Output is correct |
13 |
Execution timed out |
3547 ms |
228100 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196908 KB |
Output is correct |
2 |
Correct |
61 ms |
197084 KB |
Output is correct |
3 |
Correct |
61 ms |
197296 KB |
Output is correct |
4 |
Correct |
60 ms |
197372 KB |
Output is correct |
5 |
Correct |
65 ms |
197584 KB |
Output is correct |
6 |
Correct |
60 ms |
197536 KB |
Output is correct |
7 |
Correct |
58 ms |
196800 KB |
Output is correct |
8 |
Correct |
126 ms |
231172 KB |
Output is correct |
9 |
Correct |
172 ms |
231168 KB |
Output is correct |
10 |
Correct |
58 ms |
196904 KB |
Output is correct |
11 |
Correct |
169 ms |
229956 KB |
Output is correct |
12 |
Correct |
202 ms |
230080 KB |
Output is correct |
13 |
Correct |
113 ms |
231212 KB |
Output is correct |
14 |
Correct |
56 ms |
196812 KB |
Output is correct |
15 |
Correct |
121 ms |
219508 KB |
Output is correct |
16 |
Correct |
146 ms |
218880 KB |
Output is correct |
17 |
Correct |
183 ms |
219464 KB |
Output is correct |
18 |
Correct |
153 ms |
219476 KB |
Output is correct |
19 |
Correct |
126 ms |
224236 KB |
Output is correct |
20 |
Correct |
124 ms |
223492 KB |
Output is correct |
21 |
Correct |
129 ms |
223828 KB |
Output is correct |
22 |
Correct |
132 ms |
224084 KB |
Output is correct |
23 |
Correct |
145 ms |
220416 KB |
Output is correct |
24 |
Correct |
137 ms |
220484 KB |
Output is correct |
25 |
Correct |
143 ms |
226048 KB |
Output is correct |
26 |
Correct |
121 ms |
219684 KB |
Output is correct |
27 |
Correct |
115 ms |
219920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
196908 KB |
Output is correct |
2 |
Correct |
61 ms |
197084 KB |
Output is correct |
3 |
Correct |
61 ms |
197296 KB |
Output is correct |
4 |
Correct |
60 ms |
197372 KB |
Output is correct |
5 |
Correct |
65 ms |
197584 KB |
Output is correct |
6 |
Correct |
60 ms |
197536 KB |
Output is correct |
7 |
Correct |
58 ms |
196800 KB |
Output is correct |
8 |
Correct |
126 ms |
231172 KB |
Output is correct |
9 |
Correct |
172 ms |
231168 KB |
Output is correct |
10 |
Correct |
58 ms |
196904 KB |
Output is correct |
11 |
Correct |
169 ms |
229956 KB |
Output is correct |
12 |
Correct |
202 ms |
230080 KB |
Output is correct |
13 |
Correct |
113 ms |
231212 KB |
Output is correct |
14 |
Correct |
56 ms |
196812 KB |
Output is correct |
15 |
Correct |
121 ms |
219508 KB |
Output is correct |
16 |
Correct |
146 ms |
218880 KB |
Output is correct |
17 |
Correct |
183 ms |
219464 KB |
Output is correct |
18 |
Correct |
153 ms |
219476 KB |
Output is correct |
19 |
Correct |
126 ms |
224236 KB |
Output is correct |
20 |
Correct |
124 ms |
223492 KB |
Output is correct |
21 |
Correct |
129 ms |
223828 KB |
Output is correct |
22 |
Correct |
132 ms |
224084 KB |
Output is correct |
23 |
Correct |
145 ms |
220416 KB |
Output is correct |
24 |
Correct |
137 ms |
220484 KB |
Output is correct |
25 |
Correct |
143 ms |
226048 KB |
Output is correct |
26 |
Correct |
121 ms |
219684 KB |
Output is correct |
27 |
Correct |
115 ms |
219920 KB |
Output is correct |
28 |
Correct |
58 ms |
196808 KB |
Output is correct |
29 |
Correct |
710 ms |
197084 KB |
Output is correct |
30 |
Correct |
402 ms |
197488 KB |
Output is correct |
31 |
Correct |
653 ms |
197344 KB |
Output is correct |
32 |
Correct |
1036 ms |
197716 KB |
Output is correct |
33 |
Correct |
402 ms |
197632 KB |
Output is correct |
34 |
Correct |
53 ms |
196804 KB |
Output is correct |
35 |
Correct |
745 ms |
229268 KB |
Output is correct |
36 |
Correct |
953 ms |
229888 KB |
Output is correct |
37 |
Correct |
1091 ms |
229456 KB |
Output is correct |
38 |
Correct |
60 ms |
196808 KB |
Output is correct |
39 |
Correct |
2673 ms |
228120 KB |
Output is correct |
40 |
Correct |
643 ms |
229488 KB |
Output is correct |
41 |
Correct |
3355 ms |
227840 KB |
Output is correct |
42 |
Correct |
3419 ms |
228124 KB |
Output is correct |
43 |
Correct |
54 ms |
196804 KB |
Output is correct |
44 |
Correct |
777 ms |
217768 KB |
Output is correct |
45 |
Correct |
1574 ms |
219148 KB |
Output is correct |
46 |
Correct |
2742 ms |
217460 KB |
Output is correct |
47 |
Correct |
2436 ms |
217308 KB |
Output is correct |
48 |
Correct |
1864 ms |
221172 KB |
Output is correct |
49 |
Correct |
1284 ms |
223228 KB |
Output is correct |
50 |
Correct |
553 ms |
224028 KB |
Output is correct |
51 |
Correct |
1126 ms |
227840 KB |
Output is correct |
52 |
Correct |
908 ms |
225024 KB |
Output is correct |
53 |
Correct |
1164 ms |
226308 KB |
Output is correct |
54 |
Correct |
877 ms |
224876 KB |
Output is correct |
55 |
Correct |
1317 ms |
226732 KB |
Output is correct |
56 |
Correct |
1698 ms |
218620 KB |
Output is correct |
57 |
Correct |
2116 ms |
225252 KB |
Output is correct |
58 |
Correct |
1662 ms |
219648 KB |
Output is correct |
59 |
Correct |
291 ms |
220160 KB |
Output is correct |