#include <bits/stdc++.h>
using namespace std;
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(int u, int v){
if(st[u] > st[v]) swap(u, v);
return rmq(st[u], st[v]).second;
}
inline bool search(int a, 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;
}
vector<pair<int,int>> buffer;
const int THRESH = 750;
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(buffer.size() >= THRESH){
for(auto p : buffer) evp[p.first] += p.second;
update(1);
for(auto p : buffer) evp[p.first] -= p.second;
buffer.clear();
}
int ret = qs[d];
for(auto p : buffer){
if(search(p.first, d)){
ret += p.second;
}
}
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;
buffer.push_back({i, 1});
}
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]);
buffer.push_back({ids[a], -1});
buffer.push_back({ids[b], -1});
last++;
ids[a] = ids[b] = last;
buffer.push_back({last, 2});
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 |
194948 KB |
Output is correct |
2 |
Correct |
56 ms |
195612 KB |
Output is correct |
3 |
Correct |
59 ms |
195756 KB |
Output is correct |
4 |
Correct |
65 ms |
195804 KB |
Output is correct |
5 |
Correct |
70 ms |
196096 KB |
Output is correct |
6 |
Correct |
57 ms |
196112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
194948 KB |
Output is correct |
2 |
Correct |
56 ms |
195612 KB |
Output is correct |
3 |
Correct |
59 ms |
195756 KB |
Output is correct |
4 |
Correct |
65 ms |
195804 KB |
Output is correct |
5 |
Correct |
70 ms |
196096 KB |
Output is correct |
6 |
Correct |
57 ms |
196112 KB |
Output is correct |
7 |
Correct |
56 ms |
195016 KB |
Output is correct |
8 |
Correct |
209 ms |
197496 KB |
Output is correct |
9 |
Correct |
145 ms |
197836 KB |
Output is correct |
10 |
Correct |
174 ms |
197668 KB |
Output is correct |
11 |
Correct |
240 ms |
197940 KB |
Output is correct |
12 |
Correct |
189 ms |
198144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
195020 KB |
Output is correct |
2 |
Correct |
121 ms |
231428 KB |
Output is correct |
3 |
Correct |
112 ms |
232860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
195020 KB |
Output is correct |
2 |
Correct |
121 ms |
231428 KB |
Output is correct |
3 |
Correct |
112 ms |
232860 KB |
Output is correct |
4 |
Correct |
56 ms |
195016 KB |
Output is correct |
5 |
Correct |
1932 ms |
229428 KB |
Output is correct |
6 |
Correct |
2134 ms |
229800 KB |
Output is correct |
7 |
Correct |
2067 ms |
229616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
195016 KB |
Output is correct |
2 |
Correct |
152 ms |
231936 KB |
Output is correct |
3 |
Correct |
139 ms |
230196 KB |
Output is correct |
4 |
Correct |
107 ms |
232448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
195016 KB |
Output is correct |
2 |
Correct |
152 ms |
231936 KB |
Output is correct |
3 |
Correct |
139 ms |
230196 KB |
Output is correct |
4 |
Correct |
107 ms |
232448 KB |
Output is correct |
5 |
Correct |
55 ms |
195064 KB |
Output is correct |
6 |
Execution timed out |
3574 ms |
228028 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
195016 KB |
Output is correct |
2 |
Correct |
106 ms |
219392 KB |
Output is correct |
3 |
Correct |
127 ms |
219652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
195016 KB |
Output is correct |
2 |
Correct |
106 ms |
219392 KB |
Output is correct |
3 |
Correct |
127 ms |
219652 KB |
Output is correct |
4 |
Correct |
55 ms |
194932 KB |
Output is correct |
5 |
Correct |
1063 ms |
217868 KB |
Output is correct |
6 |
Correct |
1958 ms |
219644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
195016 KB |
Output is correct |
2 |
Correct |
142 ms |
231664 KB |
Output is correct |
3 |
Correct |
141 ms |
230144 KB |
Output is correct |
4 |
Correct |
109 ms |
231424 KB |
Output is correct |
5 |
Correct |
58 ms |
195016 KB |
Output is correct |
6 |
Correct |
103 ms |
219652 KB |
Output is correct |
7 |
Correct |
127 ms |
220304 KB |
Output is correct |
8 |
Correct |
132 ms |
220992 KB |
Output is correct |
9 |
Correct |
137 ms |
220348 KB |
Output is correct |
10 |
Correct |
121 ms |
225192 KB |
Output is correct |
11 |
Correct |
122 ms |
223328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
195016 KB |
Output is correct |
2 |
Correct |
142 ms |
231664 KB |
Output is correct |
3 |
Correct |
141 ms |
230144 KB |
Output is correct |
4 |
Correct |
109 ms |
231424 KB |
Output is correct |
5 |
Correct |
58 ms |
195016 KB |
Output is correct |
6 |
Correct |
103 ms |
219652 KB |
Output is correct |
7 |
Correct |
127 ms |
220304 KB |
Output is correct |
8 |
Correct |
132 ms |
220992 KB |
Output is correct |
9 |
Correct |
137 ms |
220348 KB |
Output is correct |
10 |
Correct |
121 ms |
225192 KB |
Output is correct |
11 |
Correct |
122 ms |
223328 KB |
Output is correct |
12 |
Correct |
61 ms |
195016 KB |
Output is correct |
13 |
Execution timed out |
3580 ms |
228032 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
195268 KB |
Output is correct |
2 |
Correct |
58 ms |
195548 KB |
Output is correct |
3 |
Correct |
55 ms |
195848 KB |
Output is correct |
4 |
Correct |
57 ms |
195704 KB |
Output is correct |
5 |
Correct |
55 ms |
195972 KB |
Output is correct |
6 |
Correct |
63 ms |
196096 KB |
Output is correct |
7 |
Correct |
51 ms |
195016 KB |
Output is correct |
8 |
Correct |
112 ms |
231680 KB |
Output is correct |
9 |
Correct |
116 ms |
231080 KB |
Output is correct |
10 |
Correct |
57 ms |
195024 KB |
Output is correct |
11 |
Correct |
138 ms |
230608 KB |
Output is correct |
12 |
Correct |
130 ms |
231672 KB |
Output is correct |
13 |
Correct |
103 ms |
231940 KB |
Output is correct |
14 |
Correct |
49 ms |
195012 KB |
Output is correct |
15 |
Correct |
106 ms |
221232 KB |
Output is correct |
16 |
Correct |
132 ms |
220704 KB |
Output is correct |
17 |
Correct |
135 ms |
220928 KB |
Output is correct |
18 |
Correct |
134 ms |
220928 KB |
Output is correct |
19 |
Correct |
134 ms |
226052 KB |
Output is correct |
20 |
Correct |
130 ms |
223488 KB |
Output is correct |
21 |
Correct |
116 ms |
224512 KB |
Output is correct |
22 |
Correct |
119 ms |
224256 KB |
Output is correct |
23 |
Correct |
132 ms |
221948 KB |
Output is correct |
24 |
Correct |
131 ms |
221700 KB |
Output is correct |
25 |
Correct |
123 ms |
225796 KB |
Output is correct |
26 |
Correct |
126 ms |
220392 KB |
Output is correct |
27 |
Correct |
118 ms |
221520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
195268 KB |
Output is correct |
2 |
Correct |
58 ms |
195548 KB |
Output is correct |
3 |
Correct |
55 ms |
195848 KB |
Output is correct |
4 |
Correct |
57 ms |
195704 KB |
Output is correct |
5 |
Correct |
55 ms |
195972 KB |
Output is correct |
6 |
Correct |
63 ms |
196096 KB |
Output is correct |
7 |
Correct |
51 ms |
195016 KB |
Output is correct |
8 |
Correct |
112 ms |
231680 KB |
Output is correct |
9 |
Correct |
116 ms |
231080 KB |
Output is correct |
10 |
Correct |
57 ms |
195024 KB |
Output is correct |
11 |
Correct |
138 ms |
230608 KB |
Output is correct |
12 |
Correct |
130 ms |
231672 KB |
Output is correct |
13 |
Correct |
103 ms |
231940 KB |
Output is correct |
14 |
Correct |
49 ms |
195012 KB |
Output is correct |
15 |
Correct |
106 ms |
221232 KB |
Output is correct |
16 |
Correct |
132 ms |
220704 KB |
Output is correct |
17 |
Correct |
135 ms |
220928 KB |
Output is correct |
18 |
Correct |
134 ms |
220928 KB |
Output is correct |
19 |
Correct |
134 ms |
226052 KB |
Output is correct |
20 |
Correct |
130 ms |
223488 KB |
Output is correct |
21 |
Correct |
116 ms |
224512 KB |
Output is correct |
22 |
Correct |
119 ms |
224256 KB |
Output is correct |
23 |
Correct |
132 ms |
221948 KB |
Output is correct |
24 |
Correct |
131 ms |
221700 KB |
Output is correct |
25 |
Correct |
123 ms |
225796 KB |
Output is correct |
26 |
Correct |
126 ms |
220392 KB |
Output is correct |
27 |
Correct |
118 ms |
221520 KB |
Output is correct |
28 |
Correct |
56 ms |
195148 KB |
Output is correct |
29 |
Correct |
202 ms |
197344 KB |
Output is correct |
30 |
Correct |
144 ms |
197892 KB |
Output is correct |
31 |
Correct |
174 ms |
197668 KB |
Output is correct |
32 |
Correct |
241 ms |
197892 KB |
Output is correct |
33 |
Correct |
192 ms |
198148 KB |
Output is correct |
34 |
Correct |
55 ms |
195012 KB |
Output is correct |
35 |
Correct |
1874 ms |
229468 KB |
Output is correct |
36 |
Correct |
2024 ms |
229720 KB |
Output is correct |
37 |
Correct |
2096 ms |
229664 KB |
Output is correct |
38 |
Correct |
55 ms |
194924 KB |
Output is correct |
39 |
Execution timed out |
3570 ms |
228052 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |