#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> v(n + 1);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
vector<int> a(m + 1);
for (int i = 1; i <= m; i++) cin >> a[i];
int tim = 0;
vector<int> tin(n + 1), tout(n + 1);
vector<vector<int>> p(n + 1, vector<int>(20));
function<void(int, int)> dfs = [&](int x, int pr) {
tin[x] = ++tim;
p[x][0] = pr;
for (int j = 1; j < 20; j++) p[x][j] = p[p[x][j - 1]][j - 1];
for (auto z : v[x]) {
if (z == pr) continue;
dfs(z, x);
}
tout[x] = tim;
};
dfs(1, 1);
auto upper = [&](int x, int y) {
return tin[x] <= tin[y] && tout[y] <= tout[x];
};
auto lca = [&](int x, int y) {
if (upper(x, y)) return x;
if (upper(y, x)) return y;
for (int j = 19; j >= 0; j--) {
if (!upper(p[x][j], y)) {
x = p[x][j];
}
}
return p[x][0];
};
vector<set<int>> pos(n + 1), st(n + 1);
for (int i = 1; i <= m; i++) pos[a[i]].insert(i);
for (int i = 1; i <= m - 1; i++) {
st[lca(a[i], a[i + 1])].insert(i);
}
for (int i = 0; i < q; i++) {
int t;
cin >> t;
if (t == 1) {
int x, y;
cin >> x >> y;
pos[a[x]].erase(x);
if (x - 1 >= 1) {
st[lca(a[x - 1], a[x])].erase(x - 1);
}
if (x + 1 <= m) {
st[lca(a[x], a[x + 1])].erase(x);
}
a[x] = y;
pos[a[x]].insert(x);
if (x - 1 >= 1) {
st[lca(a[x - 1], a[x])].insert(x - 1);
}
if (x + 1 <= m) {
st[lca(a[x], a[x + 1])].insert(x);
}
} else if (t == 2) {
int l, r, x;
cin >> l >> r >> x;
auto it = pos[x].lower_bound(l);
auto it2 = st[x].lower_bound(l);
if (it != pos[x].end() && *it <= r) {
cout << *it << " " << *it << '\n';
} else if (it2 != st[x].end() && *it2 <= r - 1) {
cout << *it2 << " " << *it2 + 1 << '\n';
} else {
cout << -1 << " " << -1 << '\n';
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
0 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
0 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
0 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
0 ms |
348 KB |
n=100 |
9 |
Correct |
0 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
0 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
348 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
348 KB |
n=100 |
22 |
Correct |
1 ms |
600 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
0 ms |
348 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
0 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
0 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
0 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
0 ms |
348 KB |
n=100 |
9 |
Correct |
0 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
0 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
348 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
348 KB |
n=100 |
22 |
Correct |
1 ms |
600 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
0 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
1 ms |
604 KB |
n=500 |
30 |
Correct |
1 ms |
604 KB |
n=500 |
31 |
Correct |
1 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
1 ms |
604 KB |
n=500 |
34 |
Correct |
1 ms |
604 KB |
n=500 |
35 |
Correct |
1 ms |
604 KB |
n=500 |
36 |
Correct |
1 ms |
604 KB |
n=500 |
37 |
Correct |
1 ms |
604 KB |
n=500 |
38 |
Correct |
1 ms |
604 KB |
n=500 |
39 |
Correct |
1 ms |
600 KB |
n=500 |
40 |
Correct |
1 ms |
604 KB |
n=500 |
41 |
Correct |
1 ms |
604 KB |
n=500 |
42 |
Correct |
1 ms |
604 KB |
n=500 |
43 |
Correct |
1 ms |
604 KB |
n=500 |
44 |
Correct |
1 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
604 KB |
n=500 |
46 |
Correct |
1 ms |
604 KB |
n=500 |
47 |
Correct |
1 ms |
604 KB |
n=500 |
48 |
Correct |
1 ms |
604 KB |
n=500 |
49 |
Correct |
1 ms |
604 KB |
n=500 |
50 |
Correct |
1 ms |
604 KB |
n=500 |
51 |
Correct |
1 ms |
604 KB |
n=500 |
52 |
Correct |
1 ms |
600 KB |
n=500 |
53 |
Correct |
2 ms |
604 KB |
n=500 |
54 |
Correct |
1 ms |
604 KB |
n=500 |
55 |
Correct |
1 ms |
600 KB |
n=278 |
56 |
Correct |
1 ms |
720 KB |
n=500 |
57 |
Correct |
1 ms |
600 KB |
n=500 |
58 |
Correct |
1 ms |
604 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
0 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
0 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
0 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
0 ms |
348 KB |
n=100 |
9 |
Correct |
0 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
0 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
348 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
348 KB |
n=100 |
22 |
Correct |
1 ms |
600 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
0 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
1 ms |
604 KB |
n=500 |
30 |
Correct |
1 ms |
604 KB |
n=500 |
31 |
Correct |
1 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
1 ms |
604 KB |
n=500 |
34 |
Correct |
1 ms |
604 KB |
n=500 |
35 |
Correct |
1 ms |
604 KB |
n=500 |
36 |
Correct |
1 ms |
604 KB |
n=500 |
37 |
Correct |
1 ms |
604 KB |
n=500 |
38 |
Correct |
1 ms |
604 KB |
n=500 |
39 |
Correct |
1 ms |
600 KB |
n=500 |
40 |
Correct |
1 ms |
604 KB |
n=500 |
41 |
Correct |
1 ms |
604 KB |
n=500 |
42 |
Correct |
1 ms |
604 KB |
n=500 |
43 |
Correct |
1 ms |
604 KB |
n=500 |
44 |
Correct |
1 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
604 KB |
n=500 |
46 |
Correct |
1 ms |
604 KB |
n=500 |
47 |
Correct |
1 ms |
604 KB |
n=500 |
48 |
Correct |
1 ms |
604 KB |
n=500 |
49 |
Correct |
1 ms |
604 KB |
n=500 |
50 |
Correct |
1 ms |
604 KB |
n=500 |
51 |
Correct |
1 ms |
604 KB |
n=500 |
52 |
Correct |
1 ms |
600 KB |
n=500 |
53 |
Correct |
2 ms |
604 KB |
n=500 |
54 |
Correct |
1 ms |
604 KB |
n=500 |
55 |
Correct |
1 ms |
600 KB |
n=278 |
56 |
Correct |
1 ms |
720 KB |
n=500 |
57 |
Correct |
1 ms |
600 KB |
n=500 |
58 |
Correct |
1 ms |
604 KB |
n=500 |
59 |
Correct |
3 ms |
1372 KB |
n=2000 |
60 |
Correct |
3 ms |
1392 KB |
n=2000 |
61 |
Correct |
3 ms |
1372 KB |
n=2000 |
62 |
Correct |
3 ms |
1368 KB |
n=2000 |
63 |
Correct |
3 ms |
1372 KB |
n=2000 |
64 |
Correct |
3 ms |
1372 KB |
n=2000 |
65 |
Correct |
3 ms |
1372 KB |
n=2000 |
66 |
Correct |
3 ms |
1372 KB |
n=2000 |
67 |
Correct |
5 ms |
1372 KB |
n=2000 |
68 |
Correct |
5 ms |
1368 KB |
n=2000 |
69 |
Correct |
4 ms |
1368 KB |
n=2000 |
70 |
Correct |
2 ms |
1372 KB |
n=2000 |
71 |
Correct |
2 ms |
1368 KB |
n=2000 |
72 |
Correct |
2 ms |
1400 KB |
n=2000 |
73 |
Correct |
2 ms |
1372 KB |
n=2000 |
74 |
Correct |
3 ms |
1372 KB |
n=1844 |
75 |
Correct |
2 ms |
1372 KB |
n=2000 |
76 |
Correct |
4 ms |
1384 KB |
n=2000 |
77 |
Correct |
3 ms |
1412 KB |
n=2000 |
78 |
Correct |
3 ms |
1372 KB |
n=2000 |
79 |
Correct |
3 ms |
1372 KB |
n=2000 |
80 |
Correct |
3 ms |
1496 KB |
n=2000 |
81 |
Correct |
3 ms |
1372 KB |
n=2000 |
82 |
Correct |
4 ms |
1372 KB |
n=2000 |
83 |
Correct |
5 ms |
1368 KB |
n=2000 |
84 |
Correct |
3 ms |
1372 KB |
n=2000 |
85 |
Correct |
3 ms |
1388 KB |
n=2000 |
86 |
Correct |
3 ms |
1372 KB |
n=2000 |
87 |
Correct |
4 ms |
1372 KB |
n=2000 |
88 |
Correct |
2 ms |
1628 KB |
n=2000 |
89 |
Correct |
4 ms |
1492 KB |
n=2000 |
90 |
Correct |
4 ms |
1492 KB |
n=2000 |
91 |
Correct |
2 ms |
1372 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
0 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
0 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
0 ms |
348 KB |
n=100 |
7 |
Correct |
1 ms |
348 KB |
n=100 |
8 |
Correct |
0 ms |
348 KB |
n=100 |
9 |
Correct |
0 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
0 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
348 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
348 KB |
n=100 |
22 |
Correct |
1 ms |
600 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
1 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
348 KB |
n=12 |
27 |
Correct |
0 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
600 KB |
n=500 |
29 |
Correct |
1 ms |
604 KB |
n=500 |
30 |
Correct |
1 ms |
604 KB |
n=500 |
31 |
Correct |
1 ms |
604 KB |
n=500 |
32 |
Correct |
1 ms |
600 KB |
n=500 |
33 |
Correct |
1 ms |
604 KB |
n=500 |
34 |
Correct |
1 ms |
604 KB |
n=500 |
35 |
Correct |
1 ms |
604 KB |
n=500 |
36 |
Correct |
1 ms |
604 KB |
n=500 |
37 |
Correct |
1 ms |
604 KB |
n=500 |
38 |
Correct |
1 ms |
604 KB |
n=500 |
39 |
Correct |
1 ms |
600 KB |
n=500 |
40 |
Correct |
1 ms |
604 KB |
n=500 |
41 |
Correct |
1 ms |
604 KB |
n=500 |
42 |
Correct |
1 ms |
604 KB |
n=500 |
43 |
Correct |
1 ms |
604 KB |
n=500 |
44 |
Correct |
1 ms |
604 KB |
n=500 |
45 |
Correct |
1 ms |
604 KB |
n=500 |
46 |
Correct |
1 ms |
604 KB |
n=500 |
47 |
Correct |
1 ms |
604 KB |
n=500 |
48 |
Correct |
1 ms |
604 KB |
n=500 |
49 |
Correct |
1 ms |
604 KB |
n=500 |
50 |
Correct |
1 ms |
604 KB |
n=500 |
51 |
Correct |
1 ms |
604 KB |
n=500 |
52 |
Correct |
1 ms |
600 KB |
n=500 |
53 |
Correct |
2 ms |
604 KB |
n=500 |
54 |
Correct |
1 ms |
604 KB |
n=500 |
55 |
Correct |
1 ms |
600 KB |
n=278 |
56 |
Correct |
1 ms |
720 KB |
n=500 |
57 |
Correct |
1 ms |
600 KB |
n=500 |
58 |
Correct |
1 ms |
604 KB |
n=500 |
59 |
Correct |
3 ms |
1372 KB |
n=2000 |
60 |
Correct |
3 ms |
1392 KB |
n=2000 |
61 |
Correct |
3 ms |
1372 KB |
n=2000 |
62 |
Correct |
3 ms |
1368 KB |
n=2000 |
63 |
Correct |
3 ms |
1372 KB |
n=2000 |
64 |
Correct |
3 ms |
1372 KB |
n=2000 |
65 |
Correct |
3 ms |
1372 KB |
n=2000 |
66 |
Correct |
3 ms |
1372 KB |
n=2000 |
67 |
Correct |
5 ms |
1372 KB |
n=2000 |
68 |
Correct |
5 ms |
1368 KB |
n=2000 |
69 |
Correct |
4 ms |
1368 KB |
n=2000 |
70 |
Correct |
2 ms |
1372 KB |
n=2000 |
71 |
Correct |
2 ms |
1368 KB |
n=2000 |
72 |
Correct |
2 ms |
1400 KB |
n=2000 |
73 |
Correct |
2 ms |
1372 KB |
n=2000 |
74 |
Correct |
3 ms |
1372 KB |
n=1844 |
75 |
Correct |
2 ms |
1372 KB |
n=2000 |
76 |
Correct |
4 ms |
1384 KB |
n=2000 |
77 |
Correct |
3 ms |
1412 KB |
n=2000 |
78 |
Correct |
3 ms |
1372 KB |
n=2000 |
79 |
Correct |
3 ms |
1372 KB |
n=2000 |
80 |
Correct |
3 ms |
1496 KB |
n=2000 |
81 |
Correct |
3 ms |
1372 KB |
n=2000 |
82 |
Correct |
4 ms |
1372 KB |
n=2000 |
83 |
Correct |
5 ms |
1368 KB |
n=2000 |
84 |
Correct |
3 ms |
1372 KB |
n=2000 |
85 |
Correct |
3 ms |
1388 KB |
n=2000 |
86 |
Correct |
3 ms |
1372 KB |
n=2000 |
87 |
Correct |
4 ms |
1372 KB |
n=2000 |
88 |
Correct |
2 ms |
1628 KB |
n=2000 |
89 |
Correct |
4 ms |
1492 KB |
n=2000 |
90 |
Correct |
4 ms |
1492 KB |
n=2000 |
91 |
Correct |
2 ms |
1372 KB |
n=2000 |
92 |
Correct |
537 ms |
101964 KB |
n=200000 |
93 |
Correct |
686 ms |
111696 KB |
n=200000 |
94 |
Correct |
422 ms |
117732 KB |
n=200000 |
95 |
Correct |
561 ms |
101688 KB |
n=200000 |
96 |
Correct |
502 ms |
101828 KB |
n=200000 |
97 |
Correct |
712 ms |
109904 KB |
n=200000 |
98 |
Correct |
494 ms |
101824 KB |
n=200000 |
99 |
Correct |
583 ms |
102224 KB |
n=200000 |
100 |
Correct |
488 ms |
101828 KB |
n=200000 |
101 |
Correct |
350 ms |
120016 KB |
n=200000 |
102 |
Correct |
307 ms |
103252 KB |
n=200000 |
103 |
Correct |
278 ms |
103248 KB |
n=200000 |
104 |
Correct |
289 ms |
103208 KB |
n=200000 |
105 |
Correct |
294 ms |
103516 KB |
n=200000 |
106 |
Correct |
295 ms |
103728 KB |
n=200000 |
107 |
Correct |
290 ms |
103676 KB |
n=200000 |
108 |
Correct |
530 ms |
102224 KB |
n=200000 |
109 |
Correct |
520 ms |
102116 KB |
n=200000 |
110 |
Correct |
534 ms |
102140 KB |
n=200000 |
111 |
Correct |
431 ms |
101396 KB |
n=200000 |
112 |
Correct |
376 ms |
118300 KB |
n=200000 |
113 |
Correct |
571 ms |
109744 KB |
n=200000 |
114 |
Correct |
455 ms |
101312 KB |
n=200000 |
115 |
Correct |
659 ms |
105928 KB |
n=200000 |
116 |
Correct |
493 ms |
102060 KB |
n=200000 |
117 |
Correct |
364 ms |
118948 KB |
n=200000 |
118 |
Correct |
638 ms |
107600 KB |
n=200000 |
119 |
Correct |
482 ms |
101876 KB |
n=200000 |
120 |
Correct |
295 ms |
119120 KB |
n=200000 |
121 |
Correct |
319 ms |
119124 KB |
n=200000 |
122 |
Correct |
296 ms |
119416 KB |
n=200000 |
123 |
Correct |
296 ms |
103508 KB |
n=200000 |
124 |
Correct |
82 ms |
22996 KB |
n=25264 |