#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAXK = 19;
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);
if (b != pos[v].end() && *b <= r) {
cout << *b << " " << *b << "\n";
continue;
}
auto blca = lower_bound(pos_lca[v].begin(), pos_lca[v].end(), l);
if (blca != pos_lca[v].end() && *blca + 1 <= r) {
cout << *blca << " " << *blca + 1 << "\n";
continue;
}
cout << "-1 -1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
13 ms |
23792 KB |
n=100 |
3 |
Correct |
13 ms |
23720 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
13 ms |
23764 KB |
n=100 |
6 |
Correct |
15 ms |
23708 KB |
n=100 |
7 |
Correct |
13 ms |
23796 KB |
n=100 |
8 |
Correct |
12 ms |
23764 KB |
n=100 |
9 |
Correct |
12 ms |
23700 KB |
n=100 |
10 |
Correct |
13 ms |
23760 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
14 ms |
23892 KB |
n=100 |
13 |
Correct |
14 ms |
23740 KB |
n=100 |
14 |
Correct |
14 ms |
23764 KB |
n=100 |
15 |
Correct |
13 ms |
23764 KB |
n=100 |
16 |
Correct |
15 ms |
23816 KB |
n=100 |
17 |
Correct |
16 ms |
23776 KB |
n=100 |
18 |
Correct |
14 ms |
23724 KB |
n=100 |
19 |
Correct |
13 ms |
23764 KB |
n=100 |
20 |
Correct |
13 ms |
23740 KB |
n=100 |
21 |
Correct |
13 ms |
23764 KB |
n=100 |
22 |
Correct |
12 ms |
23764 KB |
n=100 |
23 |
Correct |
14 ms |
23764 KB |
n=100 |
24 |
Correct |
13 ms |
23736 KB |
n=100 |
25 |
Correct |
13 ms |
23812 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23824 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
13 ms |
23792 KB |
n=100 |
3 |
Correct |
13 ms |
23720 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
13 ms |
23764 KB |
n=100 |
6 |
Correct |
15 ms |
23708 KB |
n=100 |
7 |
Correct |
13 ms |
23796 KB |
n=100 |
8 |
Correct |
12 ms |
23764 KB |
n=100 |
9 |
Correct |
12 ms |
23700 KB |
n=100 |
10 |
Correct |
13 ms |
23760 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
14 ms |
23892 KB |
n=100 |
13 |
Correct |
14 ms |
23740 KB |
n=100 |
14 |
Correct |
14 ms |
23764 KB |
n=100 |
15 |
Correct |
13 ms |
23764 KB |
n=100 |
16 |
Correct |
15 ms |
23816 KB |
n=100 |
17 |
Correct |
16 ms |
23776 KB |
n=100 |
18 |
Correct |
14 ms |
23724 KB |
n=100 |
19 |
Correct |
13 ms |
23764 KB |
n=100 |
20 |
Correct |
13 ms |
23740 KB |
n=100 |
21 |
Correct |
13 ms |
23764 KB |
n=100 |
22 |
Correct |
12 ms |
23764 KB |
n=100 |
23 |
Correct |
14 ms |
23764 KB |
n=100 |
24 |
Correct |
13 ms |
23736 KB |
n=100 |
25 |
Correct |
13 ms |
23812 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23824 KB |
n=100 |
28 |
Correct |
15 ms |
23892 KB |
n=500 |
29 |
Correct |
14 ms |
23872 KB |
n=500 |
30 |
Correct |
15 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
16 ms |
23892 KB |
n=500 |
34 |
Correct |
16 ms |
23804 KB |
n=500 |
35 |
Correct |
14 ms |
23860 KB |
n=500 |
36 |
Correct |
14 ms |
23824 KB |
n=500 |
37 |
Correct |
14 ms |
23804 KB |
n=500 |
38 |
Correct |
17 ms |
23900 KB |
n=500 |
39 |
Correct |
14 ms |
23892 KB |
n=500 |
40 |
Correct |
14 ms |
23884 KB |
n=500 |
41 |
Correct |
17 ms |
23800 KB |
n=500 |
42 |
Correct |
17 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23900 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
15 ms |
23848 KB |
n=500 |
46 |
Correct |
15 ms |
23912 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23956 KB |
n=500 |
49 |
Correct |
14 ms |
23892 KB |
n=500 |
50 |
Correct |
14 ms |
23860 KB |
n=500 |
51 |
Correct |
16 ms |
23856 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
14 ms |
23892 KB |
n=500 |
54 |
Correct |
14 ms |
23896 KB |
n=500 |
55 |
Correct |
13 ms |
23764 KB |
n=278 |
56 |
Correct |
14 ms |
23892 KB |
n=500 |
57 |
Correct |
14 ms |
23892 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
13 ms |
23792 KB |
n=100 |
3 |
Correct |
13 ms |
23720 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
13 ms |
23764 KB |
n=100 |
6 |
Correct |
15 ms |
23708 KB |
n=100 |
7 |
Correct |
13 ms |
23796 KB |
n=100 |
8 |
Correct |
12 ms |
23764 KB |
n=100 |
9 |
Correct |
12 ms |
23700 KB |
n=100 |
10 |
Correct |
13 ms |
23760 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
14 ms |
23892 KB |
n=100 |
13 |
Correct |
14 ms |
23740 KB |
n=100 |
14 |
Correct |
14 ms |
23764 KB |
n=100 |
15 |
Correct |
13 ms |
23764 KB |
n=100 |
16 |
Correct |
15 ms |
23816 KB |
n=100 |
17 |
Correct |
16 ms |
23776 KB |
n=100 |
18 |
Correct |
14 ms |
23724 KB |
n=100 |
19 |
Correct |
13 ms |
23764 KB |
n=100 |
20 |
Correct |
13 ms |
23740 KB |
n=100 |
21 |
Correct |
13 ms |
23764 KB |
n=100 |
22 |
Correct |
12 ms |
23764 KB |
n=100 |
23 |
Correct |
14 ms |
23764 KB |
n=100 |
24 |
Correct |
13 ms |
23736 KB |
n=100 |
25 |
Correct |
13 ms |
23812 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23824 KB |
n=100 |
28 |
Correct |
15 ms |
23892 KB |
n=500 |
29 |
Correct |
14 ms |
23872 KB |
n=500 |
30 |
Correct |
15 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
16 ms |
23892 KB |
n=500 |
34 |
Correct |
16 ms |
23804 KB |
n=500 |
35 |
Correct |
14 ms |
23860 KB |
n=500 |
36 |
Correct |
14 ms |
23824 KB |
n=500 |
37 |
Correct |
14 ms |
23804 KB |
n=500 |
38 |
Correct |
17 ms |
23900 KB |
n=500 |
39 |
Correct |
14 ms |
23892 KB |
n=500 |
40 |
Correct |
14 ms |
23884 KB |
n=500 |
41 |
Correct |
17 ms |
23800 KB |
n=500 |
42 |
Correct |
17 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23900 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
15 ms |
23848 KB |
n=500 |
46 |
Correct |
15 ms |
23912 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23956 KB |
n=500 |
49 |
Correct |
14 ms |
23892 KB |
n=500 |
50 |
Correct |
14 ms |
23860 KB |
n=500 |
51 |
Correct |
16 ms |
23856 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
14 ms |
23892 KB |
n=500 |
54 |
Correct |
14 ms |
23896 KB |
n=500 |
55 |
Correct |
13 ms |
23764 KB |
n=278 |
56 |
Correct |
14 ms |
23892 KB |
n=500 |
57 |
Correct |
14 ms |
23892 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
59 |
Correct |
33 ms |
24148 KB |
n=2000 |
60 |
Correct |
23 ms |
24236 KB |
n=2000 |
61 |
Correct |
18 ms |
24276 KB |
n=2000 |
62 |
Correct |
19 ms |
24248 KB |
n=2000 |
63 |
Correct |
44 ms |
24148 KB |
n=2000 |
64 |
Correct |
20 ms |
24148 KB |
n=2000 |
65 |
Correct |
43 ms |
24232 KB |
n=2000 |
66 |
Correct |
19 ms |
24148 KB |
n=2000 |
67 |
Correct |
29 ms |
24148 KB |
n=2000 |
68 |
Correct |
18 ms |
24148 KB |
n=2000 |
69 |
Correct |
21 ms |
24148 KB |
n=2000 |
70 |
Correct |
19 ms |
24148 KB |
n=2000 |
71 |
Correct |
19 ms |
24236 KB |
n=2000 |
72 |
Correct |
20 ms |
24148 KB |
n=2000 |
73 |
Correct |
19 ms |
24148 KB |
n=2000 |
74 |
Correct |
34 ms |
24124 KB |
n=1844 |
75 |
Correct |
20 ms |
24120 KB |
n=2000 |
76 |
Correct |
20 ms |
24244 KB |
n=2000 |
77 |
Correct |
19 ms |
24176 KB |
n=2000 |
78 |
Correct |
21 ms |
24148 KB |
n=2000 |
79 |
Correct |
35 ms |
24172 KB |
n=2000 |
80 |
Correct |
19 ms |
24280 KB |
n=2000 |
81 |
Correct |
19 ms |
24148 KB |
n=2000 |
82 |
Correct |
28 ms |
24120 KB |
n=2000 |
83 |
Correct |
18 ms |
24224 KB |
n=2000 |
84 |
Correct |
19 ms |
24148 KB |
n=2000 |
85 |
Correct |
20 ms |
24148 KB |
n=2000 |
86 |
Correct |
19 ms |
24204 KB |
n=2000 |
87 |
Correct |
22 ms |
24168 KB |
n=2000 |
88 |
Correct |
24 ms |
24276 KB |
n=2000 |
89 |
Correct |
18 ms |
24276 KB |
n=2000 |
90 |
Correct |
18 ms |
24276 KB |
n=2000 |
91 |
Correct |
23 ms |
24216 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
n=5 |
2 |
Correct |
13 ms |
23792 KB |
n=100 |
3 |
Correct |
13 ms |
23720 KB |
n=100 |
4 |
Correct |
13 ms |
23764 KB |
n=100 |
5 |
Correct |
13 ms |
23764 KB |
n=100 |
6 |
Correct |
15 ms |
23708 KB |
n=100 |
7 |
Correct |
13 ms |
23796 KB |
n=100 |
8 |
Correct |
12 ms |
23764 KB |
n=100 |
9 |
Correct |
12 ms |
23700 KB |
n=100 |
10 |
Correct |
13 ms |
23760 KB |
n=100 |
11 |
Correct |
13 ms |
23764 KB |
n=100 |
12 |
Correct |
14 ms |
23892 KB |
n=100 |
13 |
Correct |
14 ms |
23740 KB |
n=100 |
14 |
Correct |
14 ms |
23764 KB |
n=100 |
15 |
Correct |
13 ms |
23764 KB |
n=100 |
16 |
Correct |
15 ms |
23816 KB |
n=100 |
17 |
Correct |
16 ms |
23776 KB |
n=100 |
18 |
Correct |
14 ms |
23724 KB |
n=100 |
19 |
Correct |
13 ms |
23764 KB |
n=100 |
20 |
Correct |
13 ms |
23740 KB |
n=100 |
21 |
Correct |
13 ms |
23764 KB |
n=100 |
22 |
Correct |
12 ms |
23764 KB |
n=100 |
23 |
Correct |
14 ms |
23764 KB |
n=100 |
24 |
Correct |
13 ms |
23736 KB |
n=100 |
25 |
Correct |
13 ms |
23812 KB |
n=100 |
26 |
Correct |
13 ms |
23764 KB |
n=12 |
27 |
Correct |
13 ms |
23824 KB |
n=100 |
28 |
Correct |
15 ms |
23892 KB |
n=500 |
29 |
Correct |
14 ms |
23872 KB |
n=500 |
30 |
Correct |
15 ms |
23884 KB |
n=500 |
31 |
Correct |
14 ms |
23892 KB |
n=500 |
32 |
Correct |
16 ms |
23892 KB |
n=500 |
33 |
Correct |
16 ms |
23892 KB |
n=500 |
34 |
Correct |
16 ms |
23804 KB |
n=500 |
35 |
Correct |
14 ms |
23860 KB |
n=500 |
36 |
Correct |
14 ms |
23824 KB |
n=500 |
37 |
Correct |
14 ms |
23804 KB |
n=500 |
38 |
Correct |
17 ms |
23900 KB |
n=500 |
39 |
Correct |
14 ms |
23892 KB |
n=500 |
40 |
Correct |
14 ms |
23884 KB |
n=500 |
41 |
Correct |
17 ms |
23800 KB |
n=500 |
42 |
Correct |
17 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23900 KB |
n=500 |
44 |
Correct |
14 ms |
23872 KB |
n=500 |
45 |
Correct |
15 ms |
23848 KB |
n=500 |
46 |
Correct |
15 ms |
23912 KB |
n=500 |
47 |
Correct |
14 ms |
23892 KB |
n=500 |
48 |
Correct |
15 ms |
23956 KB |
n=500 |
49 |
Correct |
14 ms |
23892 KB |
n=500 |
50 |
Correct |
14 ms |
23860 KB |
n=500 |
51 |
Correct |
16 ms |
23856 KB |
n=500 |
52 |
Correct |
14 ms |
23892 KB |
n=500 |
53 |
Correct |
14 ms |
23892 KB |
n=500 |
54 |
Correct |
14 ms |
23896 KB |
n=500 |
55 |
Correct |
13 ms |
23764 KB |
n=278 |
56 |
Correct |
14 ms |
23892 KB |
n=500 |
57 |
Correct |
14 ms |
23892 KB |
n=500 |
58 |
Correct |
14 ms |
23892 KB |
n=500 |
59 |
Correct |
33 ms |
24148 KB |
n=2000 |
60 |
Correct |
23 ms |
24236 KB |
n=2000 |
61 |
Correct |
18 ms |
24276 KB |
n=2000 |
62 |
Correct |
19 ms |
24248 KB |
n=2000 |
63 |
Correct |
44 ms |
24148 KB |
n=2000 |
64 |
Correct |
20 ms |
24148 KB |
n=2000 |
65 |
Correct |
43 ms |
24232 KB |
n=2000 |
66 |
Correct |
19 ms |
24148 KB |
n=2000 |
67 |
Correct |
29 ms |
24148 KB |
n=2000 |
68 |
Correct |
18 ms |
24148 KB |
n=2000 |
69 |
Correct |
21 ms |
24148 KB |
n=2000 |
70 |
Correct |
19 ms |
24148 KB |
n=2000 |
71 |
Correct |
19 ms |
24236 KB |
n=2000 |
72 |
Correct |
20 ms |
24148 KB |
n=2000 |
73 |
Correct |
19 ms |
24148 KB |
n=2000 |
74 |
Correct |
34 ms |
24124 KB |
n=1844 |
75 |
Correct |
20 ms |
24120 KB |
n=2000 |
76 |
Correct |
20 ms |
24244 KB |
n=2000 |
77 |
Correct |
19 ms |
24176 KB |
n=2000 |
78 |
Correct |
21 ms |
24148 KB |
n=2000 |
79 |
Correct |
35 ms |
24172 KB |
n=2000 |
80 |
Correct |
19 ms |
24280 KB |
n=2000 |
81 |
Correct |
19 ms |
24148 KB |
n=2000 |
82 |
Correct |
28 ms |
24120 KB |
n=2000 |
83 |
Correct |
18 ms |
24224 KB |
n=2000 |
84 |
Correct |
19 ms |
24148 KB |
n=2000 |
85 |
Correct |
20 ms |
24148 KB |
n=2000 |
86 |
Correct |
19 ms |
24204 KB |
n=2000 |
87 |
Correct |
22 ms |
24168 KB |
n=2000 |
88 |
Correct |
24 ms |
24276 KB |
n=2000 |
89 |
Correct |
18 ms |
24276 KB |
n=2000 |
90 |
Correct |
18 ms |
24276 KB |
n=2000 |
91 |
Correct |
23 ms |
24216 KB |
n=2000 |
92 |
Execution timed out |
4073 ms |
66296 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |