#include <bits/stdc++.h>
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
vector<vector<int>> g, pa;
vector<int> d;
void dfs(int i, int p) {
for (int j : g[i]) if (j != p)
d[j] = d[i] + 1, pa[j][0] = i, dfs(j, i);
}
int lca(int i, int j) {
if (d[i] < d[j]) swap(i, j);
for (int ii = 19; ~ii; --ii) if (d[pa[i][ii]] >= d[j])
i = pa[i][ii];
for (int ii = 19; ~ii; --ii) if (pa[i][ii] != pa[j][ii])
i = pa[i][ii], j = pa[j][ii];
return i == j ? i : pa[i][0];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
g.resize(n), pa.assign(n, vector<int>(20)), d.resize(n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y), g[y].push_back(x);
}
pa[0][0] = d[0] = 0;
dfs(0, 0);
for (int i = 1; i < 20; ++i) for (int j = 0; j < n; ++j)
pa[j][i] = pa[pa[j][i - 1]][i - 1];
int a[m];
for (int &i : a) cin >> i, --i;
set<int> s[n], s1[n];
for (int i = 1; i < m; ++i) s[lca(a[i], a[i - 1])].insert(i - 1);
for (int i = 0; i < m; ++i) s1[a[i]].insert(i);
while (q--) {
int ty, x, y, z;
cin >> ty >> x >> y;
--x, --y;
if (ty == 1) {
if (x) s[lca(a[x], a[x - 1])].erase(x - 1);
if (x < m - 1) s[lca(a[x], a[x + 1])].erase(x);
s1[a[x]].erase(x);
a[x] = y;
if (x) s[lca(a[x], a[x - 1])].insert(x - 1);
if (x < m - 1) s[lca(a[x], a[x + 1])].insert(x);
s1[a[x]].insert(x);
}
else {
cin >> z;
--z;
auto it = s1[z].lower_bound(x);
if (it != s1[z].end() && *it <= y) {
cout << *it + 1 << ' ' << *it + 1 << '\n';
continue;
}
it = s[z].lower_bound(x);
if (it == s[z].lower_bound(y))
cout << "-1 -1\n";
else cout << *it + 1 << ' ' << *it + 2 << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
1 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
1 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
1 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
1 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
1 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
1 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
1 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
1 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
492 KB |
n=500 |
29 |
Correct |
2 ms |
492 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
492 KB |
n=500 |
32 |
Correct |
2 ms |
492 KB |
n=500 |
33 |
Correct |
2 ms |
492 KB |
n=500 |
34 |
Correct |
2 ms |
492 KB |
n=500 |
35 |
Correct |
2 ms |
492 KB |
n=500 |
36 |
Correct |
2 ms |
492 KB |
n=500 |
37 |
Correct |
1 ms |
492 KB |
n=500 |
38 |
Correct |
1 ms |
492 KB |
n=500 |
39 |
Correct |
2 ms |
492 KB |
n=500 |
40 |
Correct |
2 ms |
492 KB |
n=500 |
41 |
Correct |
1 ms |
492 KB |
n=500 |
42 |
Correct |
2 ms |
492 KB |
n=500 |
43 |
Correct |
2 ms |
492 KB |
n=500 |
44 |
Correct |
2 ms |
492 KB |
n=500 |
45 |
Correct |
2 ms |
524 KB |
n=500 |
46 |
Correct |
2 ms |
492 KB |
n=500 |
47 |
Correct |
2 ms |
492 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
492 KB |
n=500 |
50 |
Correct |
2 ms |
492 KB |
n=500 |
51 |
Correct |
2 ms |
512 KB |
n=500 |
52 |
Correct |
2 ms |
492 KB |
n=500 |
53 |
Correct |
2 ms |
492 KB |
n=500 |
54 |
Correct |
2 ms |
492 KB |
n=500 |
55 |
Correct |
1 ms |
492 KB |
n=278 |
56 |
Correct |
2 ms |
492 KB |
n=500 |
57 |
Correct |
2 ms |
492 KB |
n=500 |
58 |
Correct |
1 ms |
492 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
1 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
1 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
1 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
1 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
492 KB |
n=500 |
29 |
Correct |
2 ms |
492 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
492 KB |
n=500 |
32 |
Correct |
2 ms |
492 KB |
n=500 |
33 |
Correct |
2 ms |
492 KB |
n=500 |
34 |
Correct |
2 ms |
492 KB |
n=500 |
35 |
Correct |
2 ms |
492 KB |
n=500 |
36 |
Correct |
2 ms |
492 KB |
n=500 |
37 |
Correct |
1 ms |
492 KB |
n=500 |
38 |
Correct |
1 ms |
492 KB |
n=500 |
39 |
Correct |
2 ms |
492 KB |
n=500 |
40 |
Correct |
2 ms |
492 KB |
n=500 |
41 |
Correct |
1 ms |
492 KB |
n=500 |
42 |
Correct |
2 ms |
492 KB |
n=500 |
43 |
Correct |
2 ms |
492 KB |
n=500 |
44 |
Correct |
2 ms |
492 KB |
n=500 |
45 |
Correct |
2 ms |
524 KB |
n=500 |
46 |
Correct |
2 ms |
492 KB |
n=500 |
47 |
Correct |
2 ms |
492 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
492 KB |
n=500 |
50 |
Correct |
2 ms |
492 KB |
n=500 |
51 |
Correct |
2 ms |
512 KB |
n=500 |
52 |
Correct |
2 ms |
492 KB |
n=500 |
53 |
Correct |
2 ms |
492 KB |
n=500 |
54 |
Correct |
2 ms |
492 KB |
n=500 |
55 |
Correct |
1 ms |
492 KB |
n=278 |
56 |
Correct |
2 ms |
492 KB |
n=500 |
57 |
Correct |
2 ms |
492 KB |
n=500 |
58 |
Correct |
1 ms |
492 KB |
n=500 |
59 |
Correct |
5 ms |
1132 KB |
n=2000 |
60 |
Correct |
5 ms |
1132 KB |
n=2000 |
61 |
Correct |
5 ms |
1132 KB |
n=2000 |
62 |
Correct |
7 ms |
1132 KB |
n=2000 |
63 |
Correct |
5 ms |
1132 KB |
n=2000 |
64 |
Correct |
7 ms |
1132 KB |
n=2000 |
65 |
Correct |
5 ms |
1260 KB |
n=2000 |
66 |
Correct |
5 ms |
1132 KB |
n=2000 |
67 |
Correct |
5 ms |
1132 KB |
n=2000 |
68 |
Correct |
5 ms |
1132 KB |
n=2000 |
69 |
Correct |
4 ms |
1132 KB |
n=2000 |
70 |
Correct |
4 ms |
1260 KB |
n=2000 |
71 |
Correct |
4 ms |
1132 KB |
n=2000 |
72 |
Correct |
3 ms |
1132 KB |
n=2000 |
73 |
Correct |
3 ms |
1132 KB |
n=2000 |
74 |
Correct |
4 ms |
1132 KB |
n=1844 |
75 |
Correct |
4 ms |
1132 KB |
n=2000 |
76 |
Correct |
5 ms |
1132 KB |
n=2000 |
77 |
Correct |
5 ms |
1132 KB |
n=2000 |
78 |
Correct |
5 ms |
1132 KB |
n=2000 |
79 |
Correct |
5 ms |
1132 KB |
n=2000 |
80 |
Correct |
5 ms |
1132 KB |
n=2000 |
81 |
Correct |
5 ms |
1132 KB |
n=2000 |
82 |
Correct |
5 ms |
1132 KB |
n=2000 |
83 |
Correct |
5 ms |
1132 KB |
n=2000 |
84 |
Correct |
6 ms |
1132 KB |
n=2000 |
85 |
Correct |
5 ms |
1132 KB |
n=2000 |
86 |
Correct |
5 ms |
1132 KB |
n=2000 |
87 |
Correct |
5 ms |
1132 KB |
n=2000 |
88 |
Correct |
5 ms |
1132 KB |
n=2000 |
89 |
Correct |
5 ms |
1132 KB |
n=2000 |
90 |
Correct |
6 ms |
1260 KB |
n=2000 |
91 |
Correct |
4 ms |
1132 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
1 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
1 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
1 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
1 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
492 KB |
n=500 |
29 |
Correct |
2 ms |
492 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
492 KB |
n=500 |
32 |
Correct |
2 ms |
492 KB |
n=500 |
33 |
Correct |
2 ms |
492 KB |
n=500 |
34 |
Correct |
2 ms |
492 KB |
n=500 |
35 |
Correct |
2 ms |
492 KB |
n=500 |
36 |
Correct |
2 ms |
492 KB |
n=500 |
37 |
Correct |
1 ms |
492 KB |
n=500 |
38 |
Correct |
1 ms |
492 KB |
n=500 |
39 |
Correct |
2 ms |
492 KB |
n=500 |
40 |
Correct |
2 ms |
492 KB |
n=500 |
41 |
Correct |
1 ms |
492 KB |
n=500 |
42 |
Correct |
2 ms |
492 KB |
n=500 |
43 |
Correct |
2 ms |
492 KB |
n=500 |
44 |
Correct |
2 ms |
492 KB |
n=500 |
45 |
Correct |
2 ms |
524 KB |
n=500 |
46 |
Correct |
2 ms |
492 KB |
n=500 |
47 |
Correct |
2 ms |
492 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
492 KB |
n=500 |
50 |
Correct |
2 ms |
492 KB |
n=500 |
51 |
Correct |
2 ms |
512 KB |
n=500 |
52 |
Correct |
2 ms |
492 KB |
n=500 |
53 |
Correct |
2 ms |
492 KB |
n=500 |
54 |
Correct |
2 ms |
492 KB |
n=500 |
55 |
Correct |
1 ms |
492 KB |
n=278 |
56 |
Correct |
2 ms |
492 KB |
n=500 |
57 |
Correct |
2 ms |
492 KB |
n=500 |
58 |
Correct |
1 ms |
492 KB |
n=500 |
59 |
Correct |
5 ms |
1132 KB |
n=2000 |
60 |
Correct |
5 ms |
1132 KB |
n=2000 |
61 |
Correct |
5 ms |
1132 KB |
n=2000 |
62 |
Correct |
7 ms |
1132 KB |
n=2000 |
63 |
Correct |
5 ms |
1132 KB |
n=2000 |
64 |
Correct |
7 ms |
1132 KB |
n=2000 |
65 |
Correct |
5 ms |
1260 KB |
n=2000 |
66 |
Correct |
5 ms |
1132 KB |
n=2000 |
67 |
Correct |
5 ms |
1132 KB |
n=2000 |
68 |
Correct |
5 ms |
1132 KB |
n=2000 |
69 |
Correct |
4 ms |
1132 KB |
n=2000 |
70 |
Correct |
4 ms |
1260 KB |
n=2000 |
71 |
Correct |
4 ms |
1132 KB |
n=2000 |
72 |
Correct |
3 ms |
1132 KB |
n=2000 |
73 |
Correct |
3 ms |
1132 KB |
n=2000 |
74 |
Correct |
4 ms |
1132 KB |
n=1844 |
75 |
Correct |
4 ms |
1132 KB |
n=2000 |
76 |
Correct |
5 ms |
1132 KB |
n=2000 |
77 |
Correct |
5 ms |
1132 KB |
n=2000 |
78 |
Correct |
5 ms |
1132 KB |
n=2000 |
79 |
Correct |
5 ms |
1132 KB |
n=2000 |
80 |
Correct |
5 ms |
1132 KB |
n=2000 |
81 |
Correct |
5 ms |
1132 KB |
n=2000 |
82 |
Correct |
5 ms |
1132 KB |
n=2000 |
83 |
Correct |
5 ms |
1132 KB |
n=2000 |
84 |
Correct |
6 ms |
1132 KB |
n=2000 |
85 |
Correct |
5 ms |
1132 KB |
n=2000 |
86 |
Correct |
5 ms |
1132 KB |
n=2000 |
87 |
Correct |
5 ms |
1132 KB |
n=2000 |
88 |
Correct |
5 ms |
1132 KB |
n=2000 |
89 |
Correct |
5 ms |
1132 KB |
n=2000 |
90 |
Correct |
6 ms |
1260 KB |
n=2000 |
91 |
Correct |
4 ms |
1132 KB |
n=2000 |
92 |
Correct |
1010 ms |
76780 KB |
n=200000 |
93 |
Correct |
1727 ms |
76012 KB |
n=200000 |
94 |
Correct |
1664 ms |
75900 KB |
n=200000 |
95 |
Correct |
1016 ms |
76496 KB |
n=200000 |
96 |
Correct |
922 ms |
76768 KB |
n=200000 |
97 |
Correct |
1736 ms |
76220 KB |
n=200000 |
98 |
Correct |
954 ms |
76640 KB |
n=200000 |
99 |
Correct |
1246 ms |
76140 KB |
n=200000 |
100 |
Correct |
960 ms |
76644 KB |
n=200000 |
101 |
Correct |
1662 ms |
75884 KB |
n=200000 |
102 |
Correct |
583 ms |
77548 KB |
n=200000 |
103 |
Correct |
592 ms |
77676 KB |
n=200000 |
104 |
Correct |
578 ms |
77604 KB |
n=200000 |
105 |
Correct |
586 ms |
77420 KB |
n=200000 |
106 |
Correct |
584 ms |
77292 KB |
n=200000 |
107 |
Correct |
582 ms |
77292 KB |
n=200000 |
108 |
Correct |
1177 ms |
76140 KB |
n=200000 |
109 |
Correct |
1164 ms |
75756 KB |
n=200000 |
110 |
Correct |
1143 ms |
75756 KB |
n=200000 |
111 |
Correct |
950 ms |
75744 KB |
n=200000 |
112 |
Correct |
1691 ms |
75116 KB |
n=200000 |
113 |
Correct |
1726 ms |
75176 KB |
n=200000 |
114 |
Correct |
965 ms |
75744 KB |
n=200000 |
115 |
Correct |
1855 ms |
75152 KB |
n=200000 |
116 |
Correct |
924 ms |
75576 KB |
n=200000 |
117 |
Correct |
1698 ms |
74860 KB |
n=200000 |
118 |
Correct |
1817 ms |
74848 KB |
n=200000 |
119 |
Correct |
892 ms |
75744 KB |
n=200000 |
120 |
Correct |
1595 ms |
74760 KB |
n=200000 |
121 |
Correct |
1575 ms |
74648 KB |
n=200000 |
122 |
Correct |
1592 ms |
75116 KB |
n=200000 |
123 |
Correct |
594 ms |
76268 KB |
n=200000 |
124 |
Correct |
334 ms |
16364 KB |
n=25264 |