#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000, LG = 20;
vector<int> graph[MAX_N];
int up[MAX_N][LG];
int depth[MAX_N];
void dfs(int u, int p) {
up[u][0] = p;
for (int i = 1; i < LG; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (int v : graph[u]) {
if (v != p) {
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
}
int lca(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
for (int i = LG - 1; i >= 0; i--) {
if (depth[u] - (1 << i) >= depth[v]) {
u = up[u][i];
}
}
if (u == v) {
return u;
}
for (int i = LG - 1; i >= 0; i--) {
if (up[u][i] != up[v][i]) {
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<int> a(m);
for (int &value : a) {
cin >> value;
value--;
}
depth[0] = 0;
dfs(0, 0);
vector<set<int>> S(n), Sv(n);
for (int i = 0; i < m; i++) {
Sv[a[i]].insert(i);
}
for (int i = 0; i < m - 1; i++) {
S[lca(a[i], a[i + 1])].insert(i);
}
while (q--) {
int tp;
cin >> tp;
if (tp == 1) {
int pos, v;
cin >> pos >> v;
pos--, v--;
Sv[a[pos]].erase(pos);
for (int j = max(0, pos - 1); j <= pos && j < m - 1; j++) {
S[lca(a[j], a[j + 1])].erase(j);
}
a[pos] = v;
Sv[a[pos]].insert(pos);
for (int j = max(0, pos - 1); j <= pos && j < m - 1; j++) {
S[lca(a[j], a[j + 1])].insert(j);
}
} else {
int l, r, v;
cin >> l >> r >> v;
l--, v--;
auto itv = Sv[v].lower_bound(l);
if (itv != Sv[v].end() && *itv < r) {
cout << *itv + 1 << " " << *itv + 1 << "\n";
} else {
auto it = S[v].lower_bound(l);
if (it != S[v].end() && *it + 1 < r) {
cout << *it + 1 << " " << *it + 2 << "\n";
} else {
cout << "-1 -1\n";
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
n=5 |
2 |
Correct |
2 ms |
4956 KB |
n=100 |
3 |
Correct |
3 ms |
4956 KB |
n=100 |
4 |
Correct |
3 ms |
5212 KB |
n=100 |
5 |
Correct |
2 ms |
5212 KB |
n=100 |
6 |
Correct |
2 ms |
4956 KB |
n=100 |
7 |
Correct |
2 ms |
4956 KB |
n=100 |
8 |
Correct |
2 ms |
4952 KB |
n=100 |
9 |
Correct |
2 ms |
4956 KB |
n=100 |
10 |
Correct |
2 ms |
4956 KB |
n=100 |
11 |
Correct |
2 ms |
5212 KB |
n=100 |
12 |
Correct |
2 ms |
4956 KB |
n=100 |
13 |
Correct |
2 ms |
4956 KB |
n=100 |
14 |
Correct |
3 ms |
5168 KB |
n=100 |
15 |
Correct |
2 ms |
5212 KB |
n=100 |
16 |
Correct |
2 ms |
4956 KB |
n=100 |
17 |
Correct |
2 ms |
4956 KB |
n=100 |
18 |
Correct |
3 ms |
4956 KB |
n=100 |
19 |
Correct |
2 ms |
5172 KB |
n=100 |
20 |
Correct |
2 ms |
4956 KB |
n=100 |
21 |
Correct |
2 ms |
5208 KB |
n=100 |
22 |
Correct |
2 ms |
4956 KB |
n=100 |
23 |
Correct |
3 ms |
4952 KB |
n=100 |
24 |
Correct |
2 ms |
5168 KB |
n=100 |
25 |
Correct |
2 ms |
4956 KB |
n=100 |
26 |
Correct |
2 ms |
4952 KB |
n=12 |
27 |
Correct |
2 ms |
4956 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
n=5 |
2 |
Correct |
2 ms |
4956 KB |
n=100 |
3 |
Correct |
3 ms |
4956 KB |
n=100 |
4 |
Correct |
3 ms |
5212 KB |
n=100 |
5 |
Correct |
2 ms |
5212 KB |
n=100 |
6 |
Correct |
2 ms |
4956 KB |
n=100 |
7 |
Correct |
2 ms |
4956 KB |
n=100 |
8 |
Correct |
2 ms |
4952 KB |
n=100 |
9 |
Correct |
2 ms |
4956 KB |
n=100 |
10 |
Correct |
2 ms |
4956 KB |
n=100 |
11 |
Correct |
2 ms |
5212 KB |
n=100 |
12 |
Correct |
2 ms |
4956 KB |
n=100 |
13 |
Correct |
2 ms |
4956 KB |
n=100 |
14 |
Correct |
3 ms |
5168 KB |
n=100 |
15 |
Correct |
2 ms |
5212 KB |
n=100 |
16 |
Correct |
2 ms |
4956 KB |
n=100 |
17 |
Correct |
2 ms |
4956 KB |
n=100 |
18 |
Correct |
3 ms |
4956 KB |
n=100 |
19 |
Correct |
2 ms |
5172 KB |
n=100 |
20 |
Correct |
2 ms |
4956 KB |
n=100 |
21 |
Correct |
2 ms |
5208 KB |
n=100 |
22 |
Correct |
2 ms |
4956 KB |
n=100 |
23 |
Correct |
3 ms |
4952 KB |
n=100 |
24 |
Correct |
2 ms |
5168 KB |
n=100 |
25 |
Correct |
2 ms |
4956 KB |
n=100 |
26 |
Correct |
2 ms |
4952 KB |
n=12 |
27 |
Correct |
2 ms |
4956 KB |
n=100 |
28 |
Correct |
3 ms |
5212 KB |
n=500 |
29 |
Correct |
2 ms |
5212 KB |
n=500 |
30 |
Correct |
3 ms |
5212 KB |
n=500 |
31 |
Correct |
3 ms |
5176 KB |
n=500 |
32 |
Correct |
3 ms |
5212 KB |
n=500 |
33 |
Correct |
2 ms |
5180 KB |
n=500 |
34 |
Correct |
3 ms |
5212 KB |
n=500 |
35 |
Correct |
3 ms |
5208 KB |
n=500 |
36 |
Correct |
3 ms |
5212 KB |
n=500 |
37 |
Correct |
2 ms |
5136 KB |
n=500 |
38 |
Correct |
3 ms |
5208 KB |
n=500 |
39 |
Correct |
2 ms |
5212 KB |
n=500 |
40 |
Correct |
2 ms |
5212 KB |
n=500 |
41 |
Correct |
2 ms |
5208 KB |
n=500 |
42 |
Correct |
2 ms |
5212 KB |
n=500 |
43 |
Correct |
3 ms |
5212 KB |
n=500 |
44 |
Correct |
3 ms |
5212 KB |
n=500 |
45 |
Correct |
3 ms |
5208 KB |
n=500 |
46 |
Correct |
3 ms |
5208 KB |
n=500 |
47 |
Correct |
3 ms |
5212 KB |
n=500 |
48 |
Correct |
3 ms |
5340 KB |
n=500 |
49 |
Correct |
3 ms |
5212 KB |
n=500 |
50 |
Correct |
2 ms |
5212 KB |
n=500 |
51 |
Correct |
2 ms |
5212 KB |
n=500 |
52 |
Correct |
2 ms |
5212 KB |
n=500 |
53 |
Correct |
3 ms |
5212 KB |
n=500 |
54 |
Correct |
2 ms |
5212 KB |
n=500 |
55 |
Correct |
2 ms |
5212 KB |
n=278 |
56 |
Correct |
3 ms |
5212 KB |
n=500 |
57 |
Correct |
3 ms |
5212 KB |
n=500 |
58 |
Correct |
3 ms |
5212 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
n=5 |
2 |
Correct |
2 ms |
4956 KB |
n=100 |
3 |
Correct |
3 ms |
4956 KB |
n=100 |
4 |
Correct |
3 ms |
5212 KB |
n=100 |
5 |
Correct |
2 ms |
5212 KB |
n=100 |
6 |
Correct |
2 ms |
4956 KB |
n=100 |
7 |
Correct |
2 ms |
4956 KB |
n=100 |
8 |
Correct |
2 ms |
4952 KB |
n=100 |
9 |
Correct |
2 ms |
4956 KB |
n=100 |
10 |
Correct |
2 ms |
4956 KB |
n=100 |
11 |
Correct |
2 ms |
5212 KB |
n=100 |
12 |
Correct |
2 ms |
4956 KB |
n=100 |
13 |
Correct |
2 ms |
4956 KB |
n=100 |
14 |
Correct |
3 ms |
5168 KB |
n=100 |
15 |
Correct |
2 ms |
5212 KB |
n=100 |
16 |
Correct |
2 ms |
4956 KB |
n=100 |
17 |
Correct |
2 ms |
4956 KB |
n=100 |
18 |
Correct |
3 ms |
4956 KB |
n=100 |
19 |
Correct |
2 ms |
5172 KB |
n=100 |
20 |
Correct |
2 ms |
4956 KB |
n=100 |
21 |
Correct |
2 ms |
5208 KB |
n=100 |
22 |
Correct |
2 ms |
4956 KB |
n=100 |
23 |
Correct |
3 ms |
4952 KB |
n=100 |
24 |
Correct |
2 ms |
5168 KB |
n=100 |
25 |
Correct |
2 ms |
4956 KB |
n=100 |
26 |
Correct |
2 ms |
4952 KB |
n=12 |
27 |
Correct |
2 ms |
4956 KB |
n=100 |
28 |
Correct |
3 ms |
5212 KB |
n=500 |
29 |
Correct |
2 ms |
5212 KB |
n=500 |
30 |
Correct |
3 ms |
5212 KB |
n=500 |
31 |
Correct |
3 ms |
5176 KB |
n=500 |
32 |
Correct |
3 ms |
5212 KB |
n=500 |
33 |
Correct |
2 ms |
5180 KB |
n=500 |
34 |
Correct |
3 ms |
5212 KB |
n=500 |
35 |
Correct |
3 ms |
5208 KB |
n=500 |
36 |
Correct |
3 ms |
5212 KB |
n=500 |
37 |
Correct |
2 ms |
5136 KB |
n=500 |
38 |
Correct |
3 ms |
5208 KB |
n=500 |
39 |
Correct |
2 ms |
5212 KB |
n=500 |
40 |
Correct |
2 ms |
5212 KB |
n=500 |
41 |
Correct |
2 ms |
5208 KB |
n=500 |
42 |
Correct |
2 ms |
5212 KB |
n=500 |
43 |
Correct |
3 ms |
5212 KB |
n=500 |
44 |
Correct |
3 ms |
5212 KB |
n=500 |
45 |
Correct |
3 ms |
5208 KB |
n=500 |
46 |
Correct |
3 ms |
5208 KB |
n=500 |
47 |
Correct |
3 ms |
5212 KB |
n=500 |
48 |
Correct |
3 ms |
5340 KB |
n=500 |
49 |
Correct |
3 ms |
5212 KB |
n=500 |
50 |
Correct |
2 ms |
5212 KB |
n=500 |
51 |
Correct |
2 ms |
5212 KB |
n=500 |
52 |
Correct |
2 ms |
5212 KB |
n=500 |
53 |
Correct |
3 ms |
5212 KB |
n=500 |
54 |
Correct |
2 ms |
5212 KB |
n=500 |
55 |
Correct |
2 ms |
5212 KB |
n=278 |
56 |
Correct |
3 ms |
5212 KB |
n=500 |
57 |
Correct |
3 ms |
5212 KB |
n=500 |
58 |
Correct |
3 ms |
5212 KB |
n=500 |
59 |
Correct |
4 ms |
5720 KB |
n=2000 |
60 |
Correct |
5 ms |
5724 KB |
n=2000 |
61 |
Correct |
4 ms |
5724 KB |
n=2000 |
62 |
Correct |
5 ms |
5876 KB |
n=2000 |
63 |
Correct |
4 ms |
5724 KB |
n=2000 |
64 |
Correct |
5 ms |
5724 KB |
n=2000 |
65 |
Correct |
4 ms |
5808 KB |
n=2000 |
66 |
Correct |
5 ms |
5724 KB |
n=2000 |
67 |
Correct |
5 ms |
5724 KB |
n=2000 |
68 |
Correct |
5 ms |
5724 KB |
n=2000 |
69 |
Correct |
4 ms |
5724 KB |
n=2000 |
70 |
Correct |
4 ms |
5724 KB |
n=2000 |
71 |
Correct |
4 ms |
5724 KB |
n=2000 |
72 |
Correct |
3 ms |
5724 KB |
n=2000 |
73 |
Correct |
4 ms |
5724 KB |
n=2000 |
74 |
Correct |
4 ms |
5724 KB |
n=1844 |
75 |
Correct |
4 ms |
5724 KB |
n=2000 |
76 |
Correct |
4 ms |
5724 KB |
n=2000 |
77 |
Correct |
4 ms |
5728 KB |
n=2000 |
78 |
Correct |
5 ms |
5720 KB |
n=2000 |
79 |
Correct |
4 ms |
5720 KB |
n=2000 |
80 |
Correct |
4 ms |
5724 KB |
n=2000 |
81 |
Correct |
5 ms |
5724 KB |
n=2000 |
82 |
Correct |
4 ms |
5728 KB |
n=2000 |
83 |
Correct |
4 ms |
5724 KB |
n=2000 |
84 |
Correct |
4 ms |
5724 KB |
n=2000 |
85 |
Correct |
5 ms |
5720 KB |
n=2000 |
86 |
Correct |
5 ms |
5724 KB |
n=2000 |
87 |
Correct |
4 ms |
5724 KB |
n=2000 |
88 |
Correct |
4 ms |
5724 KB |
n=2000 |
89 |
Correct |
4 ms |
5864 KB |
n=2000 |
90 |
Correct |
4 ms |
5724 KB |
n=2000 |
91 |
Correct |
4 ms |
5720 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
n=5 |
2 |
Correct |
2 ms |
4956 KB |
n=100 |
3 |
Correct |
3 ms |
4956 KB |
n=100 |
4 |
Correct |
3 ms |
5212 KB |
n=100 |
5 |
Correct |
2 ms |
5212 KB |
n=100 |
6 |
Correct |
2 ms |
4956 KB |
n=100 |
7 |
Correct |
2 ms |
4956 KB |
n=100 |
8 |
Correct |
2 ms |
4952 KB |
n=100 |
9 |
Correct |
2 ms |
4956 KB |
n=100 |
10 |
Correct |
2 ms |
4956 KB |
n=100 |
11 |
Correct |
2 ms |
5212 KB |
n=100 |
12 |
Correct |
2 ms |
4956 KB |
n=100 |
13 |
Correct |
2 ms |
4956 KB |
n=100 |
14 |
Correct |
3 ms |
5168 KB |
n=100 |
15 |
Correct |
2 ms |
5212 KB |
n=100 |
16 |
Correct |
2 ms |
4956 KB |
n=100 |
17 |
Correct |
2 ms |
4956 KB |
n=100 |
18 |
Correct |
3 ms |
4956 KB |
n=100 |
19 |
Correct |
2 ms |
5172 KB |
n=100 |
20 |
Correct |
2 ms |
4956 KB |
n=100 |
21 |
Correct |
2 ms |
5208 KB |
n=100 |
22 |
Correct |
2 ms |
4956 KB |
n=100 |
23 |
Correct |
3 ms |
4952 KB |
n=100 |
24 |
Correct |
2 ms |
5168 KB |
n=100 |
25 |
Correct |
2 ms |
4956 KB |
n=100 |
26 |
Correct |
2 ms |
4952 KB |
n=12 |
27 |
Correct |
2 ms |
4956 KB |
n=100 |
28 |
Correct |
3 ms |
5212 KB |
n=500 |
29 |
Correct |
2 ms |
5212 KB |
n=500 |
30 |
Correct |
3 ms |
5212 KB |
n=500 |
31 |
Correct |
3 ms |
5176 KB |
n=500 |
32 |
Correct |
3 ms |
5212 KB |
n=500 |
33 |
Correct |
2 ms |
5180 KB |
n=500 |
34 |
Correct |
3 ms |
5212 KB |
n=500 |
35 |
Correct |
3 ms |
5208 KB |
n=500 |
36 |
Correct |
3 ms |
5212 KB |
n=500 |
37 |
Correct |
2 ms |
5136 KB |
n=500 |
38 |
Correct |
3 ms |
5208 KB |
n=500 |
39 |
Correct |
2 ms |
5212 KB |
n=500 |
40 |
Correct |
2 ms |
5212 KB |
n=500 |
41 |
Correct |
2 ms |
5208 KB |
n=500 |
42 |
Correct |
2 ms |
5212 KB |
n=500 |
43 |
Correct |
3 ms |
5212 KB |
n=500 |
44 |
Correct |
3 ms |
5212 KB |
n=500 |
45 |
Correct |
3 ms |
5208 KB |
n=500 |
46 |
Correct |
3 ms |
5208 KB |
n=500 |
47 |
Correct |
3 ms |
5212 KB |
n=500 |
48 |
Correct |
3 ms |
5340 KB |
n=500 |
49 |
Correct |
3 ms |
5212 KB |
n=500 |
50 |
Correct |
2 ms |
5212 KB |
n=500 |
51 |
Correct |
2 ms |
5212 KB |
n=500 |
52 |
Correct |
2 ms |
5212 KB |
n=500 |
53 |
Correct |
3 ms |
5212 KB |
n=500 |
54 |
Correct |
2 ms |
5212 KB |
n=500 |
55 |
Correct |
2 ms |
5212 KB |
n=278 |
56 |
Correct |
3 ms |
5212 KB |
n=500 |
57 |
Correct |
3 ms |
5212 KB |
n=500 |
58 |
Correct |
3 ms |
5212 KB |
n=500 |
59 |
Correct |
4 ms |
5720 KB |
n=2000 |
60 |
Correct |
5 ms |
5724 KB |
n=2000 |
61 |
Correct |
4 ms |
5724 KB |
n=2000 |
62 |
Correct |
5 ms |
5876 KB |
n=2000 |
63 |
Correct |
4 ms |
5724 KB |
n=2000 |
64 |
Correct |
5 ms |
5724 KB |
n=2000 |
65 |
Correct |
4 ms |
5808 KB |
n=2000 |
66 |
Correct |
5 ms |
5724 KB |
n=2000 |
67 |
Correct |
5 ms |
5724 KB |
n=2000 |
68 |
Correct |
5 ms |
5724 KB |
n=2000 |
69 |
Correct |
4 ms |
5724 KB |
n=2000 |
70 |
Correct |
4 ms |
5724 KB |
n=2000 |
71 |
Correct |
4 ms |
5724 KB |
n=2000 |
72 |
Correct |
3 ms |
5724 KB |
n=2000 |
73 |
Correct |
4 ms |
5724 KB |
n=2000 |
74 |
Correct |
4 ms |
5724 KB |
n=1844 |
75 |
Correct |
4 ms |
5724 KB |
n=2000 |
76 |
Correct |
4 ms |
5724 KB |
n=2000 |
77 |
Correct |
4 ms |
5728 KB |
n=2000 |
78 |
Correct |
5 ms |
5720 KB |
n=2000 |
79 |
Correct |
4 ms |
5720 KB |
n=2000 |
80 |
Correct |
4 ms |
5724 KB |
n=2000 |
81 |
Correct |
5 ms |
5724 KB |
n=2000 |
82 |
Correct |
4 ms |
5728 KB |
n=2000 |
83 |
Correct |
4 ms |
5724 KB |
n=2000 |
84 |
Correct |
4 ms |
5724 KB |
n=2000 |
85 |
Correct |
5 ms |
5720 KB |
n=2000 |
86 |
Correct |
5 ms |
5724 KB |
n=2000 |
87 |
Correct |
4 ms |
5724 KB |
n=2000 |
88 |
Correct |
4 ms |
5724 KB |
n=2000 |
89 |
Correct |
4 ms |
5864 KB |
n=2000 |
90 |
Correct |
4 ms |
5724 KB |
n=2000 |
91 |
Correct |
4 ms |
5720 KB |
n=2000 |
92 |
Correct |
405 ms |
74688 KB |
n=200000 |
93 |
Correct |
514 ms |
80916 KB |
n=200000 |
94 |
Correct |
582 ms |
85588 KB |
n=200000 |
95 |
Correct |
400 ms |
74432 KB |
n=200000 |
96 |
Correct |
389 ms |
74440 KB |
n=200000 |
97 |
Correct |
521 ms |
79700 KB |
n=200000 |
98 |
Correct |
387 ms |
74480 KB |
n=200000 |
99 |
Correct |
448 ms |
74580 KB |
n=200000 |
100 |
Correct |
401 ms |
74440 KB |
n=200000 |
101 |
Correct |
512 ms |
87052 KB |
n=200000 |
102 |
Correct |
237 ms |
75600 KB |
n=200000 |
103 |
Correct |
228 ms |
75512 KB |
n=200000 |
104 |
Correct |
232 ms |
75604 KB |
n=200000 |
105 |
Correct |
226 ms |
76040 KB |
n=200000 |
106 |
Correct |
243 ms |
76092 KB |
n=200000 |
107 |
Correct |
230 ms |
76112 KB |
n=200000 |
108 |
Correct |
432 ms |
74580 KB |
n=200000 |
109 |
Correct |
435 ms |
74560 KB |
n=200000 |
110 |
Correct |
435 ms |
74408 KB |
n=200000 |
111 |
Correct |
384 ms |
73924 KB |
n=200000 |
112 |
Correct |
529 ms |
85840 KB |
n=200000 |
113 |
Correct |
529 ms |
79956 KB |
n=200000 |
114 |
Correct |
392 ms |
73932 KB |
n=200000 |
115 |
Correct |
535 ms |
77224 KB |
n=200000 |
116 |
Correct |
378 ms |
74564 KB |
n=200000 |
117 |
Correct |
506 ms |
86356 KB |
n=200000 |
118 |
Correct |
510 ms |
78416 KB |
n=200000 |
119 |
Correct |
387 ms |
74436 KB |
n=200000 |
120 |
Correct |
481 ms |
86136 KB |
n=200000 |
121 |
Correct |
477 ms |
86096 KB |
n=200000 |
122 |
Correct |
491 ms |
86400 KB |
n=200000 |
123 |
Correct |
219 ms |
75856 KB |
n=200000 |
124 |
Correct |
108 ms |
22776 KB |
n=25264 |