#include <bits/stdc++.h>
using namespace std;
const int Z = 1.2e5, B = 17;
int N, K, p[Z][B], up[Z][2], d[Z], pE[Z], res[Z];
vector<int> h[Z];
vector<array<int, 2>> g[Z], q0[Z], q1[Z];
vector<array<int, 3>> qQ;
vector<array<int, 4>> pQ;
void init(const int &u) {
for(int i = 0; i + 1 < B; ++i)
p[u][i+1] = p[p[u][i]][i];
for(const auto &[v, i] : g[u]) if(v != p[u][0]) {
p[v][0] = u;
d[v] = d[u] + 1;
pE[v] = i;
if(pE[u] < i) {
up[v][0] = up[u][0];
up[v][1] = u;
} else {
up[v][0] = u;
up[v][1] = up[u][1];
}
init(v);
}
}
int _u, _v;
int lca(int u, int v) {
bool swapped = 0;
if((swapped = (d[u] < d[v]))) swap(u, v);
if(d[u] > d[v]) {
for(int i = B; i--; ) if((d[u] - d[v] - 1) & (1<<i))
u = p[u][i];
if(p[u][0] == v) {
_u = u;
return v;
}
u = p[u][0];
}
for(int i = B; i--; ) if(p[u][i] != p[v][i])
u = p[u][i], v = p[v][i];
if(swapped) swap(u, v);
_u = u, _v = v;
return p[u][0];
}
int F[2*Z];
void add(int i, const int &v) {
for(++i; i < N + K; i += i&-i) F[i] += v;
}
int get(int i) {
int v {};
for(++i; i > 0 + 0; i -= i&-i) v += F[i];
return v;
}
void dfs(const int &u) {
add(pE[u], 1);
for(const auto &[j, i] : q1[u])
res[j] -= get(i);
for(const auto &[v, i] : g[u]) if(v != p[u][0])
dfs(v);
for(const auto &[j, i] : q0[u])
res[j] += get(i);
for(const int &v : h[u])
add(pE[v], -1);
}
int main() {
cin >> N >> K;
for(int i {}, j {}; i < N - 1 + K; ++i) {
char t; int u, v;
cin >> t >> u; --u;
if(t == 'S') {
cin >> v; --v;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
if(t == 'Q') {
cin >> v; --v;
pQ.push_back({j++, i, u, v});
}
if(t == 'C')
qQ.push_back({j++, i, u});
}
for(int u = N; u--; )
reverse(begin(g[u]), end(g[u]));
init(0);
for(const auto &[j, i, u] : qQ) {
q0[u].push_back({j, i});
q1[up[u][1]].push_back({j, i});
}
for(int u = N; u--; )
h[up[u][0]].push_back(u);
dfs(0);
for(const auto &[j, i, u, v] : pQ) {
int w = lca(u, v), val = w == u ? pE[_u] : pE[u];
if(w != u && w != v && pE[_u] < pE[_v])
val = i;
if(u == v || (max(d[up[u][0]], d[up[v][1]]) <= d[w] && val < i))
res[j] = -1;
else
res[j] = -2;
}
for(int j = 0; j < K; ++j) {
if(res[j] < 0) cout << (res[j] & 1 ? "yes" : "no");
else cout << 1 + res[j];
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
15680 KB |
Output is correct |
2 |
Correct |
93 ms |
16580 KB |
Output is correct |
3 |
Correct |
88 ms |
16708 KB |
Output is correct |
4 |
Correct |
108 ms |
16800 KB |
Output is correct |
5 |
Correct |
92 ms |
16504 KB |
Output is correct |
6 |
Correct |
91 ms |
16412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
15680 KB |
Output is correct |
2 |
Correct |
93 ms |
16580 KB |
Output is correct |
3 |
Correct |
88 ms |
16708 KB |
Output is correct |
4 |
Correct |
108 ms |
16800 KB |
Output is correct |
5 |
Correct |
92 ms |
16504 KB |
Output is correct |
6 |
Correct |
91 ms |
16412 KB |
Output is correct |
7 |
Correct |
69 ms |
15724 KB |
Output is correct |
8 |
Correct |
84 ms |
17552 KB |
Output is correct |
9 |
Correct |
81 ms |
17640 KB |
Output is correct |
10 |
Correct |
86 ms |
17716 KB |
Output is correct |
11 |
Correct |
81 ms |
17448 KB |
Output is correct |
12 |
Correct |
75 ms |
17156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15648 KB |
Output is correct |
2 |
Correct |
231 ms |
33548 KB |
Output is correct |
3 |
Correct |
216 ms |
33580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15648 KB |
Output is correct |
2 |
Correct |
231 ms |
33548 KB |
Output is correct |
3 |
Correct |
216 ms |
33580 KB |
Output is correct |
4 |
Correct |
68 ms |
15680 KB |
Output is correct |
5 |
Correct |
226 ms |
34120 KB |
Output is correct |
6 |
Correct |
169 ms |
34504 KB |
Output is correct |
7 |
Correct |
171 ms |
34876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15668 KB |
Output is correct |
2 |
Correct |
258 ms |
41528 KB |
Output is correct |
3 |
Correct |
248 ms |
41620 KB |
Output is correct |
4 |
Correct |
239 ms |
40676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15668 KB |
Output is correct |
2 |
Correct |
258 ms |
41528 KB |
Output is correct |
3 |
Correct |
248 ms |
41620 KB |
Output is correct |
4 |
Correct |
239 ms |
40676 KB |
Output is correct |
5 |
Correct |
66 ms |
15672 KB |
Output is correct |
6 |
Correct |
258 ms |
43684 KB |
Output is correct |
7 |
Correct |
245 ms |
44704 KB |
Output is correct |
8 |
Correct |
258 ms |
44728 KB |
Output is correct |
9 |
Correct |
275 ms |
44636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
15584 KB |
Output is correct |
2 |
Correct |
239 ms |
34092 KB |
Output is correct |
3 |
Correct |
244 ms |
34608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
15584 KB |
Output is correct |
2 |
Correct |
239 ms |
34092 KB |
Output is correct |
3 |
Correct |
244 ms |
34608 KB |
Output is correct |
4 |
Correct |
72 ms |
15648 KB |
Output is correct |
5 |
Correct |
238 ms |
35872 KB |
Output is correct |
6 |
Correct |
254 ms |
36544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15728 KB |
Output is correct |
2 |
Correct |
262 ms |
41644 KB |
Output is correct |
3 |
Correct |
243 ms |
41636 KB |
Output is correct |
4 |
Correct |
235 ms |
40588 KB |
Output is correct |
5 |
Correct |
74 ms |
15648 KB |
Output is correct |
6 |
Correct |
227 ms |
34100 KB |
Output is correct |
7 |
Correct |
257 ms |
34640 KB |
Output is correct |
8 |
Correct |
285 ms |
34408 KB |
Output is correct |
9 |
Correct |
270 ms |
35072 KB |
Output is correct |
10 |
Correct |
311 ms |
38040 KB |
Output is correct |
11 |
Correct |
318 ms |
36968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
15728 KB |
Output is correct |
2 |
Correct |
262 ms |
41644 KB |
Output is correct |
3 |
Correct |
243 ms |
41636 KB |
Output is correct |
4 |
Correct |
235 ms |
40588 KB |
Output is correct |
5 |
Correct |
74 ms |
15648 KB |
Output is correct |
6 |
Correct |
227 ms |
34100 KB |
Output is correct |
7 |
Correct |
257 ms |
34640 KB |
Output is correct |
8 |
Correct |
285 ms |
34408 KB |
Output is correct |
9 |
Correct |
270 ms |
35072 KB |
Output is correct |
10 |
Correct |
311 ms |
38040 KB |
Output is correct |
11 |
Correct |
318 ms |
36968 KB |
Output is correct |
12 |
Correct |
66 ms |
15760 KB |
Output is correct |
13 |
Correct |
256 ms |
43572 KB |
Output is correct |
14 |
Correct |
223 ms |
44660 KB |
Output is correct |
15 |
Correct |
290 ms |
44772 KB |
Output is correct |
16 |
Correct |
258 ms |
44668 KB |
Output is correct |
17 |
Correct |
68 ms |
15736 KB |
Output is correct |
18 |
Correct |
230 ms |
35852 KB |
Output is correct |
19 |
Correct |
245 ms |
36528 KB |
Output is correct |
20 |
Correct |
309 ms |
36408 KB |
Output is correct |
21 |
Correct |
278 ms |
36748 KB |
Output is correct |
22 |
Correct |
293 ms |
39428 KB |
Output is correct |
23 |
Correct |
306 ms |
39716 KB |
Output is correct |
24 |
Correct |
291 ms |
38456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
15620 KB |
Output is correct |
2 |
Correct |
101 ms |
16596 KB |
Output is correct |
3 |
Correct |
93 ms |
16680 KB |
Output is correct |
4 |
Correct |
98 ms |
16688 KB |
Output is correct |
5 |
Correct |
91 ms |
16496 KB |
Output is correct |
6 |
Correct |
88 ms |
16356 KB |
Output is correct |
7 |
Correct |
74 ms |
15728 KB |
Output is correct |
8 |
Correct |
217 ms |
33616 KB |
Output is correct |
9 |
Correct |
217 ms |
33672 KB |
Output is correct |
10 |
Correct |
69 ms |
15672 KB |
Output is correct |
11 |
Correct |
255 ms |
41608 KB |
Output is correct |
12 |
Correct |
246 ms |
41616 KB |
Output is correct |
13 |
Correct |
228 ms |
40576 KB |
Output is correct |
14 |
Correct |
72 ms |
15732 KB |
Output is correct |
15 |
Correct |
224 ms |
34080 KB |
Output is correct |
16 |
Correct |
246 ms |
34576 KB |
Output is correct |
17 |
Correct |
284 ms |
34492 KB |
Output is correct |
18 |
Correct |
262 ms |
34940 KB |
Output is correct |
19 |
Correct |
347 ms |
38132 KB |
Output is correct |
20 |
Correct |
315 ms |
36984 KB |
Output is correct |
21 |
Correct |
245 ms |
34024 KB |
Output is correct |
22 |
Correct |
236 ms |
34016 KB |
Output is correct |
23 |
Correct |
246 ms |
34224 KB |
Output is correct |
24 |
Correct |
260 ms |
34208 KB |
Output is correct |
25 |
Correct |
271 ms |
36040 KB |
Output is correct |
26 |
Correct |
269 ms |
35000 KB |
Output is correct |
27 |
Correct |
271 ms |
34956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
15620 KB |
Output is correct |
2 |
Correct |
101 ms |
16596 KB |
Output is correct |
3 |
Correct |
93 ms |
16680 KB |
Output is correct |
4 |
Correct |
98 ms |
16688 KB |
Output is correct |
5 |
Correct |
91 ms |
16496 KB |
Output is correct |
6 |
Correct |
88 ms |
16356 KB |
Output is correct |
7 |
Correct |
74 ms |
15728 KB |
Output is correct |
8 |
Correct |
217 ms |
33616 KB |
Output is correct |
9 |
Correct |
217 ms |
33672 KB |
Output is correct |
10 |
Correct |
69 ms |
15672 KB |
Output is correct |
11 |
Correct |
255 ms |
41608 KB |
Output is correct |
12 |
Correct |
246 ms |
41616 KB |
Output is correct |
13 |
Correct |
228 ms |
40576 KB |
Output is correct |
14 |
Correct |
72 ms |
15732 KB |
Output is correct |
15 |
Correct |
224 ms |
34080 KB |
Output is correct |
16 |
Correct |
246 ms |
34576 KB |
Output is correct |
17 |
Correct |
284 ms |
34492 KB |
Output is correct |
18 |
Correct |
262 ms |
34940 KB |
Output is correct |
19 |
Correct |
347 ms |
38132 KB |
Output is correct |
20 |
Correct |
315 ms |
36984 KB |
Output is correct |
21 |
Correct |
245 ms |
34024 KB |
Output is correct |
22 |
Correct |
236 ms |
34016 KB |
Output is correct |
23 |
Correct |
246 ms |
34224 KB |
Output is correct |
24 |
Correct |
260 ms |
34208 KB |
Output is correct |
25 |
Correct |
271 ms |
36040 KB |
Output is correct |
26 |
Correct |
269 ms |
35000 KB |
Output is correct |
27 |
Correct |
271 ms |
34956 KB |
Output is correct |
28 |
Correct |
70 ms |
15716 KB |
Output is correct |
29 |
Correct |
99 ms |
17744 KB |
Output is correct |
30 |
Correct |
89 ms |
17540 KB |
Output is correct |
31 |
Correct |
86 ms |
17712 KB |
Output is correct |
32 |
Correct |
80 ms |
17448 KB |
Output is correct |
33 |
Correct |
75 ms |
17228 KB |
Output is correct |
34 |
Correct |
68 ms |
15792 KB |
Output is correct |
35 |
Correct |
212 ms |
33904 KB |
Output is correct |
36 |
Correct |
159 ms |
34468 KB |
Output is correct |
37 |
Correct |
171 ms |
34800 KB |
Output is correct |
38 |
Correct |
66 ms |
15672 KB |
Output is correct |
39 |
Correct |
248 ms |
43584 KB |
Output is correct |
40 |
Correct |
221 ms |
44708 KB |
Output is correct |
41 |
Correct |
273 ms |
44780 KB |
Output is correct |
42 |
Correct |
271 ms |
44592 KB |
Output is correct |
43 |
Correct |
75 ms |
15716 KB |
Output is correct |
44 |
Correct |
229 ms |
36000 KB |
Output is correct |
45 |
Correct |
237 ms |
36564 KB |
Output is correct |
46 |
Correct |
270 ms |
36292 KB |
Output is correct |
47 |
Correct |
272 ms |
36852 KB |
Output is correct |
48 |
Correct |
285 ms |
39512 KB |
Output is correct |
49 |
Correct |
303 ms |
39744 KB |
Output is correct |
50 |
Correct |
283 ms |
38388 KB |
Output is correct |
51 |
Correct |
204 ms |
35788 KB |
Output is correct |
52 |
Correct |
198 ms |
35828 KB |
Output is correct |
53 |
Correct |
186 ms |
35048 KB |
Output is correct |
54 |
Correct |
194 ms |
35880 KB |
Output is correct |
55 |
Correct |
189 ms |
35460 KB |
Output is correct |
56 |
Correct |
227 ms |
34920 KB |
Output is correct |
57 |
Correct |
246 ms |
36764 KB |
Output is correct |
58 |
Correct |
264 ms |
37096 KB |
Output is correct |
59 |
Correct |
300 ms |
35320 KB |
Output is correct |