#include "bits/stdc++.h"
using namespace std;
const int N = 120000, L = 17;
int pp[L][N], dd[N], vv[N], up[N], dn[N];
vector<pair<int, int>> g[N];
void dfs(int i) {
for (int l = 1; l < L; l++) pp[l][i] = pp[l - 1][pp[l - 1][i]];
for (auto [j, h] : g[i])
if (j != pp[0][i]) {
vv[j] = h;
if (i == 0)
up[j] = dn[j] = i;
else if (vv[j] > vv[i])
up[j] = up[i], dn[j] = i;
else
up[j] = i, dn[j] = dn[i];
dd[j] = dd[i] + 1;
pp[0][j] = i;
dfs(j);
}
}
int jmp(int i, int k) {
for (int l = 0; l < L; l++)
if (k >> l & 1) i = pp[l][i];
return i;
}
int lca(int i, int j) {
if (dd[i] < dd[j]) swap(i, j);
i = jmp(i, dd[i] - dd[j]);
if (i == j) return i;
for (int l = L - 1; l >= 0; l--)
if (pp[l][i] != pp[l][j]) i = pp[l][i], j = pp[l][j];
return pp[0][i];
}
bool has(int i, int j, int h) {
if (i == j) return 1;
int p = lca(i, j);
if (p == i)
return (dd[dn[j]] <= dd[i] && vv[jmp(j, dd[j] - dd[i] - 1)] < h);
else if (p == j)
return (dd[up[i]] <= dd[j] && vv[i] < h);
else
return (dd[up[i]] <= dd[p] && dd[dn[j]] <= dd[p] &&
vv[jmp(i, dd[i] - dd[p] - 1)] > vv[jmp(j, dd[j] - dd[p] - 1)] &&
vv[i] < h);
}
int cp[N], sz[N];
bool dead[N];
vector<int> cup[N], cen[N], eds;
vector<pair<int, int>> upd[N * 2];
struct FT {
vector<int> t, inds;
int all;
void bld(vector<int> inds_) {
inds = inds_;
sort(inds.begin(), inds.end());
t.assign(inds.size(), 0);
all = 0;
}
void upd(int i) {
i = lower_bound(inds.begin(), inds.end(), i) - inds.begin();
while (i < (int)inds.size()) t[i]++, i |= i + 1;
all++;
}
int qry(int i) { // # > i
i = lower_bound(inds.begin(), inds.end(), i) - inds.begin();
int x = all;
while (i >= 0) x -= t[i], i &= i + 1, i--;
return x;
}
} ft[N];
int dfs1(int p, int i) {
sz[i] = 1;
for (auto [j, h] : g[i])
if (p != j && !dead[j]) sz[i] += dfs1(i, j);
return sz[i];
}
int dfs2(int p, int i, int n) {
for (auto [j, h] : g[i])
if (p != j && !dead[j] && sz[j] * 2 > n) return dfs2(i, j, n);
return i;
}
void dfs3(int c, int p, int i, int htop, int hlast, bool up, bool dn) {
cen[i].push_back(c);
cup[i].push_back(up ? htop : -1);
if (dn) {
upd[hlast].push_back({c, htop});
}
for (auto [j, h] : g[i])
if (p != j && !dead[j]) {
eds.push_back(h);
dfs3(c, i, j, htop, h, up && h < hlast, dn && h > hlast);
}
}
void cd(int p, int i) {
i = dfs2(-1, i, dfs1(-1, i));
cp[i] = p;
dead[i] = 1;
for (auto [j, h] : g[i])
if (!dead[j]) {
eds.push_back(h);
dfs3(i, i, j, h, h, 1, 1);
}
ft[i].bld(eds);
vector<int>().swap(eds);
for (auto [j, h] : g[i])
if (!dead[j]) cd(i, j);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<array<int, 3>> qry;
for (int h = 0; h < n + q - 1; h++) {
char t;
cin >> t;
if (t == 'S') {
int i, j;
cin >> i >> j, i--, j--;
g[i].push_back({j, h}), g[j].push_back({i, h});
} else if (t == 'Q') {
int i, j;
cin >> i >> j, i--, j--;
qry.push_back({i, j, h});
} else {
int i;
cin >> i, i--;
qry.push_back({-1, i, h});
}
}
pp[0][0] = 0;
dfs(0);
for (int i = 0; i < n; i++) cp[i] = -1;
cd(-1, 0);
for (int i = 0; i < n; i++) {
reverse(cen[i].begin(), cen[i].end());
reverse(cup[i].begin(), cup[i].end());
}
int ch = 0;
for (int h_ = 0; h_ < q; h_++) {
auto [i, j, h] = qry[h_];
while (ch <= h) {
for (auto [c, h] : upd[ch]) ft[c].upd(h);
ch++;
}
if (i == -1) {
i = j;
int cnt = 1 + ft[i].all, c = cp[i], d = 0;
while (~c) {
if (~cup[i][d] && cup[i][d] < h) {
int x = 1 + ft[c].qry(cup[i][d]);
cnt += x;
}
c = cp[c], d++;
}
cout << cnt << '\n';
} else {
cout << (has(i, j, h) ? "yes\n" : "no\n");
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
34100 KB |
Output is correct |
2 |
Correct |
40 ms |
35312 KB |
Output is correct |
3 |
Correct |
36 ms |
35272 KB |
Output is correct |
4 |
Correct |
47 ms |
35824 KB |
Output is correct |
5 |
Correct |
42 ms |
35864 KB |
Output is correct |
6 |
Correct |
42 ms |
35132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
34100 KB |
Output is correct |
2 |
Correct |
40 ms |
35312 KB |
Output is correct |
3 |
Correct |
36 ms |
35272 KB |
Output is correct |
4 |
Correct |
47 ms |
35824 KB |
Output is correct |
5 |
Correct |
42 ms |
35864 KB |
Output is correct |
6 |
Correct |
42 ms |
35132 KB |
Output is correct |
7 |
Correct |
30 ms |
34048 KB |
Output is correct |
8 |
Correct |
41 ms |
35032 KB |
Output is correct |
9 |
Correct |
49 ms |
35020 KB |
Output is correct |
10 |
Correct |
45 ms |
35436 KB |
Output is correct |
11 |
Correct |
57 ms |
35664 KB |
Output is correct |
12 |
Correct |
34 ms |
34928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
33988 KB |
Output is correct |
2 |
Correct |
170 ms |
54484 KB |
Output is correct |
3 |
Correct |
149 ms |
54420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
33988 KB |
Output is correct |
2 |
Correct |
170 ms |
54484 KB |
Output is correct |
3 |
Correct |
149 ms |
54420 KB |
Output is correct |
4 |
Correct |
30 ms |
34088 KB |
Output is correct |
5 |
Correct |
146 ms |
54380 KB |
Output is correct |
6 |
Correct |
161 ms |
53516 KB |
Output is correct |
7 |
Correct |
135 ms |
53812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
34004 KB |
Output is correct |
2 |
Correct |
419 ms |
87560 KB |
Output is correct |
3 |
Correct |
428 ms |
87460 KB |
Output is correct |
4 |
Correct |
350 ms |
96672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
34004 KB |
Output is correct |
2 |
Correct |
419 ms |
87560 KB |
Output is correct |
3 |
Correct |
428 ms |
87460 KB |
Output is correct |
4 |
Correct |
350 ms |
96672 KB |
Output is correct |
5 |
Correct |
27 ms |
34076 KB |
Output is correct |
6 |
Correct |
475 ms |
86988 KB |
Output is correct |
7 |
Correct |
351 ms |
95300 KB |
Output is correct |
8 |
Correct |
421 ms |
86656 KB |
Output is correct |
9 |
Correct |
446 ms |
86728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
33960 KB |
Output is correct |
2 |
Correct |
308 ms |
86524 KB |
Output is correct |
3 |
Correct |
382 ms |
79876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
33960 KB |
Output is correct |
2 |
Correct |
308 ms |
86524 KB |
Output is correct |
3 |
Correct |
382 ms |
79876 KB |
Output is correct |
4 |
Correct |
28 ms |
34252 KB |
Output is correct |
5 |
Correct |
321 ms |
86276 KB |
Output is correct |
6 |
Correct |
390 ms |
79112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33988 KB |
Output is correct |
2 |
Correct |
368 ms |
87448 KB |
Output is correct |
3 |
Correct |
416 ms |
87424 KB |
Output is correct |
4 |
Correct |
309 ms |
96704 KB |
Output is correct |
5 |
Correct |
26 ms |
33992 KB |
Output is correct |
6 |
Correct |
284 ms |
86704 KB |
Output is correct |
7 |
Correct |
347 ms |
79620 KB |
Output is correct |
8 |
Correct |
439 ms |
75524 KB |
Output is correct |
9 |
Correct |
491 ms |
75268 KB |
Output is correct |
10 |
Correct |
557 ms |
87556 KB |
Output is correct |
11 |
Correct |
588 ms |
87276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
33988 KB |
Output is correct |
2 |
Correct |
368 ms |
87448 KB |
Output is correct |
3 |
Correct |
416 ms |
87424 KB |
Output is correct |
4 |
Correct |
309 ms |
96704 KB |
Output is correct |
5 |
Correct |
26 ms |
33992 KB |
Output is correct |
6 |
Correct |
284 ms |
86704 KB |
Output is correct |
7 |
Correct |
347 ms |
79620 KB |
Output is correct |
8 |
Correct |
439 ms |
75524 KB |
Output is correct |
9 |
Correct |
491 ms |
75268 KB |
Output is correct |
10 |
Correct |
557 ms |
87556 KB |
Output is correct |
11 |
Correct |
588 ms |
87276 KB |
Output is correct |
12 |
Correct |
24 ms |
34068 KB |
Output is correct |
13 |
Correct |
408 ms |
87088 KB |
Output is correct |
14 |
Correct |
329 ms |
95328 KB |
Output is correct |
15 |
Correct |
522 ms |
86664 KB |
Output is correct |
16 |
Correct |
413 ms |
86716 KB |
Output is correct |
17 |
Correct |
27 ms |
34176 KB |
Output is correct |
18 |
Correct |
337 ms |
86292 KB |
Output is correct |
19 |
Correct |
405 ms |
79240 KB |
Output is correct |
20 |
Correct |
474 ms |
75276 KB |
Output is correct |
21 |
Correct |
448 ms |
75196 KB |
Output is correct |
22 |
Correct |
586 ms |
86540 KB |
Output is correct |
23 |
Correct |
615 ms |
90468 KB |
Output is correct |
24 |
Correct |
543 ms |
89860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
34120 KB |
Output is correct |
2 |
Correct |
54 ms |
35404 KB |
Output is correct |
3 |
Correct |
36 ms |
35120 KB |
Output is correct |
4 |
Correct |
53 ms |
35800 KB |
Output is correct |
5 |
Correct |
40 ms |
35848 KB |
Output is correct |
6 |
Correct |
34 ms |
35228 KB |
Output is correct |
7 |
Correct |
29 ms |
34000 KB |
Output is correct |
8 |
Correct |
145 ms |
54432 KB |
Output is correct |
9 |
Correct |
134 ms |
54412 KB |
Output is correct |
10 |
Correct |
23 ms |
34000 KB |
Output is correct |
11 |
Correct |
412 ms |
87552 KB |
Output is correct |
12 |
Correct |
404 ms |
87560 KB |
Output is correct |
13 |
Correct |
332 ms |
96676 KB |
Output is correct |
14 |
Correct |
31 ms |
34176 KB |
Output is correct |
15 |
Correct |
305 ms |
86528 KB |
Output is correct |
16 |
Correct |
411 ms |
79624 KB |
Output is correct |
17 |
Correct |
439 ms |
75496 KB |
Output is correct |
18 |
Correct |
496 ms |
75336 KB |
Output is correct |
19 |
Correct |
484 ms |
87440 KB |
Output is correct |
20 |
Correct |
499 ms |
87388 KB |
Output is correct |
21 |
Correct |
224 ms |
55040 KB |
Output is correct |
22 |
Correct |
183 ms |
55048 KB |
Output is correct |
23 |
Correct |
332 ms |
62552 KB |
Output is correct |
24 |
Correct |
346 ms |
62816 KB |
Output is correct |
25 |
Correct |
474 ms |
74112 KB |
Output is correct |
26 |
Correct |
408 ms |
70112 KB |
Output is correct |
27 |
Correct |
452 ms |
70040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
34120 KB |
Output is correct |
2 |
Correct |
54 ms |
35404 KB |
Output is correct |
3 |
Correct |
36 ms |
35120 KB |
Output is correct |
4 |
Correct |
53 ms |
35800 KB |
Output is correct |
5 |
Correct |
40 ms |
35848 KB |
Output is correct |
6 |
Correct |
34 ms |
35228 KB |
Output is correct |
7 |
Correct |
29 ms |
34000 KB |
Output is correct |
8 |
Correct |
145 ms |
54432 KB |
Output is correct |
9 |
Correct |
134 ms |
54412 KB |
Output is correct |
10 |
Correct |
23 ms |
34000 KB |
Output is correct |
11 |
Correct |
412 ms |
87552 KB |
Output is correct |
12 |
Correct |
404 ms |
87560 KB |
Output is correct |
13 |
Correct |
332 ms |
96676 KB |
Output is correct |
14 |
Correct |
31 ms |
34176 KB |
Output is correct |
15 |
Correct |
305 ms |
86528 KB |
Output is correct |
16 |
Correct |
411 ms |
79624 KB |
Output is correct |
17 |
Correct |
439 ms |
75496 KB |
Output is correct |
18 |
Correct |
496 ms |
75336 KB |
Output is correct |
19 |
Correct |
484 ms |
87440 KB |
Output is correct |
20 |
Correct |
499 ms |
87388 KB |
Output is correct |
21 |
Correct |
224 ms |
55040 KB |
Output is correct |
22 |
Correct |
183 ms |
55048 KB |
Output is correct |
23 |
Correct |
332 ms |
62552 KB |
Output is correct |
24 |
Correct |
346 ms |
62816 KB |
Output is correct |
25 |
Correct |
474 ms |
74112 KB |
Output is correct |
26 |
Correct |
408 ms |
70112 KB |
Output is correct |
27 |
Correct |
452 ms |
70040 KB |
Output is correct |
28 |
Correct |
39 ms |
33996 KB |
Output is correct |
29 |
Correct |
39 ms |
35060 KB |
Output is correct |
30 |
Correct |
35 ms |
34800 KB |
Output is correct |
31 |
Correct |
57 ms |
35676 KB |
Output is correct |
32 |
Correct |
41 ms |
35600 KB |
Output is correct |
33 |
Correct |
33 ms |
34796 KB |
Output is correct |
34 |
Correct |
44 ms |
34064 KB |
Output is correct |
35 |
Correct |
155 ms |
54220 KB |
Output is correct |
36 |
Correct |
135 ms |
53624 KB |
Output is correct |
37 |
Correct |
161 ms |
53780 KB |
Output is correct |
38 |
Correct |
23 ms |
34076 KB |
Output is correct |
39 |
Correct |
454 ms |
87036 KB |
Output is correct |
40 |
Correct |
351 ms |
95492 KB |
Output is correct |
41 |
Correct |
422 ms |
86736 KB |
Output is correct |
42 |
Correct |
450 ms |
87052 KB |
Output is correct |
43 |
Correct |
27 ms |
33992 KB |
Output is correct |
44 |
Correct |
346 ms |
86272 KB |
Output is correct |
45 |
Correct |
466 ms |
79168 KB |
Output is correct |
46 |
Correct |
481 ms |
75268 KB |
Output is correct |
47 |
Correct |
519 ms |
75104 KB |
Output is correct |
48 |
Correct |
673 ms |
86684 KB |
Output is correct |
49 |
Correct |
859 ms |
90468 KB |
Output is correct |
50 |
Correct |
740 ms |
89896 KB |
Output is correct |
51 |
Correct |
258 ms |
54908 KB |
Output is correct |
52 |
Correct |
173 ms |
54432 KB |
Output is correct |
53 |
Correct |
181 ms |
54236 KB |
Output is correct |
54 |
Correct |
188 ms |
54816 KB |
Output is correct |
55 |
Correct |
216 ms |
54452 KB |
Output is correct |
56 |
Correct |
458 ms |
62828 KB |
Output is correct |
57 |
Correct |
590 ms |
74008 KB |
Output is correct |
58 |
Correct |
640 ms |
70464 KB |
Output is correct |
59 |
Correct |
419 ms |
69468 KB |
Output is correct |