#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, q;
int a[N];
vector<int> G[N];
int dep[N], par[N][18];
set<int> v1[N], v2[N];
void dfs(int u) {
for (int v : G[u]) if (v != par[u][0]) {
par[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 17; i >= 0; --i) if (dep[par[u][i]] >= dep[v]) u = par[u][i];
for (int i = 17; i >= 0; --i) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
return u == v ? u : par[u][0];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
par[1][0] = 1;
dfs(1);
for (int j = 1; j <= 17; ++j) {
for (int i = 1; i <= n; ++i) par[i][j] = par[par[i][j - 1]][j - 1];
}
for (int i = 1; i <= m; ++i) {
cin >> a[i];
v1[a[i]].insert(i);
}
for (int i = 1; i < m; ++i) {
v2[lca(a[i], a[i + 1])].insert(i);
}
while(q--) {
int type; cin >> type;
if (type == 1) {
int pos, u; cin >> pos >> u;
v1[a[pos]].erase(pos);
if (pos > 1) v2[lca(a[pos], a[pos - 1])].erase(pos - 1);
if (pos < m) v2[lca(a[pos], a[pos + 1])].erase(pos);
a[pos] = u;
v1[a[pos]].insert(pos);
if (pos > 1) v2[lca(a[pos], a[pos - 1])].insert(pos - 1);
if (pos < m) v2[lca(a[pos], a[pos + 1])].insert(pos);
} else {
int l, r, u; cin >> l >> r >> u;
set<int>::iterator it1 = v1[u].lower_bound(l);
set<int>::iterator it2 = v2[u].lower_bound(l);
int x = -1, y = -1;
if (it1 != v1[u].end() && (*it1) <= r) x = (*it1), y = (*it1);
if (it2 != v2[u].end() && (*it2) < r) x = (*it2), y = (*it2) + 1;
printf("%d %d\n", x, y);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
20 ms |
23904 KB |
n=100 |
3 |
Correct |
22 ms |
23976 KB |
n=100 |
4 |
Correct |
19 ms |
23976 KB |
n=100 |
5 |
Correct |
21 ms |
23976 KB |
n=100 |
6 |
Correct |
20 ms |
23976 KB |
n=100 |
7 |
Correct |
21 ms |
24048 KB |
n=100 |
8 |
Correct |
20 ms |
24084 KB |
n=100 |
9 |
Correct |
21 ms |
24084 KB |
n=100 |
10 |
Correct |
22 ms |
24084 KB |
n=100 |
11 |
Correct |
22 ms |
24084 KB |
n=100 |
12 |
Correct |
22 ms |
24084 KB |
n=100 |
13 |
Correct |
20 ms |
24084 KB |
n=100 |
14 |
Correct |
21 ms |
24084 KB |
n=100 |
15 |
Correct |
25 ms |
24084 KB |
n=100 |
16 |
Correct |
24 ms |
24084 KB |
n=100 |
17 |
Correct |
22 ms |
24084 KB |
n=100 |
18 |
Correct |
20 ms |
24084 KB |
n=100 |
19 |
Correct |
20 ms |
24084 KB |
n=100 |
20 |
Correct |
21 ms |
24084 KB |
n=100 |
21 |
Correct |
20 ms |
24084 KB |
n=100 |
22 |
Correct |
20 ms |
24084 KB |
n=100 |
23 |
Correct |
20 ms |
24084 KB |
n=100 |
24 |
Correct |
21 ms |
24084 KB |
n=100 |
25 |
Correct |
21 ms |
24084 KB |
n=100 |
26 |
Correct |
23 ms |
24156 KB |
n=12 |
27 |
Correct |
19 ms |
24156 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
20 ms |
23904 KB |
n=100 |
3 |
Correct |
22 ms |
23976 KB |
n=100 |
4 |
Correct |
19 ms |
23976 KB |
n=100 |
5 |
Correct |
21 ms |
23976 KB |
n=100 |
6 |
Correct |
20 ms |
23976 KB |
n=100 |
7 |
Correct |
21 ms |
24048 KB |
n=100 |
8 |
Correct |
20 ms |
24084 KB |
n=100 |
9 |
Correct |
21 ms |
24084 KB |
n=100 |
10 |
Correct |
22 ms |
24084 KB |
n=100 |
11 |
Correct |
22 ms |
24084 KB |
n=100 |
12 |
Correct |
22 ms |
24084 KB |
n=100 |
13 |
Correct |
20 ms |
24084 KB |
n=100 |
14 |
Correct |
21 ms |
24084 KB |
n=100 |
15 |
Correct |
25 ms |
24084 KB |
n=100 |
16 |
Correct |
24 ms |
24084 KB |
n=100 |
17 |
Correct |
22 ms |
24084 KB |
n=100 |
18 |
Correct |
20 ms |
24084 KB |
n=100 |
19 |
Correct |
20 ms |
24084 KB |
n=100 |
20 |
Correct |
21 ms |
24084 KB |
n=100 |
21 |
Correct |
20 ms |
24084 KB |
n=100 |
22 |
Correct |
20 ms |
24084 KB |
n=100 |
23 |
Correct |
20 ms |
24084 KB |
n=100 |
24 |
Correct |
21 ms |
24084 KB |
n=100 |
25 |
Correct |
21 ms |
24084 KB |
n=100 |
26 |
Correct |
23 ms |
24156 KB |
n=12 |
27 |
Correct |
19 ms |
24156 KB |
n=100 |
28 |
Correct |
22 ms |
24156 KB |
n=500 |
29 |
Correct |
22 ms |
24156 KB |
n=500 |
30 |
Correct |
22 ms |
24160 KB |
n=500 |
31 |
Correct |
20 ms |
24160 KB |
n=500 |
32 |
Correct |
19 ms |
24160 KB |
n=500 |
33 |
Correct |
21 ms |
24160 KB |
n=500 |
34 |
Correct |
20 ms |
24160 KB |
n=500 |
35 |
Correct |
20 ms |
24160 KB |
n=500 |
36 |
Correct |
20 ms |
24160 KB |
n=500 |
37 |
Correct |
21 ms |
24160 KB |
n=500 |
38 |
Correct |
22 ms |
24160 KB |
n=500 |
39 |
Correct |
22 ms |
24160 KB |
n=500 |
40 |
Correct |
20 ms |
24160 KB |
n=500 |
41 |
Correct |
20 ms |
24160 KB |
n=500 |
42 |
Correct |
20 ms |
24160 KB |
n=500 |
43 |
Correct |
22 ms |
24160 KB |
n=500 |
44 |
Correct |
21 ms |
24160 KB |
n=500 |
45 |
Correct |
21 ms |
24160 KB |
n=500 |
46 |
Correct |
20 ms |
24160 KB |
n=500 |
47 |
Correct |
20 ms |
24160 KB |
n=500 |
48 |
Correct |
24 ms |
24160 KB |
n=500 |
49 |
Correct |
21 ms |
24160 KB |
n=500 |
50 |
Correct |
23 ms |
24160 KB |
n=500 |
51 |
Correct |
20 ms |
24160 KB |
n=500 |
52 |
Correct |
21 ms |
24160 KB |
n=500 |
53 |
Correct |
23 ms |
24160 KB |
n=500 |
54 |
Correct |
22 ms |
24160 KB |
n=500 |
55 |
Correct |
22 ms |
24160 KB |
n=278 |
56 |
Correct |
21 ms |
24160 KB |
n=500 |
57 |
Correct |
21 ms |
24160 KB |
n=500 |
58 |
Correct |
21 ms |
24160 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
20 ms |
23904 KB |
n=100 |
3 |
Correct |
22 ms |
23976 KB |
n=100 |
4 |
Correct |
19 ms |
23976 KB |
n=100 |
5 |
Correct |
21 ms |
23976 KB |
n=100 |
6 |
Correct |
20 ms |
23976 KB |
n=100 |
7 |
Correct |
21 ms |
24048 KB |
n=100 |
8 |
Correct |
20 ms |
24084 KB |
n=100 |
9 |
Correct |
21 ms |
24084 KB |
n=100 |
10 |
Correct |
22 ms |
24084 KB |
n=100 |
11 |
Correct |
22 ms |
24084 KB |
n=100 |
12 |
Correct |
22 ms |
24084 KB |
n=100 |
13 |
Correct |
20 ms |
24084 KB |
n=100 |
14 |
Correct |
21 ms |
24084 KB |
n=100 |
15 |
Correct |
25 ms |
24084 KB |
n=100 |
16 |
Correct |
24 ms |
24084 KB |
n=100 |
17 |
Correct |
22 ms |
24084 KB |
n=100 |
18 |
Correct |
20 ms |
24084 KB |
n=100 |
19 |
Correct |
20 ms |
24084 KB |
n=100 |
20 |
Correct |
21 ms |
24084 KB |
n=100 |
21 |
Correct |
20 ms |
24084 KB |
n=100 |
22 |
Correct |
20 ms |
24084 KB |
n=100 |
23 |
Correct |
20 ms |
24084 KB |
n=100 |
24 |
Correct |
21 ms |
24084 KB |
n=100 |
25 |
Correct |
21 ms |
24084 KB |
n=100 |
26 |
Correct |
23 ms |
24156 KB |
n=12 |
27 |
Correct |
19 ms |
24156 KB |
n=100 |
28 |
Correct |
22 ms |
24156 KB |
n=500 |
29 |
Correct |
22 ms |
24156 KB |
n=500 |
30 |
Correct |
22 ms |
24160 KB |
n=500 |
31 |
Correct |
20 ms |
24160 KB |
n=500 |
32 |
Correct |
19 ms |
24160 KB |
n=500 |
33 |
Correct |
21 ms |
24160 KB |
n=500 |
34 |
Correct |
20 ms |
24160 KB |
n=500 |
35 |
Correct |
20 ms |
24160 KB |
n=500 |
36 |
Correct |
20 ms |
24160 KB |
n=500 |
37 |
Correct |
21 ms |
24160 KB |
n=500 |
38 |
Correct |
22 ms |
24160 KB |
n=500 |
39 |
Correct |
22 ms |
24160 KB |
n=500 |
40 |
Correct |
20 ms |
24160 KB |
n=500 |
41 |
Correct |
20 ms |
24160 KB |
n=500 |
42 |
Correct |
20 ms |
24160 KB |
n=500 |
43 |
Correct |
22 ms |
24160 KB |
n=500 |
44 |
Correct |
21 ms |
24160 KB |
n=500 |
45 |
Correct |
21 ms |
24160 KB |
n=500 |
46 |
Correct |
20 ms |
24160 KB |
n=500 |
47 |
Correct |
20 ms |
24160 KB |
n=500 |
48 |
Correct |
24 ms |
24160 KB |
n=500 |
49 |
Correct |
21 ms |
24160 KB |
n=500 |
50 |
Correct |
23 ms |
24160 KB |
n=500 |
51 |
Correct |
20 ms |
24160 KB |
n=500 |
52 |
Correct |
21 ms |
24160 KB |
n=500 |
53 |
Correct |
23 ms |
24160 KB |
n=500 |
54 |
Correct |
22 ms |
24160 KB |
n=500 |
55 |
Correct |
22 ms |
24160 KB |
n=278 |
56 |
Correct |
21 ms |
24160 KB |
n=500 |
57 |
Correct |
21 ms |
24160 KB |
n=500 |
58 |
Correct |
21 ms |
24160 KB |
n=500 |
59 |
Correct |
24 ms |
24540 KB |
n=2000 |
60 |
Correct |
24 ms |
24540 KB |
n=2000 |
61 |
Correct |
25 ms |
24556 KB |
n=2000 |
62 |
Correct |
26 ms |
24572 KB |
n=2000 |
63 |
Correct |
26 ms |
24572 KB |
n=2000 |
64 |
Correct |
24 ms |
24588 KB |
n=2000 |
65 |
Correct |
24 ms |
24588 KB |
n=2000 |
66 |
Correct |
26 ms |
24588 KB |
n=2000 |
67 |
Correct |
27 ms |
24588 KB |
n=2000 |
68 |
Correct |
25 ms |
24716 KB |
n=2000 |
69 |
Correct |
24 ms |
24716 KB |
n=2000 |
70 |
Correct |
24 ms |
24716 KB |
n=2000 |
71 |
Correct |
23 ms |
24716 KB |
n=2000 |
72 |
Correct |
25 ms |
24716 KB |
n=2000 |
73 |
Correct |
24 ms |
24716 KB |
n=2000 |
74 |
Correct |
29 ms |
24716 KB |
n=1844 |
75 |
Correct |
24 ms |
24716 KB |
n=2000 |
76 |
Correct |
24 ms |
24716 KB |
n=2000 |
77 |
Correct |
25 ms |
24716 KB |
n=2000 |
78 |
Correct |
26 ms |
24716 KB |
n=2000 |
79 |
Correct |
25 ms |
24716 KB |
n=2000 |
80 |
Correct |
24 ms |
24716 KB |
n=2000 |
81 |
Correct |
26 ms |
24716 KB |
n=2000 |
82 |
Correct |
26 ms |
24716 KB |
n=2000 |
83 |
Correct |
23 ms |
24716 KB |
n=2000 |
84 |
Correct |
26 ms |
24716 KB |
n=2000 |
85 |
Correct |
25 ms |
24716 KB |
n=2000 |
86 |
Correct |
25 ms |
24716 KB |
n=2000 |
87 |
Correct |
26 ms |
24716 KB |
n=2000 |
88 |
Correct |
24 ms |
24716 KB |
n=2000 |
89 |
Correct |
25 ms |
24716 KB |
n=2000 |
90 |
Correct |
27 ms |
24716 KB |
n=2000 |
91 |
Correct |
30 ms |
24716 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
20 ms |
23904 KB |
n=100 |
3 |
Correct |
22 ms |
23976 KB |
n=100 |
4 |
Correct |
19 ms |
23976 KB |
n=100 |
5 |
Correct |
21 ms |
23976 KB |
n=100 |
6 |
Correct |
20 ms |
23976 KB |
n=100 |
7 |
Correct |
21 ms |
24048 KB |
n=100 |
8 |
Correct |
20 ms |
24084 KB |
n=100 |
9 |
Correct |
21 ms |
24084 KB |
n=100 |
10 |
Correct |
22 ms |
24084 KB |
n=100 |
11 |
Correct |
22 ms |
24084 KB |
n=100 |
12 |
Correct |
22 ms |
24084 KB |
n=100 |
13 |
Correct |
20 ms |
24084 KB |
n=100 |
14 |
Correct |
21 ms |
24084 KB |
n=100 |
15 |
Correct |
25 ms |
24084 KB |
n=100 |
16 |
Correct |
24 ms |
24084 KB |
n=100 |
17 |
Correct |
22 ms |
24084 KB |
n=100 |
18 |
Correct |
20 ms |
24084 KB |
n=100 |
19 |
Correct |
20 ms |
24084 KB |
n=100 |
20 |
Correct |
21 ms |
24084 KB |
n=100 |
21 |
Correct |
20 ms |
24084 KB |
n=100 |
22 |
Correct |
20 ms |
24084 KB |
n=100 |
23 |
Correct |
20 ms |
24084 KB |
n=100 |
24 |
Correct |
21 ms |
24084 KB |
n=100 |
25 |
Correct |
21 ms |
24084 KB |
n=100 |
26 |
Correct |
23 ms |
24156 KB |
n=12 |
27 |
Correct |
19 ms |
24156 KB |
n=100 |
28 |
Correct |
22 ms |
24156 KB |
n=500 |
29 |
Correct |
22 ms |
24156 KB |
n=500 |
30 |
Correct |
22 ms |
24160 KB |
n=500 |
31 |
Correct |
20 ms |
24160 KB |
n=500 |
32 |
Correct |
19 ms |
24160 KB |
n=500 |
33 |
Correct |
21 ms |
24160 KB |
n=500 |
34 |
Correct |
20 ms |
24160 KB |
n=500 |
35 |
Correct |
20 ms |
24160 KB |
n=500 |
36 |
Correct |
20 ms |
24160 KB |
n=500 |
37 |
Correct |
21 ms |
24160 KB |
n=500 |
38 |
Correct |
22 ms |
24160 KB |
n=500 |
39 |
Correct |
22 ms |
24160 KB |
n=500 |
40 |
Correct |
20 ms |
24160 KB |
n=500 |
41 |
Correct |
20 ms |
24160 KB |
n=500 |
42 |
Correct |
20 ms |
24160 KB |
n=500 |
43 |
Correct |
22 ms |
24160 KB |
n=500 |
44 |
Correct |
21 ms |
24160 KB |
n=500 |
45 |
Correct |
21 ms |
24160 KB |
n=500 |
46 |
Correct |
20 ms |
24160 KB |
n=500 |
47 |
Correct |
20 ms |
24160 KB |
n=500 |
48 |
Correct |
24 ms |
24160 KB |
n=500 |
49 |
Correct |
21 ms |
24160 KB |
n=500 |
50 |
Correct |
23 ms |
24160 KB |
n=500 |
51 |
Correct |
20 ms |
24160 KB |
n=500 |
52 |
Correct |
21 ms |
24160 KB |
n=500 |
53 |
Correct |
23 ms |
24160 KB |
n=500 |
54 |
Correct |
22 ms |
24160 KB |
n=500 |
55 |
Correct |
22 ms |
24160 KB |
n=278 |
56 |
Correct |
21 ms |
24160 KB |
n=500 |
57 |
Correct |
21 ms |
24160 KB |
n=500 |
58 |
Correct |
21 ms |
24160 KB |
n=500 |
59 |
Correct |
24 ms |
24540 KB |
n=2000 |
60 |
Correct |
24 ms |
24540 KB |
n=2000 |
61 |
Correct |
25 ms |
24556 KB |
n=2000 |
62 |
Correct |
26 ms |
24572 KB |
n=2000 |
63 |
Correct |
26 ms |
24572 KB |
n=2000 |
64 |
Correct |
24 ms |
24588 KB |
n=2000 |
65 |
Correct |
24 ms |
24588 KB |
n=2000 |
66 |
Correct |
26 ms |
24588 KB |
n=2000 |
67 |
Correct |
27 ms |
24588 KB |
n=2000 |
68 |
Correct |
25 ms |
24716 KB |
n=2000 |
69 |
Correct |
24 ms |
24716 KB |
n=2000 |
70 |
Correct |
24 ms |
24716 KB |
n=2000 |
71 |
Correct |
23 ms |
24716 KB |
n=2000 |
72 |
Correct |
25 ms |
24716 KB |
n=2000 |
73 |
Correct |
24 ms |
24716 KB |
n=2000 |
74 |
Correct |
29 ms |
24716 KB |
n=1844 |
75 |
Correct |
24 ms |
24716 KB |
n=2000 |
76 |
Correct |
24 ms |
24716 KB |
n=2000 |
77 |
Correct |
25 ms |
24716 KB |
n=2000 |
78 |
Correct |
26 ms |
24716 KB |
n=2000 |
79 |
Correct |
25 ms |
24716 KB |
n=2000 |
80 |
Correct |
24 ms |
24716 KB |
n=2000 |
81 |
Correct |
26 ms |
24716 KB |
n=2000 |
82 |
Correct |
26 ms |
24716 KB |
n=2000 |
83 |
Correct |
23 ms |
24716 KB |
n=2000 |
84 |
Correct |
26 ms |
24716 KB |
n=2000 |
85 |
Correct |
25 ms |
24716 KB |
n=2000 |
86 |
Correct |
25 ms |
24716 KB |
n=2000 |
87 |
Correct |
26 ms |
24716 KB |
n=2000 |
88 |
Correct |
24 ms |
24716 KB |
n=2000 |
89 |
Correct |
25 ms |
24716 KB |
n=2000 |
90 |
Correct |
27 ms |
24716 KB |
n=2000 |
91 |
Correct |
30 ms |
24716 KB |
n=2000 |
92 |
Correct |
1074 ms |
66940 KB |
n=200000 |
93 |
Correct |
1723 ms |
70600 KB |
n=200000 |
94 |
Correct |
1586 ms |
74012 KB |
n=200000 |
95 |
Correct |
1058 ms |
74012 KB |
n=200000 |
96 |
Correct |
1040 ms |
74012 KB |
n=200000 |
97 |
Correct |
1729 ms |
74012 KB |
n=200000 |
98 |
Correct |
1295 ms |
74012 KB |
n=200000 |
99 |
Correct |
1393 ms |
74012 KB |
n=200000 |
100 |
Correct |
1067 ms |
74012 KB |
n=200000 |
101 |
Correct |
1638 ms |
75112 KB |
n=200000 |
102 |
Correct |
591 ms |
75112 KB |
n=200000 |
103 |
Correct |
608 ms |
75112 KB |
n=200000 |
104 |
Correct |
606 ms |
75112 KB |
n=200000 |
105 |
Correct |
589 ms |
75112 KB |
n=200000 |
106 |
Correct |
580 ms |
75112 KB |
n=200000 |
107 |
Correct |
601 ms |
75112 KB |
n=200000 |
108 |
Correct |
1180 ms |
75112 KB |
n=200000 |
109 |
Correct |
1203 ms |
75112 KB |
n=200000 |
110 |
Correct |
1182 ms |
75112 KB |
n=200000 |
111 |
Correct |
1049 ms |
75112 KB |
n=200000 |
112 |
Correct |
1561 ms |
75112 KB |
n=200000 |
113 |
Correct |
1598 ms |
75112 KB |
n=200000 |
114 |
Correct |
1027 ms |
75112 KB |
n=200000 |
115 |
Correct |
1604 ms |
75112 KB |
n=200000 |
116 |
Correct |
962 ms |
75112 KB |
n=200000 |
117 |
Correct |
1573 ms |
75112 KB |
n=200000 |
118 |
Correct |
1544 ms |
75112 KB |
n=200000 |
119 |
Correct |
1025 ms |
75112 KB |
n=200000 |
120 |
Correct |
1575 ms |
75112 KB |
n=200000 |
121 |
Correct |
1540 ms |
75112 KB |
n=200000 |
122 |
Correct |
1521 ms |
75420 KB |
n=200000 |
123 |
Correct |
573 ms |
75420 KB |
n=200000 |
124 |
Correct |
344 ms |
75420 KB |
n=25264 |