#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(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;
}
gp_hash_table<int, int> buffer;
const int THRESH = 1500;
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[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]);
buffer[ids[a]]--;
buffer[ids[b]]--;
last++;
ids[a] = ids[b] = last;
buffer[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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
195012 KB |
Output is correct |
2 |
Correct |
59 ms |
195820 KB |
Output is correct |
3 |
Correct |
65 ms |
195848 KB |
Output is correct |
4 |
Correct |
57 ms |
195800 KB |
Output is correct |
5 |
Correct |
59 ms |
196112 KB |
Output is correct |
6 |
Correct |
54 ms |
196100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
195012 KB |
Output is correct |
2 |
Correct |
59 ms |
195820 KB |
Output is correct |
3 |
Correct |
65 ms |
195848 KB |
Output is correct |
4 |
Correct |
57 ms |
195800 KB |
Output is correct |
5 |
Correct |
59 ms |
196112 KB |
Output is correct |
6 |
Correct |
54 ms |
196100 KB |
Output is correct |
7 |
Correct |
55 ms |
195012 KB |
Output is correct |
8 |
Correct |
776 ms |
197644 KB |
Output is correct |
9 |
Correct |
516 ms |
197980 KB |
Output is correct |
10 |
Correct |
605 ms |
197852 KB |
Output is correct |
11 |
Correct |
345 ms |
198036 KB |
Output is correct |
12 |
Correct |
228 ms |
198148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
195060 KB |
Output is correct |
2 |
Correct |
129 ms |
234408 KB |
Output is correct |
3 |
Correct |
126 ms |
234368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
195060 KB |
Output is correct |
2 |
Correct |
129 ms |
234408 KB |
Output is correct |
3 |
Correct |
126 ms |
234368 KB |
Output is correct |
4 |
Correct |
54 ms |
195016 KB |
Output is correct |
5 |
Correct |
1018 ms |
231700 KB |
Output is correct |
6 |
Correct |
1476 ms |
231684 KB |
Output is correct |
7 |
Correct |
1763 ms |
231652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
195016 KB |
Output is correct |
2 |
Correct |
179 ms |
233092 KB |
Output is correct |
3 |
Correct |
151 ms |
233084 KB |
Output is correct |
4 |
Correct |
111 ms |
234316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
195016 KB |
Output is correct |
2 |
Correct |
179 ms |
233092 KB |
Output is correct |
3 |
Correct |
151 ms |
233084 KB |
Output is correct |
4 |
Correct |
111 ms |
234316 KB |
Output is correct |
5 |
Correct |
54 ms |
195016 KB |
Output is correct |
6 |
Execution timed out |
3540 ms |
230524 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
195016 KB |
Output is correct |
2 |
Correct |
117 ms |
222592 KB |
Output is correct |
3 |
Correct |
134 ms |
221812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
195016 KB |
Output is correct |
2 |
Correct |
117 ms |
222592 KB |
Output is correct |
3 |
Correct |
134 ms |
221812 KB |
Output is correct |
4 |
Correct |
55 ms |
195024 KB |
Output is correct |
5 |
Correct |
941 ms |
220032 KB |
Output is correct |
6 |
Correct |
2029 ms |
224204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
195016 KB |
Output is correct |
2 |
Correct |
143 ms |
233032 KB |
Output is correct |
3 |
Correct |
158 ms |
233088 KB |
Output is correct |
4 |
Correct |
112 ms |
234368 KB |
Output is correct |
5 |
Correct |
50 ms |
195092 KB |
Output is correct |
6 |
Correct |
106 ms |
222440 KB |
Output is correct |
7 |
Correct |
138 ms |
221888 KB |
Output is correct |
8 |
Correct |
135 ms |
222460 KB |
Output is correct |
9 |
Correct |
149 ms |
222336 KB |
Output is correct |
10 |
Correct |
123 ms |
227196 KB |
Output is correct |
11 |
Correct |
125 ms |
226436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
195016 KB |
Output is correct |
2 |
Correct |
143 ms |
233032 KB |
Output is correct |
3 |
Correct |
158 ms |
233088 KB |
Output is correct |
4 |
Correct |
112 ms |
234368 KB |
Output is correct |
5 |
Correct |
50 ms |
195092 KB |
Output is correct |
6 |
Correct |
106 ms |
222440 KB |
Output is correct |
7 |
Correct |
138 ms |
221888 KB |
Output is correct |
8 |
Correct |
135 ms |
222460 KB |
Output is correct |
9 |
Correct |
149 ms |
222336 KB |
Output is correct |
10 |
Correct |
123 ms |
227196 KB |
Output is correct |
11 |
Correct |
125 ms |
226436 KB |
Output is correct |
12 |
Correct |
58 ms |
195260 KB |
Output is correct |
13 |
Execution timed out |
3550 ms |
230440 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
195016 KB |
Output is correct |
2 |
Correct |
67 ms |
195860 KB |
Output is correct |
3 |
Correct |
58 ms |
196004 KB |
Output is correct |
4 |
Correct |
59 ms |
195804 KB |
Output is correct |
5 |
Correct |
55 ms |
196100 KB |
Output is correct |
6 |
Correct |
54 ms |
196096 KB |
Output is correct |
7 |
Correct |
52 ms |
195096 KB |
Output is correct |
8 |
Correct |
114 ms |
234372 KB |
Output is correct |
9 |
Correct |
122 ms |
234116 KB |
Output is correct |
10 |
Correct |
49 ms |
194912 KB |
Output is correct |
11 |
Correct |
141 ms |
232916 KB |
Output is correct |
12 |
Correct |
146 ms |
232956 KB |
Output is correct |
13 |
Correct |
112 ms |
234112 KB |
Output is correct |
14 |
Correct |
51 ms |
195016 KB |
Output is correct |
15 |
Correct |
110 ms |
222592 KB |
Output is correct |
16 |
Correct |
132 ms |
222020 KB |
Output is correct |
17 |
Correct |
133 ms |
222308 KB |
Output is correct |
18 |
Correct |
148 ms |
222520 KB |
Output is correct |
19 |
Correct |
125 ms |
226996 KB |
Output is correct |
20 |
Correct |
124 ms |
226288 KB |
Output is correct |
21 |
Correct |
118 ms |
226948 KB |
Output is correct |
22 |
Correct |
125 ms |
227200 KB |
Output is correct |
23 |
Correct |
154 ms |
223560 KB |
Output is correct |
24 |
Correct |
145 ms |
223744 KB |
Output is correct |
25 |
Correct |
129 ms |
228996 KB |
Output is correct |
26 |
Correct |
123 ms |
222592 KB |
Output is correct |
27 |
Correct |
130 ms |
222920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
195016 KB |
Output is correct |
2 |
Correct |
67 ms |
195860 KB |
Output is correct |
3 |
Correct |
58 ms |
196004 KB |
Output is correct |
4 |
Correct |
59 ms |
195804 KB |
Output is correct |
5 |
Correct |
55 ms |
196100 KB |
Output is correct |
6 |
Correct |
54 ms |
196096 KB |
Output is correct |
7 |
Correct |
52 ms |
195096 KB |
Output is correct |
8 |
Correct |
114 ms |
234372 KB |
Output is correct |
9 |
Correct |
122 ms |
234116 KB |
Output is correct |
10 |
Correct |
49 ms |
194912 KB |
Output is correct |
11 |
Correct |
141 ms |
232916 KB |
Output is correct |
12 |
Correct |
146 ms |
232956 KB |
Output is correct |
13 |
Correct |
112 ms |
234112 KB |
Output is correct |
14 |
Correct |
51 ms |
195016 KB |
Output is correct |
15 |
Correct |
110 ms |
222592 KB |
Output is correct |
16 |
Correct |
132 ms |
222020 KB |
Output is correct |
17 |
Correct |
133 ms |
222308 KB |
Output is correct |
18 |
Correct |
148 ms |
222520 KB |
Output is correct |
19 |
Correct |
125 ms |
226996 KB |
Output is correct |
20 |
Correct |
124 ms |
226288 KB |
Output is correct |
21 |
Correct |
118 ms |
226948 KB |
Output is correct |
22 |
Correct |
125 ms |
227200 KB |
Output is correct |
23 |
Correct |
154 ms |
223560 KB |
Output is correct |
24 |
Correct |
145 ms |
223744 KB |
Output is correct |
25 |
Correct |
129 ms |
228996 KB |
Output is correct |
26 |
Correct |
123 ms |
222592 KB |
Output is correct |
27 |
Correct |
130 ms |
222920 KB |
Output is correct |
28 |
Correct |
55 ms |
194940 KB |
Output is correct |
29 |
Correct |
767 ms |
197652 KB |
Output is correct |
30 |
Correct |
505 ms |
197892 KB |
Output is correct |
31 |
Correct |
663 ms |
197800 KB |
Output is correct |
32 |
Correct |
346 ms |
197832 KB |
Output is correct |
33 |
Correct |
228 ms |
198148 KB |
Output is correct |
34 |
Correct |
59 ms |
195016 KB |
Output is correct |
35 |
Correct |
909 ms |
231556 KB |
Output is correct |
36 |
Correct |
1298 ms |
231548 KB |
Output is correct |
37 |
Correct |
1344 ms |
231552 KB |
Output is correct |
38 |
Correct |
55 ms |
195012 KB |
Output is correct |
39 |
Execution timed out |
3582 ms |
230528 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |