#include <bits/stdc++.h>
using namespace std;
vector<int> d;
vector<vector<int>> E, sp;
void dfs(int x, int p = -1) {
sp[x][0] = p;
if (p != -1) d[x] = d[p] + 1;
for (auto i : E[x])
if (i != p) dfs(i, x);
}
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
for (int i = 19; i >= 0; i--)
if (d[x] - (1 << i) >= d[y]) x = sp[x][i];
if (x == y) return x;
for (int i = 19; i >= 0; i--) {
if (sp[x][i] != sp[y][i]) {
x = sp[x][i]; y = sp[y][i];
}
}
return sp[x][0];
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
E.assign(n, {});
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y; x--; y--;
E[x].push_back(y);
E[y].push_back(x);
}
d.assign(n, 0);
sp.assign(n, vector<int>(20, -1));
dfs(0);
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++)
if (sp[j][i - 1] != -1) sp[j][i] = sp[sp[j][i - 1]][i - 1];
}
vector<int> a(m);
for (auto &i : a) {
cin >> i; i--;
}
while (q--) {
int tp;
cin >> tp;
if (tp == 1) {
int ps, v;
cin >> ps >> v; ps--; v--;
a[ps] = v;
}
else {
int l, r, v;
cin >> l >> r >> v; l--; r--; v--;
pair<int, int> ans = {-2, -2};
for (int i = l; i <= r; i++) {
if (a[i] == v) {
ans = {i, i};
break;
}
if (i != r && lca(a[i], a[i + 1]) == v) {
ans = {i, i + 1};
break;
}
}
cout << ans.first + 1 << ' ' << ans.second + 1 << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=5 |
2 |
Correct |
1 ms |
336 KB |
n=100 |
3 |
Correct |
1 ms |
336 KB |
n=100 |
4 |
Correct |
1 ms |
336 KB |
n=100 |
5 |
Correct |
1 ms |
336 KB |
n=100 |
6 |
Correct |
1 ms |
336 KB |
n=100 |
7 |
Correct |
1 ms |
336 KB |
n=100 |
8 |
Correct |
1 ms |
336 KB |
n=100 |
9 |
Correct |
1 ms |
336 KB |
n=100 |
10 |
Correct |
1 ms |
504 KB |
n=100 |
11 |
Correct |
1 ms |
336 KB |
n=100 |
12 |
Correct |
1 ms |
336 KB |
n=100 |
13 |
Correct |
1 ms |
336 KB |
n=100 |
14 |
Correct |
1 ms |
336 KB |
n=100 |
15 |
Correct |
1 ms |
336 KB |
n=100 |
16 |
Correct |
1 ms |
504 KB |
n=100 |
17 |
Correct |
1 ms |
336 KB |
n=100 |
18 |
Correct |
1 ms |
336 KB |
n=100 |
19 |
Correct |
1 ms |
336 KB |
n=100 |
20 |
Correct |
1 ms |
336 KB |
n=100 |
21 |
Correct |
1 ms |
504 KB |
n=100 |
22 |
Correct |
1 ms |
508 KB |
n=100 |
23 |
Correct |
1 ms |
336 KB |
n=100 |
24 |
Correct |
1 ms |
336 KB |
n=100 |
25 |
Correct |
1 ms |
504 KB |
n=100 |
26 |
Correct |
1 ms |
336 KB |
n=12 |
27 |
Correct |
1 ms |
336 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=5 |
2 |
Correct |
1 ms |
336 KB |
n=100 |
3 |
Correct |
1 ms |
336 KB |
n=100 |
4 |
Correct |
1 ms |
336 KB |
n=100 |
5 |
Correct |
1 ms |
336 KB |
n=100 |
6 |
Correct |
1 ms |
336 KB |
n=100 |
7 |
Correct |
1 ms |
336 KB |
n=100 |
8 |
Correct |
1 ms |
336 KB |
n=100 |
9 |
Correct |
1 ms |
336 KB |
n=100 |
10 |
Correct |
1 ms |
504 KB |
n=100 |
11 |
Correct |
1 ms |
336 KB |
n=100 |
12 |
Correct |
1 ms |
336 KB |
n=100 |
13 |
Correct |
1 ms |
336 KB |
n=100 |
14 |
Correct |
1 ms |
336 KB |
n=100 |
15 |
Correct |
1 ms |
336 KB |
n=100 |
16 |
Correct |
1 ms |
504 KB |
n=100 |
17 |
Correct |
1 ms |
336 KB |
n=100 |
18 |
Correct |
1 ms |
336 KB |
n=100 |
19 |
Correct |
1 ms |
336 KB |
n=100 |
20 |
Correct |
1 ms |
336 KB |
n=100 |
21 |
Correct |
1 ms |
504 KB |
n=100 |
22 |
Correct |
1 ms |
508 KB |
n=100 |
23 |
Correct |
1 ms |
336 KB |
n=100 |
24 |
Correct |
1 ms |
336 KB |
n=100 |
25 |
Correct |
1 ms |
504 KB |
n=100 |
26 |
Correct |
1 ms |
336 KB |
n=12 |
27 |
Correct |
1 ms |
336 KB |
n=100 |
28 |
Correct |
1 ms |
336 KB |
n=500 |
29 |
Correct |
2 ms |
592 KB |
n=500 |
30 |
Correct |
2 ms |
504 KB |
n=500 |
31 |
Correct |
2 ms |
336 KB |
n=500 |
32 |
Correct |
1 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
336 KB |
n=500 |
34 |
Correct |
1 ms |
336 KB |
n=500 |
35 |
Correct |
3 ms |
336 KB |
n=500 |
36 |
Correct |
7 ms |
336 KB |
n=500 |
37 |
Correct |
7 ms |
336 KB |
n=500 |
38 |
Correct |
7 ms |
336 KB |
n=500 |
39 |
Correct |
4 ms |
336 KB |
n=500 |
40 |
Correct |
5 ms |
336 KB |
n=500 |
41 |
Correct |
6 ms |
336 KB |
n=500 |
42 |
Correct |
5 ms |
336 KB |
n=500 |
43 |
Correct |
4 ms |
336 KB |
n=500 |
44 |
Correct |
6 ms |
336 KB |
n=500 |
45 |
Correct |
2 ms |
504 KB |
n=500 |
46 |
Correct |
2 ms |
336 KB |
n=500 |
47 |
Correct |
2 ms |
504 KB |
n=500 |
48 |
Correct |
1 ms |
336 KB |
n=500 |
49 |
Correct |
1 ms |
336 KB |
n=500 |
50 |
Correct |
4 ms |
336 KB |
n=500 |
51 |
Correct |
3 ms |
336 KB |
n=500 |
52 |
Correct |
2 ms |
336 KB |
n=500 |
53 |
Correct |
2 ms |
336 KB |
n=500 |
54 |
Correct |
3 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
336 KB |
n=278 |
56 |
Correct |
4 ms |
508 KB |
n=500 |
57 |
Correct |
4 ms |
584 KB |
n=500 |
58 |
Correct |
8 ms |
336 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=5 |
2 |
Correct |
1 ms |
336 KB |
n=100 |
3 |
Correct |
1 ms |
336 KB |
n=100 |
4 |
Correct |
1 ms |
336 KB |
n=100 |
5 |
Correct |
1 ms |
336 KB |
n=100 |
6 |
Correct |
1 ms |
336 KB |
n=100 |
7 |
Correct |
1 ms |
336 KB |
n=100 |
8 |
Correct |
1 ms |
336 KB |
n=100 |
9 |
Correct |
1 ms |
336 KB |
n=100 |
10 |
Correct |
1 ms |
504 KB |
n=100 |
11 |
Correct |
1 ms |
336 KB |
n=100 |
12 |
Correct |
1 ms |
336 KB |
n=100 |
13 |
Correct |
1 ms |
336 KB |
n=100 |
14 |
Correct |
1 ms |
336 KB |
n=100 |
15 |
Correct |
1 ms |
336 KB |
n=100 |
16 |
Correct |
1 ms |
504 KB |
n=100 |
17 |
Correct |
1 ms |
336 KB |
n=100 |
18 |
Correct |
1 ms |
336 KB |
n=100 |
19 |
Correct |
1 ms |
336 KB |
n=100 |
20 |
Correct |
1 ms |
336 KB |
n=100 |
21 |
Correct |
1 ms |
504 KB |
n=100 |
22 |
Correct |
1 ms |
508 KB |
n=100 |
23 |
Correct |
1 ms |
336 KB |
n=100 |
24 |
Correct |
1 ms |
336 KB |
n=100 |
25 |
Correct |
1 ms |
504 KB |
n=100 |
26 |
Correct |
1 ms |
336 KB |
n=12 |
27 |
Correct |
1 ms |
336 KB |
n=100 |
28 |
Correct |
1 ms |
336 KB |
n=500 |
29 |
Correct |
2 ms |
592 KB |
n=500 |
30 |
Correct |
2 ms |
504 KB |
n=500 |
31 |
Correct |
2 ms |
336 KB |
n=500 |
32 |
Correct |
1 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
336 KB |
n=500 |
34 |
Correct |
1 ms |
336 KB |
n=500 |
35 |
Correct |
3 ms |
336 KB |
n=500 |
36 |
Correct |
7 ms |
336 KB |
n=500 |
37 |
Correct |
7 ms |
336 KB |
n=500 |
38 |
Correct |
7 ms |
336 KB |
n=500 |
39 |
Correct |
4 ms |
336 KB |
n=500 |
40 |
Correct |
5 ms |
336 KB |
n=500 |
41 |
Correct |
6 ms |
336 KB |
n=500 |
42 |
Correct |
5 ms |
336 KB |
n=500 |
43 |
Correct |
4 ms |
336 KB |
n=500 |
44 |
Correct |
6 ms |
336 KB |
n=500 |
45 |
Correct |
2 ms |
504 KB |
n=500 |
46 |
Correct |
2 ms |
336 KB |
n=500 |
47 |
Correct |
2 ms |
504 KB |
n=500 |
48 |
Correct |
1 ms |
336 KB |
n=500 |
49 |
Correct |
1 ms |
336 KB |
n=500 |
50 |
Correct |
4 ms |
336 KB |
n=500 |
51 |
Correct |
3 ms |
336 KB |
n=500 |
52 |
Correct |
2 ms |
336 KB |
n=500 |
53 |
Correct |
2 ms |
336 KB |
n=500 |
54 |
Correct |
3 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
336 KB |
n=278 |
56 |
Correct |
4 ms |
508 KB |
n=500 |
57 |
Correct |
4 ms |
584 KB |
n=500 |
58 |
Correct |
8 ms |
336 KB |
n=500 |
59 |
Correct |
3 ms |
1016 KB |
n=2000 |
60 |
Correct |
31 ms |
848 KB |
n=2000 |
61 |
Correct |
24 ms |
848 KB |
n=2000 |
62 |
Correct |
17 ms |
848 KB |
n=2000 |
63 |
Correct |
2 ms |
848 KB |
n=2000 |
64 |
Correct |
21 ms |
848 KB |
n=2000 |
65 |
Correct |
2 ms |
848 KB |
n=2000 |
66 |
Correct |
26 ms |
848 KB |
n=2000 |
67 |
Correct |
3 ms |
848 KB |
n=2000 |
68 |
Correct |
22 ms |
912 KB |
n=2000 |
69 |
Correct |
116 ms |
880 KB |
n=2000 |
70 |
Correct |
107 ms |
848 KB |
n=2000 |
71 |
Correct |
106 ms |
848 KB |
n=2000 |
72 |
Correct |
60 ms |
848 KB |
n=2000 |
73 |
Correct |
60 ms |
872 KB |
n=2000 |
74 |
Correct |
2 ms |
848 KB |
n=1844 |
75 |
Correct |
65 ms |
868 KB |
n=2000 |
76 |
Correct |
56 ms |
864 KB |
n=2000 |
77 |
Correct |
55 ms |
592 KB |
n=2000 |
78 |
Correct |
57 ms |
864 KB |
n=2000 |
79 |
Correct |
2 ms |
848 KB |
n=2000 |
80 |
Correct |
24 ms |
944 KB |
n=2000 |
81 |
Correct |
20 ms |
848 KB |
n=2000 |
82 |
Correct |
3 ms |
732 KB |
n=2000 |
83 |
Correct |
28 ms |
860 KB |
n=2000 |
84 |
Correct |
24 ms |
880 KB |
n=2000 |
85 |
Correct |
56 ms |
904 KB |
n=2000 |
86 |
Correct |
47 ms |
848 KB |
n=2000 |
87 |
Correct |
25 ms |
884 KB |
n=2000 |
88 |
Correct |
146 ms |
848 KB |
n=2000 |
89 |
Correct |
143 ms |
848 KB |
n=2000 |
90 |
Correct |
67 ms |
1016 KB |
n=2000 |
91 |
Correct |
84 ms |
864 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=5 |
2 |
Correct |
1 ms |
336 KB |
n=100 |
3 |
Correct |
1 ms |
336 KB |
n=100 |
4 |
Correct |
1 ms |
336 KB |
n=100 |
5 |
Correct |
1 ms |
336 KB |
n=100 |
6 |
Correct |
1 ms |
336 KB |
n=100 |
7 |
Correct |
1 ms |
336 KB |
n=100 |
8 |
Correct |
1 ms |
336 KB |
n=100 |
9 |
Correct |
1 ms |
336 KB |
n=100 |
10 |
Correct |
1 ms |
504 KB |
n=100 |
11 |
Correct |
1 ms |
336 KB |
n=100 |
12 |
Correct |
1 ms |
336 KB |
n=100 |
13 |
Correct |
1 ms |
336 KB |
n=100 |
14 |
Correct |
1 ms |
336 KB |
n=100 |
15 |
Correct |
1 ms |
336 KB |
n=100 |
16 |
Correct |
1 ms |
504 KB |
n=100 |
17 |
Correct |
1 ms |
336 KB |
n=100 |
18 |
Correct |
1 ms |
336 KB |
n=100 |
19 |
Correct |
1 ms |
336 KB |
n=100 |
20 |
Correct |
1 ms |
336 KB |
n=100 |
21 |
Correct |
1 ms |
504 KB |
n=100 |
22 |
Correct |
1 ms |
508 KB |
n=100 |
23 |
Correct |
1 ms |
336 KB |
n=100 |
24 |
Correct |
1 ms |
336 KB |
n=100 |
25 |
Correct |
1 ms |
504 KB |
n=100 |
26 |
Correct |
1 ms |
336 KB |
n=12 |
27 |
Correct |
1 ms |
336 KB |
n=100 |
28 |
Correct |
1 ms |
336 KB |
n=500 |
29 |
Correct |
2 ms |
592 KB |
n=500 |
30 |
Correct |
2 ms |
504 KB |
n=500 |
31 |
Correct |
2 ms |
336 KB |
n=500 |
32 |
Correct |
1 ms |
504 KB |
n=500 |
33 |
Correct |
3 ms |
336 KB |
n=500 |
34 |
Correct |
1 ms |
336 KB |
n=500 |
35 |
Correct |
3 ms |
336 KB |
n=500 |
36 |
Correct |
7 ms |
336 KB |
n=500 |
37 |
Correct |
7 ms |
336 KB |
n=500 |
38 |
Correct |
7 ms |
336 KB |
n=500 |
39 |
Correct |
4 ms |
336 KB |
n=500 |
40 |
Correct |
5 ms |
336 KB |
n=500 |
41 |
Correct |
6 ms |
336 KB |
n=500 |
42 |
Correct |
5 ms |
336 KB |
n=500 |
43 |
Correct |
4 ms |
336 KB |
n=500 |
44 |
Correct |
6 ms |
336 KB |
n=500 |
45 |
Correct |
2 ms |
504 KB |
n=500 |
46 |
Correct |
2 ms |
336 KB |
n=500 |
47 |
Correct |
2 ms |
504 KB |
n=500 |
48 |
Correct |
1 ms |
336 KB |
n=500 |
49 |
Correct |
1 ms |
336 KB |
n=500 |
50 |
Correct |
4 ms |
336 KB |
n=500 |
51 |
Correct |
3 ms |
336 KB |
n=500 |
52 |
Correct |
2 ms |
336 KB |
n=500 |
53 |
Correct |
2 ms |
336 KB |
n=500 |
54 |
Correct |
3 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
336 KB |
n=278 |
56 |
Correct |
4 ms |
508 KB |
n=500 |
57 |
Correct |
4 ms |
584 KB |
n=500 |
58 |
Correct |
8 ms |
336 KB |
n=500 |
59 |
Correct |
3 ms |
1016 KB |
n=2000 |
60 |
Correct |
31 ms |
848 KB |
n=2000 |
61 |
Correct |
24 ms |
848 KB |
n=2000 |
62 |
Correct |
17 ms |
848 KB |
n=2000 |
63 |
Correct |
2 ms |
848 KB |
n=2000 |
64 |
Correct |
21 ms |
848 KB |
n=2000 |
65 |
Correct |
2 ms |
848 KB |
n=2000 |
66 |
Correct |
26 ms |
848 KB |
n=2000 |
67 |
Correct |
3 ms |
848 KB |
n=2000 |
68 |
Correct |
22 ms |
912 KB |
n=2000 |
69 |
Correct |
116 ms |
880 KB |
n=2000 |
70 |
Correct |
107 ms |
848 KB |
n=2000 |
71 |
Correct |
106 ms |
848 KB |
n=2000 |
72 |
Correct |
60 ms |
848 KB |
n=2000 |
73 |
Correct |
60 ms |
872 KB |
n=2000 |
74 |
Correct |
2 ms |
848 KB |
n=1844 |
75 |
Correct |
65 ms |
868 KB |
n=2000 |
76 |
Correct |
56 ms |
864 KB |
n=2000 |
77 |
Correct |
55 ms |
592 KB |
n=2000 |
78 |
Correct |
57 ms |
864 KB |
n=2000 |
79 |
Correct |
2 ms |
848 KB |
n=2000 |
80 |
Correct |
24 ms |
944 KB |
n=2000 |
81 |
Correct |
20 ms |
848 KB |
n=2000 |
82 |
Correct |
3 ms |
732 KB |
n=2000 |
83 |
Correct |
28 ms |
860 KB |
n=2000 |
84 |
Correct |
24 ms |
880 KB |
n=2000 |
85 |
Correct |
56 ms |
904 KB |
n=2000 |
86 |
Correct |
47 ms |
848 KB |
n=2000 |
87 |
Correct |
25 ms |
884 KB |
n=2000 |
88 |
Correct |
146 ms |
848 KB |
n=2000 |
89 |
Correct |
143 ms |
848 KB |
n=2000 |
90 |
Correct |
67 ms |
1016 KB |
n=2000 |
91 |
Correct |
84 ms |
864 KB |
n=2000 |
92 |
Correct |
377 ms |
41892 KB |
n=200000 |
93 |
Execution timed out |
4054 ms |
44416 KB |
Time limit exceeded |
94 |
Halted |
0 ms |
0 KB |
- |