#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAXK = 20;
vector<int> graph[MAXN];
int dp[MAXN][MAXK];
int as[MAXN], prof[MAXN];
set<int> pos[MAXN], pos_lca[MAXN];
void dfs(int u, int p) {
for (int v: graph[u]) {
if (v == p) continue;
prof[v] = prof[u] + 1;
dp[v][0] = u;
for (int k = 1; k < MAXK; ++k)
dp[v][k] = dp[dp[v][k-1]][k-1];
dfs(v, u);
}
}
int lca(int a, int b) {
if (prof[a] > prof[b]) swap(a, b);
int d = prof[b] - prof[a];
for (int k = 0; k < MAXK; ++k)
if (d & (1 << k)) b = dp[b][k];
if (a == b) return a;
for (int k = MAXK - 1; k >= 0; --k)
if (dp[a][k] != dp[b][k]) {
a = dp[a][k];
b = dp[b][k];
}
return dp[a][0];
}
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n-1; ++i) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(1, 1);
for (int i = 1; i <= m; ++i)
cin >> as[i];
for (int i = 1; i <= m; ++i) {
pos[as[i]].insert(i);
if (i < m)
pos_lca[lca(as[i], as[i+1])].insert(i);
}
for (int qq = 0; qq < q; ++qq) {
int t;
cin >> t;
if (t == 1) {
int i, v;
cin >> i >> v;
pos[as[i]].erase(i);
if (i > 1) pos_lca[lca(as[i-1], as[i])].erase(i-1);
if (i < m) pos_lca[lca(as[i], as[i+1])].erase(i);
as[i] = v;
pos[as[i]].insert(i);
if (i > 1) pos_lca[lca(as[i-1], as[i])].insert(i-1);
if (i < m) pos_lca[lca(as[i], as[i+1])].insert(i);
}
else {
int l, r, v;
cin >> l >> r >> v;
auto b = lower_bound(pos[v].begin(), pos[v].end(), l);
auto e = upper_bound(pos[v].begin(), pos[v].end(), r);
if (b != e) {
cout << *b << " " << *b << "\n";
continue;
}
auto blca = lower_bound(pos_lca[v].begin(), pos_lca[v].end(), l);
auto elca = upper_bound(pos_lca[v].begin(), pos_lca[v].end(), r);
if (blca != elca && *blca + 1 <= r) {
cout << *blca << " " << *blca + 1 << "\n";
continue;
}
cout << "-1 -1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23792 KB |
n=5 |
2 |
Correct |
12 ms |
23792 KB |
n=100 |
3 |
Correct |
12 ms |
23816 KB |
n=100 |
4 |
Correct |
12 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23800 KB |
n=100 |
6 |
Correct |
13 ms |
23800 KB |
n=100 |
7 |
Correct |
15 ms |
23780 KB |
n=100 |
8 |
Correct |
13 ms |
23792 KB |
n=100 |
9 |
Correct |
13 ms |
23744 KB |
n=100 |
10 |
Correct |
12 ms |
23724 KB |
n=100 |
11 |
Correct |
12 ms |
23764 KB |
n=100 |
12 |
Correct |
13 ms |
23876 KB |
n=100 |
13 |
Correct |
11 ms |
23764 KB |
n=100 |
14 |
Correct |
12 ms |
23820 KB |
n=100 |
15 |
Correct |
12 ms |
23764 KB |
n=100 |
16 |
Correct |
12 ms |
23764 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
12 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23744 KB |
n=100 |
20 |
Correct |
12 ms |
23764 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
15 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23724 KB |
n=100 |
24 |
Correct |
12 ms |
23764 KB |
n=100 |
25 |
Correct |
12 ms |
23764 KB |
n=100 |
26 |
Correct |
12 ms |
23792 KB |
n=12 |
27 |
Correct |
13 ms |
23792 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23792 KB |
n=5 |
2 |
Correct |
12 ms |
23792 KB |
n=100 |
3 |
Correct |
12 ms |
23816 KB |
n=100 |
4 |
Correct |
12 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23800 KB |
n=100 |
6 |
Correct |
13 ms |
23800 KB |
n=100 |
7 |
Correct |
15 ms |
23780 KB |
n=100 |
8 |
Correct |
13 ms |
23792 KB |
n=100 |
9 |
Correct |
13 ms |
23744 KB |
n=100 |
10 |
Correct |
12 ms |
23724 KB |
n=100 |
11 |
Correct |
12 ms |
23764 KB |
n=100 |
12 |
Correct |
13 ms |
23876 KB |
n=100 |
13 |
Correct |
11 ms |
23764 KB |
n=100 |
14 |
Correct |
12 ms |
23820 KB |
n=100 |
15 |
Correct |
12 ms |
23764 KB |
n=100 |
16 |
Correct |
12 ms |
23764 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
12 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23744 KB |
n=100 |
20 |
Correct |
12 ms |
23764 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
15 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23724 KB |
n=100 |
24 |
Correct |
12 ms |
23764 KB |
n=100 |
25 |
Correct |
12 ms |
23764 KB |
n=100 |
26 |
Correct |
12 ms |
23792 KB |
n=12 |
27 |
Correct |
13 ms |
23792 KB |
n=100 |
28 |
Correct |
18 ms |
23824 KB |
n=500 |
29 |
Correct |
13 ms |
23892 KB |
n=500 |
30 |
Correct |
13 ms |
23892 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
14 ms |
23880 KB |
n=500 |
34 |
Correct |
16 ms |
23892 KB |
n=500 |
35 |
Correct |
14 ms |
23940 KB |
n=500 |
36 |
Correct |
14 ms |
23876 KB |
n=500 |
37 |
Correct |
13 ms |
23892 KB |
n=500 |
38 |
Correct |
17 ms |
23892 KB |
n=500 |
39 |
Correct |
18 ms |
23908 KB |
n=500 |
40 |
Correct |
14 ms |
23892 KB |
n=500 |
41 |
Correct |
13 ms |
23892 KB |
n=500 |
42 |
Correct |
13 ms |
23892 KB |
n=500 |
43 |
Correct |
13 ms |
23804 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23948 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23804 KB |
n=500 |
49 |
Correct |
13 ms |
23864 KB |
n=500 |
50 |
Correct |
14 ms |
23872 KB |
n=500 |
51 |
Correct |
17 ms |
23936 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
13 ms |
23828 KB |
n=500 |
54 |
Correct |
13 ms |
23924 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
17 ms |
24048 KB |
n=500 |
57 |
Correct |
14 ms |
23852 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23792 KB |
n=5 |
2 |
Correct |
12 ms |
23792 KB |
n=100 |
3 |
Correct |
12 ms |
23816 KB |
n=100 |
4 |
Correct |
12 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23800 KB |
n=100 |
6 |
Correct |
13 ms |
23800 KB |
n=100 |
7 |
Correct |
15 ms |
23780 KB |
n=100 |
8 |
Correct |
13 ms |
23792 KB |
n=100 |
9 |
Correct |
13 ms |
23744 KB |
n=100 |
10 |
Correct |
12 ms |
23724 KB |
n=100 |
11 |
Correct |
12 ms |
23764 KB |
n=100 |
12 |
Correct |
13 ms |
23876 KB |
n=100 |
13 |
Correct |
11 ms |
23764 KB |
n=100 |
14 |
Correct |
12 ms |
23820 KB |
n=100 |
15 |
Correct |
12 ms |
23764 KB |
n=100 |
16 |
Correct |
12 ms |
23764 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
12 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23744 KB |
n=100 |
20 |
Correct |
12 ms |
23764 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
15 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23724 KB |
n=100 |
24 |
Correct |
12 ms |
23764 KB |
n=100 |
25 |
Correct |
12 ms |
23764 KB |
n=100 |
26 |
Correct |
12 ms |
23792 KB |
n=12 |
27 |
Correct |
13 ms |
23792 KB |
n=100 |
28 |
Correct |
18 ms |
23824 KB |
n=500 |
29 |
Correct |
13 ms |
23892 KB |
n=500 |
30 |
Correct |
13 ms |
23892 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
14 ms |
23880 KB |
n=500 |
34 |
Correct |
16 ms |
23892 KB |
n=500 |
35 |
Correct |
14 ms |
23940 KB |
n=500 |
36 |
Correct |
14 ms |
23876 KB |
n=500 |
37 |
Correct |
13 ms |
23892 KB |
n=500 |
38 |
Correct |
17 ms |
23892 KB |
n=500 |
39 |
Correct |
18 ms |
23908 KB |
n=500 |
40 |
Correct |
14 ms |
23892 KB |
n=500 |
41 |
Correct |
13 ms |
23892 KB |
n=500 |
42 |
Correct |
13 ms |
23892 KB |
n=500 |
43 |
Correct |
13 ms |
23804 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23948 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23804 KB |
n=500 |
49 |
Correct |
13 ms |
23864 KB |
n=500 |
50 |
Correct |
14 ms |
23872 KB |
n=500 |
51 |
Correct |
17 ms |
23936 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
13 ms |
23828 KB |
n=500 |
54 |
Correct |
13 ms |
23924 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
17 ms |
24048 KB |
n=500 |
57 |
Correct |
14 ms |
23852 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
59 |
Correct |
53 ms |
24296 KB |
n=2000 |
60 |
Correct |
20 ms |
24324 KB |
n=2000 |
61 |
Correct |
20 ms |
24276 KB |
n=2000 |
62 |
Correct |
20 ms |
24276 KB |
n=2000 |
63 |
Correct |
73 ms |
24248 KB |
n=2000 |
64 |
Correct |
19 ms |
24312 KB |
n=2000 |
65 |
Correct |
69 ms |
24276 KB |
n=2000 |
66 |
Correct |
18 ms |
24276 KB |
n=2000 |
67 |
Correct |
42 ms |
24288 KB |
n=2000 |
68 |
Correct |
18 ms |
24276 KB |
n=2000 |
69 |
Correct |
20 ms |
24196 KB |
n=2000 |
70 |
Correct |
19 ms |
24276 KB |
n=2000 |
71 |
Correct |
21 ms |
24276 KB |
n=2000 |
72 |
Correct |
18 ms |
24252 KB |
n=2000 |
73 |
Correct |
18 ms |
24264 KB |
n=2000 |
74 |
Correct |
47 ms |
24188 KB |
n=1844 |
75 |
Correct |
22 ms |
24284 KB |
n=2000 |
76 |
Correct |
17 ms |
24276 KB |
n=2000 |
77 |
Correct |
20 ms |
24276 KB |
n=2000 |
78 |
Correct |
19 ms |
24276 KB |
n=2000 |
79 |
Correct |
36 ms |
24276 KB |
n=2000 |
80 |
Correct |
18 ms |
24312 KB |
n=2000 |
81 |
Correct |
18 ms |
24276 KB |
n=2000 |
82 |
Correct |
42 ms |
24196 KB |
n=2000 |
83 |
Correct |
18 ms |
24320 KB |
n=2000 |
84 |
Correct |
19 ms |
24276 KB |
n=2000 |
85 |
Correct |
19 ms |
24280 KB |
n=2000 |
86 |
Correct |
18 ms |
24220 KB |
n=2000 |
87 |
Correct |
18 ms |
24268 KB |
n=2000 |
88 |
Correct |
18 ms |
24428 KB |
n=2000 |
89 |
Correct |
19 ms |
24404 KB |
n=2000 |
90 |
Correct |
18 ms |
24404 KB |
n=2000 |
91 |
Correct |
19 ms |
24276 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23792 KB |
n=5 |
2 |
Correct |
12 ms |
23792 KB |
n=100 |
3 |
Correct |
12 ms |
23816 KB |
n=100 |
4 |
Correct |
12 ms |
23764 KB |
n=100 |
5 |
Correct |
14 ms |
23800 KB |
n=100 |
6 |
Correct |
13 ms |
23800 KB |
n=100 |
7 |
Correct |
15 ms |
23780 KB |
n=100 |
8 |
Correct |
13 ms |
23792 KB |
n=100 |
9 |
Correct |
13 ms |
23744 KB |
n=100 |
10 |
Correct |
12 ms |
23724 KB |
n=100 |
11 |
Correct |
12 ms |
23764 KB |
n=100 |
12 |
Correct |
13 ms |
23876 KB |
n=100 |
13 |
Correct |
11 ms |
23764 KB |
n=100 |
14 |
Correct |
12 ms |
23820 KB |
n=100 |
15 |
Correct |
12 ms |
23764 KB |
n=100 |
16 |
Correct |
12 ms |
23764 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
12 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23744 KB |
n=100 |
20 |
Correct |
12 ms |
23764 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
15 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23724 KB |
n=100 |
24 |
Correct |
12 ms |
23764 KB |
n=100 |
25 |
Correct |
12 ms |
23764 KB |
n=100 |
26 |
Correct |
12 ms |
23792 KB |
n=12 |
27 |
Correct |
13 ms |
23792 KB |
n=100 |
28 |
Correct |
18 ms |
23824 KB |
n=500 |
29 |
Correct |
13 ms |
23892 KB |
n=500 |
30 |
Correct |
13 ms |
23892 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
14 ms |
23880 KB |
n=500 |
34 |
Correct |
16 ms |
23892 KB |
n=500 |
35 |
Correct |
14 ms |
23940 KB |
n=500 |
36 |
Correct |
14 ms |
23876 KB |
n=500 |
37 |
Correct |
13 ms |
23892 KB |
n=500 |
38 |
Correct |
17 ms |
23892 KB |
n=500 |
39 |
Correct |
18 ms |
23908 KB |
n=500 |
40 |
Correct |
14 ms |
23892 KB |
n=500 |
41 |
Correct |
13 ms |
23892 KB |
n=500 |
42 |
Correct |
13 ms |
23892 KB |
n=500 |
43 |
Correct |
13 ms |
23804 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23948 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23804 KB |
n=500 |
49 |
Correct |
13 ms |
23864 KB |
n=500 |
50 |
Correct |
14 ms |
23872 KB |
n=500 |
51 |
Correct |
17 ms |
23936 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
13 ms |
23828 KB |
n=500 |
54 |
Correct |
13 ms |
23924 KB |
n=500 |
55 |
Correct |
14 ms |
23892 KB |
n=278 |
56 |
Correct |
17 ms |
24048 KB |
n=500 |
57 |
Correct |
14 ms |
23852 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
59 |
Correct |
53 ms |
24296 KB |
n=2000 |
60 |
Correct |
20 ms |
24324 KB |
n=2000 |
61 |
Correct |
20 ms |
24276 KB |
n=2000 |
62 |
Correct |
20 ms |
24276 KB |
n=2000 |
63 |
Correct |
73 ms |
24248 KB |
n=2000 |
64 |
Correct |
19 ms |
24312 KB |
n=2000 |
65 |
Correct |
69 ms |
24276 KB |
n=2000 |
66 |
Correct |
18 ms |
24276 KB |
n=2000 |
67 |
Correct |
42 ms |
24288 KB |
n=2000 |
68 |
Correct |
18 ms |
24276 KB |
n=2000 |
69 |
Correct |
20 ms |
24196 KB |
n=2000 |
70 |
Correct |
19 ms |
24276 KB |
n=2000 |
71 |
Correct |
21 ms |
24276 KB |
n=2000 |
72 |
Correct |
18 ms |
24252 KB |
n=2000 |
73 |
Correct |
18 ms |
24264 KB |
n=2000 |
74 |
Correct |
47 ms |
24188 KB |
n=1844 |
75 |
Correct |
22 ms |
24284 KB |
n=2000 |
76 |
Correct |
17 ms |
24276 KB |
n=2000 |
77 |
Correct |
20 ms |
24276 KB |
n=2000 |
78 |
Correct |
19 ms |
24276 KB |
n=2000 |
79 |
Correct |
36 ms |
24276 KB |
n=2000 |
80 |
Correct |
18 ms |
24312 KB |
n=2000 |
81 |
Correct |
18 ms |
24276 KB |
n=2000 |
82 |
Correct |
42 ms |
24196 KB |
n=2000 |
83 |
Correct |
18 ms |
24320 KB |
n=2000 |
84 |
Correct |
19 ms |
24276 KB |
n=2000 |
85 |
Correct |
19 ms |
24280 KB |
n=2000 |
86 |
Correct |
18 ms |
24220 KB |
n=2000 |
87 |
Correct |
18 ms |
24268 KB |
n=2000 |
88 |
Correct |
18 ms |
24428 KB |
n=2000 |
89 |
Correct |
19 ms |
24404 KB |
n=2000 |
90 |
Correct |
18 ms |
24404 KB |
n=2000 |
91 |
Correct |
19 ms |
24276 KB |
n=2000 |
92 |
Execution timed out |
4054 ms |
70412 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |