//https://oj.uz/problem/view/IZhO18_treearray
#include<bits/stdc++.h>
const int N = 2e5 + 5;
using namespace std;
vector <int> adj[N];
set <int> ans[2][N];
int n, m, q, a[N], p[N][20], d[N];
void dfs(int u){
for (int i = 0; i < (int)adj[u].size(); i++){
int v = adj[u][i];
if (v == p[u][0]) continue;
d[v] = d[u] + 1; p[v][0] = u;
dfs(v);
}
}
int lca(int u, int v){
if (d[u] < d[v]) swap(u, v);
for (int i = 19; i >= 0; i--) if (d[u] - (1 << i) >= d[v]) u = p[u][i];
for (int i = 19; i >= 0; i--) if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return ((u != v) ? p[u][0] : u);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 2; i <= n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v); adj[v].push_back(u);
}
dfs(1);
for (int i = 1; i < 20; i++) for (int j = 1; j <= n; j++) p[j][i] = p[p[j][i-1]][i-1];
for (int i = 1; i <= m; i++) cin >> a[i];
for (int i = 1; i <= m; i++) ans[0][a[i]].insert(i);
for (int i = 1; i < m; i++) ans[1][lca(a[i], a[i+1])].insert(i);
while (q--){
int type;
cin >> type;
if (type == 1){
int pos, v;
cin >> pos >> v;
ans[0][a[pos]].erase(pos);
if (pos > 1) ans[1][lca(a[pos-1], a[pos])].erase(pos-1);
if (pos < m) ans[1][lca(a[pos], a[pos+1])].erase(pos);
a[pos] = v;
ans[0][a[pos]].insert(pos);
if (pos > 1) ans[1][lca(a[pos-1], a[pos])].insert(pos-1);
if (pos < m) ans[1][lca(a[pos], a[pos+1])].insert(pos);
}
else{
int l, r, v;
cin >> l >> r >> v;
set <int> ::iterator it = ans[0][v].lower_bound(l);
if (it != ans[0][v].end() && *it <= r) {
cout << *it << " " << *it << "\n";
continue;
}
it = ans[1][v].lower_bound(l);
if (it != ans[1][v].end() && *it < r){
cout << *it << " " << (*it)+1 << "\n";
continue;
}
cout << "-1 -1\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
22 ms |
24036 KB |
n=100 |
3 |
Correct |
21 ms |
24036 KB |
n=100 |
4 |
Correct |
20 ms |
24036 KB |
n=100 |
5 |
Correct |
23 ms |
24036 KB |
n=100 |
6 |
Correct |
23 ms |
24036 KB |
n=100 |
7 |
Correct |
22 ms |
24036 KB |
n=100 |
8 |
Correct |
21 ms |
24036 KB |
n=100 |
9 |
Correct |
21 ms |
24036 KB |
n=100 |
10 |
Correct |
22 ms |
24036 KB |
n=100 |
11 |
Correct |
20 ms |
24036 KB |
n=100 |
12 |
Correct |
21 ms |
24128 KB |
n=100 |
13 |
Correct |
22 ms |
24128 KB |
n=100 |
14 |
Correct |
20 ms |
24128 KB |
n=100 |
15 |
Correct |
21 ms |
24128 KB |
n=100 |
16 |
Correct |
20 ms |
24128 KB |
n=100 |
17 |
Correct |
21 ms |
24128 KB |
n=100 |
18 |
Correct |
21 ms |
24128 KB |
n=100 |
19 |
Correct |
22 ms |
24128 KB |
n=100 |
20 |
Correct |
21 ms |
24128 KB |
n=100 |
21 |
Correct |
22 ms |
24128 KB |
n=100 |
22 |
Correct |
22 ms |
24128 KB |
n=100 |
23 |
Correct |
22 ms |
24128 KB |
n=100 |
24 |
Correct |
21 ms |
24128 KB |
n=100 |
25 |
Correct |
27 ms |
24128 KB |
n=100 |
26 |
Correct |
20 ms |
24128 KB |
n=12 |
27 |
Correct |
20 ms |
24132 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
22 ms |
24036 KB |
n=100 |
3 |
Correct |
21 ms |
24036 KB |
n=100 |
4 |
Correct |
20 ms |
24036 KB |
n=100 |
5 |
Correct |
23 ms |
24036 KB |
n=100 |
6 |
Correct |
23 ms |
24036 KB |
n=100 |
7 |
Correct |
22 ms |
24036 KB |
n=100 |
8 |
Correct |
21 ms |
24036 KB |
n=100 |
9 |
Correct |
21 ms |
24036 KB |
n=100 |
10 |
Correct |
22 ms |
24036 KB |
n=100 |
11 |
Correct |
20 ms |
24036 KB |
n=100 |
12 |
Correct |
21 ms |
24128 KB |
n=100 |
13 |
Correct |
22 ms |
24128 KB |
n=100 |
14 |
Correct |
20 ms |
24128 KB |
n=100 |
15 |
Correct |
21 ms |
24128 KB |
n=100 |
16 |
Correct |
20 ms |
24128 KB |
n=100 |
17 |
Correct |
21 ms |
24128 KB |
n=100 |
18 |
Correct |
21 ms |
24128 KB |
n=100 |
19 |
Correct |
22 ms |
24128 KB |
n=100 |
20 |
Correct |
21 ms |
24128 KB |
n=100 |
21 |
Correct |
22 ms |
24128 KB |
n=100 |
22 |
Correct |
22 ms |
24128 KB |
n=100 |
23 |
Correct |
22 ms |
24128 KB |
n=100 |
24 |
Correct |
21 ms |
24128 KB |
n=100 |
25 |
Correct |
27 ms |
24128 KB |
n=100 |
26 |
Correct |
20 ms |
24128 KB |
n=12 |
27 |
Correct |
20 ms |
24132 KB |
n=100 |
28 |
Correct |
22 ms |
24264 KB |
n=500 |
29 |
Correct |
22 ms |
24276 KB |
n=500 |
30 |
Correct |
21 ms |
24292 KB |
n=500 |
31 |
Correct |
22 ms |
24308 KB |
n=500 |
32 |
Correct |
23 ms |
24324 KB |
n=500 |
33 |
Correct |
22 ms |
24336 KB |
n=500 |
34 |
Correct |
23 ms |
24352 KB |
n=500 |
35 |
Correct |
23 ms |
24620 KB |
n=500 |
36 |
Correct |
27 ms |
24620 KB |
n=500 |
37 |
Correct |
22 ms |
24620 KB |
n=500 |
38 |
Correct |
21 ms |
24620 KB |
n=500 |
39 |
Correct |
21 ms |
24620 KB |
n=500 |
40 |
Correct |
20 ms |
24620 KB |
n=500 |
41 |
Correct |
21 ms |
24620 KB |
n=500 |
42 |
Correct |
22 ms |
24620 KB |
n=500 |
43 |
Correct |
23 ms |
24620 KB |
n=500 |
44 |
Correct |
22 ms |
24620 KB |
n=500 |
45 |
Correct |
23 ms |
24620 KB |
n=500 |
46 |
Correct |
25 ms |
24620 KB |
n=500 |
47 |
Correct |
23 ms |
24620 KB |
n=500 |
48 |
Correct |
21 ms |
24620 KB |
n=500 |
49 |
Correct |
21 ms |
24620 KB |
n=500 |
50 |
Correct |
23 ms |
24620 KB |
n=500 |
51 |
Correct |
28 ms |
24620 KB |
n=500 |
52 |
Correct |
22 ms |
24728 KB |
n=500 |
53 |
Correct |
22 ms |
24728 KB |
n=500 |
54 |
Correct |
22 ms |
24728 KB |
n=500 |
55 |
Correct |
23 ms |
24728 KB |
n=278 |
56 |
Correct |
22 ms |
24728 KB |
n=500 |
57 |
Correct |
22 ms |
24728 KB |
n=500 |
58 |
Correct |
21 ms |
24728 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
22 ms |
24036 KB |
n=100 |
3 |
Correct |
21 ms |
24036 KB |
n=100 |
4 |
Correct |
20 ms |
24036 KB |
n=100 |
5 |
Correct |
23 ms |
24036 KB |
n=100 |
6 |
Correct |
23 ms |
24036 KB |
n=100 |
7 |
Correct |
22 ms |
24036 KB |
n=100 |
8 |
Correct |
21 ms |
24036 KB |
n=100 |
9 |
Correct |
21 ms |
24036 KB |
n=100 |
10 |
Correct |
22 ms |
24036 KB |
n=100 |
11 |
Correct |
20 ms |
24036 KB |
n=100 |
12 |
Correct |
21 ms |
24128 KB |
n=100 |
13 |
Correct |
22 ms |
24128 KB |
n=100 |
14 |
Correct |
20 ms |
24128 KB |
n=100 |
15 |
Correct |
21 ms |
24128 KB |
n=100 |
16 |
Correct |
20 ms |
24128 KB |
n=100 |
17 |
Correct |
21 ms |
24128 KB |
n=100 |
18 |
Correct |
21 ms |
24128 KB |
n=100 |
19 |
Correct |
22 ms |
24128 KB |
n=100 |
20 |
Correct |
21 ms |
24128 KB |
n=100 |
21 |
Correct |
22 ms |
24128 KB |
n=100 |
22 |
Correct |
22 ms |
24128 KB |
n=100 |
23 |
Correct |
22 ms |
24128 KB |
n=100 |
24 |
Correct |
21 ms |
24128 KB |
n=100 |
25 |
Correct |
27 ms |
24128 KB |
n=100 |
26 |
Correct |
20 ms |
24128 KB |
n=12 |
27 |
Correct |
20 ms |
24132 KB |
n=100 |
28 |
Correct |
22 ms |
24264 KB |
n=500 |
29 |
Correct |
22 ms |
24276 KB |
n=500 |
30 |
Correct |
21 ms |
24292 KB |
n=500 |
31 |
Correct |
22 ms |
24308 KB |
n=500 |
32 |
Correct |
23 ms |
24324 KB |
n=500 |
33 |
Correct |
22 ms |
24336 KB |
n=500 |
34 |
Correct |
23 ms |
24352 KB |
n=500 |
35 |
Correct |
23 ms |
24620 KB |
n=500 |
36 |
Correct |
27 ms |
24620 KB |
n=500 |
37 |
Correct |
22 ms |
24620 KB |
n=500 |
38 |
Correct |
21 ms |
24620 KB |
n=500 |
39 |
Correct |
21 ms |
24620 KB |
n=500 |
40 |
Correct |
20 ms |
24620 KB |
n=500 |
41 |
Correct |
21 ms |
24620 KB |
n=500 |
42 |
Correct |
22 ms |
24620 KB |
n=500 |
43 |
Correct |
23 ms |
24620 KB |
n=500 |
44 |
Correct |
22 ms |
24620 KB |
n=500 |
45 |
Correct |
23 ms |
24620 KB |
n=500 |
46 |
Correct |
25 ms |
24620 KB |
n=500 |
47 |
Correct |
23 ms |
24620 KB |
n=500 |
48 |
Correct |
21 ms |
24620 KB |
n=500 |
49 |
Correct |
21 ms |
24620 KB |
n=500 |
50 |
Correct |
23 ms |
24620 KB |
n=500 |
51 |
Correct |
28 ms |
24620 KB |
n=500 |
52 |
Correct |
22 ms |
24728 KB |
n=500 |
53 |
Correct |
22 ms |
24728 KB |
n=500 |
54 |
Correct |
22 ms |
24728 KB |
n=500 |
55 |
Correct |
23 ms |
24728 KB |
n=278 |
56 |
Correct |
22 ms |
24728 KB |
n=500 |
57 |
Correct |
22 ms |
24728 KB |
n=500 |
58 |
Correct |
21 ms |
24728 KB |
n=500 |
59 |
Correct |
27 ms |
25076 KB |
n=2000 |
60 |
Correct |
27 ms |
25336 KB |
n=2000 |
61 |
Correct |
25 ms |
25336 KB |
n=2000 |
62 |
Correct |
27 ms |
25336 KB |
n=2000 |
63 |
Correct |
27 ms |
25336 KB |
n=2000 |
64 |
Correct |
28 ms |
25508 KB |
n=2000 |
65 |
Correct |
26 ms |
25508 KB |
n=2000 |
66 |
Correct |
28 ms |
25648 KB |
n=2000 |
67 |
Correct |
27 ms |
25648 KB |
n=2000 |
68 |
Correct |
27 ms |
25760 KB |
n=2000 |
69 |
Correct |
28 ms |
25760 KB |
n=2000 |
70 |
Correct |
24 ms |
25760 KB |
n=2000 |
71 |
Correct |
27 ms |
25784 KB |
n=2000 |
72 |
Correct |
24 ms |
25964 KB |
n=2000 |
73 |
Correct |
23 ms |
25964 KB |
n=2000 |
74 |
Correct |
28 ms |
25964 KB |
n=1844 |
75 |
Correct |
25 ms |
25992 KB |
n=2000 |
76 |
Correct |
25 ms |
26052 KB |
n=2000 |
77 |
Correct |
25 ms |
26136 KB |
n=2000 |
78 |
Correct |
26 ms |
26136 KB |
n=2000 |
79 |
Correct |
26 ms |
26136 KB |
n=2000 |
80 |
Correct |
26 ms |
26136 KB |
n=2000 |
81 |
Correct |
26 ms |
26136 KB |
n=2000 |
82 |
Correct |
24 ms |
26148 KB |
n=2000 |
83 |
Correct |
27 ms |
26148 KB |
n=2000 |
84 |
Correct |
24 ms |
26148 KB |
n=2000 |
85 |
Correct |
24 ms |
26148 KB |
n=2000 |
86 |
Correct |
24 ms |
26148 KB |
n=2000 |
87 |
Correct |
24 ms |
26148 KB |
n=2000 |
88 |
Correct |
24 ms |
26260 KB |
n=2000 |
89 |
Correct |
24 ms |
26260 KB |
n=2000 |
90 |
Correct |
24 ms |
26264 KB |
n=2000 |
91 |
Correct |
22 ms |
26264 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
n=5 |
2 |
Correct |
22 ms |
24036 KB |
n=100 |
3 |
Correct |
21 ms |
24036 KB |
n=100 |
4 |
Correct |
20 ms |
24036 KB |
n=100 |
5 |
Correct |
23 ms |
24036 KB |
n=100 |
6 |
Correct |
23 ms |
24036 KB |
n=100 |
7 |
Correct |
22 ms |
24036 KB |
n=100 |
8 |
Correct |
21 ms |
24036 KB |
n=100 |
9 |
Correct |
21 ms |
24036 KB |
n=100 |
10 |
Correct |
22 ms |
24036 KB |
n=100 |
11 |
Correct |
20 ms |
24036 KB |
n=100 |
12 |
Correct |
21 ms |
24128 KB |
n=100 |
13 |
Correct |
22 ms |
24128 KB |
n=100 |
14 |
Correct |
20 ms |
24128 KB |
n=100 |
15 |
Correct |
21 ms |
24128 KB |
n=100 |
16 |
Correct |
20 ms |
24128 KB |
n=100 |
17 |
Correct |
21 ms |
24128 KB |
n=100 |
18 |
Correct |
21 ms |
24128 KB |
n=100 |
19 |
Correct |
22 ms |
24128 KB |
n=100 |
20 |
Correct |
21 ms |
24128 KB |
n=100 |
21 |
Correct |
22 ms |
24128 KB |
n=100 |
22 |
Correct |
22 ms |
24128 KB |
n=100 |
23 |
Correct |
22 ms |
24128 KB |
n=100 |
24 |
Correct |
21 ms |
24128 KB |
n=100 |
25 |
Correct |
27 ms |
24128 KB |
n=100 |
26 |
Correct |
20 ms |
24128 KB |
n=12 |
27 |
Correct |
20 ms |
24132 KB |
n=100 |
28 |
Correct |
22 ms |
24264 KB |
n=500 |
29 |
Correct |
22 ms |
24276 KB |
n=500 |
30 |
Correct |
21 ms |
24292 KB |
n=500 |
31 |
Correct |
22 ms |
24308 KB |
n=500 |
32 |
Correct |
23 ms |
24324 KB |
n=500 |
33 |
Correct |
22 ms |
24336 KB |
n=500 |
34 |
Correct |
23 ms |
24352 KB |
n=500 |
35 |
Correct |
23 ms |
24620 KB |
n=500 |
36 |
Correct |
27 ms |
24620 KB |
n=500 |
37 |
Correct |
22 ms |
24620 KB |
n=500 |
38 |
Correct |
21 ms |
24620 KB |
n=500 |
39 |
Correct |
21 ms |
24620 KB |
n=500 |
40 |
Correct |
20 ms |
24620 KB |
n=500 |
41 |
Correct |
21 ms |
24620 KB |
n=500 |
42 |
Correct |
22 ms |
24620 KB |
n=500 |
43 |
Correct |
23 ms |
24620 KB |
n=500 |
44 |
Correct |
22 ms |
24620 KB |
n=500 |
45 |
Correct |
23 ms |
24620 KB |
n=500 |
46 |
Correct |
25 ms |
24620 KB |
n=500 |
47 |
Correct |
23 ms |
24620 KB |
n=500 |
48 |
Correct |
21 ms |
24620 KB |
n=500 |
49 |
Correct |
21 ms |
24620 KB |
n=500 |
50 |
Correct |
23 ms |
24620 KB |
n=500 |
51 |
Correct |
28 ms |
24620 KB |
n=500 |
52 |
Correct |
22 ms |
24728 KB |
n=500 |
53 |
Correct |
22 ms |
24728 KB |
n=500 |
54 |
Correct |
22 ms |
24728 KB |
n=500 |
55 |
Correct |
23 ms |
24728 KB |
n=278 |
56 |
Correct |
22 ms |
24728 KB |
n=500 |
57 |
Correct |
22 ms |
24728 KB |
n=500 |
58 |
Correct |
21 ms |
24728 KB |
n=500 |
59 |
Correct |
27 ms |
25076 KB |
n=2000 |
60 |
Correct |
27 ms |
25336 KB |
n=2000 |
61 |
Correct |
25 ms |
25336 KB |
n=2000 |
62 |
Correct |
27 ms |
25336 KB |
n=2000 |
63 |
Correct |
27 ms |
25336 KB |
n=2000 |
64 |
Correct |
28 ms |
25508 KB |
n=2000 |
65 |
Correct |
26 ms |
25508 KB |
n=2000 |
66 |
Correct |
28 ms |
25648 KB |
n=2000 |
67 |
Correct |
27 ms |
25648 KB |
n=2000 |
68 |
Correct |
27 ms |
25760 KB |
n=2000 |
69 |
Correct |
28 ms |
25760 KB |
n=2000 |
70 |
Correct |
24 ms |
25760 KB |
n=2000 |
71 |
Correct |
27 ms |
25784 KB |
n=2000 |
72 |
Correct |
24 ms |
25964 KB |
n=2000 |
73 |
Correct |
23 ms |
25964 KB |
n=2000 |
74 |
Correct |
28 ms |
25964 KB |
n=1844 |
75 |
Correct |
25 ms |
25992 KB |
n=2000 |
76 |
Correct |
25 ms |
26052 KB |
n=2000 |
77 |
Correct |
25 ms |
26136 KB |
n=2000 |
78 |
Correct |
26 ms |
26136 KB |
n=2000 |
79 |
Correct |
26 ms |
26136 KB |
n=2000 |
80 |
Correct |
26 ms |
26136 KB |
n=2000 |
81 |
Correct |
26 ms |
26136 KB |
n=2000 |
82 |
Correct |
24 ms |
26148 KB |
n=2000 |
83 |
Correct |
27 ms |
26148 KB |
n=2000 |
84 |
Correct |
24 ms |
26148 KB |
n=2000 |
85 |
Correct |
24 ms |
26148 KB |
n=2000 |
86 |
Correct |
24 ms |
26148 KB |
n=2000 |
87 |
Correct |
24 ms |
26148 KB |
n=2000 |
88 |
Correct |
24 ms |
26260 KB |
n=2000 |
89 |
Correct |
24 ms |
26260 KB |
n=2000 |
90 |
Correct |
24 ms |
26264 KB |
n=2000 |
91 |
Correct |
22 ms |
26264 KB |
n=2000 |
92 |
Correct |
949 ms |
70048 KB |
n=200000 |
93 |
Correct |
1407 ms |
84440 KB |
n=200000 |
94 |
Correct |
1373 ms |
97840 KB |
n=200000 |
95 |
Correct |
982 ms |
97840 KB |
n=200000 |
96 |
Correct |
922 ms |
98076 KB |
n=200000 |
97 |
Correct |
1377 ms |
103992 KB |
n=200000 |
98 |
Correct |
929 ms |
103992 KB |
n=200000 |
99 |
Correct |
1154 ms |
103992 KB |
n=200000 |
100 |
Correct |
952 ms |
103992 KB |
n=200000 |
101 |
Correct |
1443 ms |
113228 KB |
n=200000 |
102 |
Correct |
529 ms |
113228 KB |
n=200000 |
103 |
Correct |
546 ms |
113228 KB |
n=200000 |
104 |
Correct |
565 ms |
113228 KB |
n=200000 |
105 |
Correct |
529 ms |
113228 KB |
n=200000 |
106 |
Correct |
530 ms |
113228 KB |
n=200000 |
107 |
Correct |
535 ms |
113228 KB |
n=200000 |
108 |
Correct |
1079 ms |
113228 KB |
n=200000 |
109 |
Correct |
1063 ms |
113228 KB |
n=200000 |
110 |
Correct |
1088 ms |
113228 KB |
n=200000 |
111 |
Correct |
925 ms |
113228 KB |
n=200000 |
112 |
Correct |
1411 ms |
113228 KB |
n=200000 |
113 |
Correct |
1549 ms |
113228 KB |
n=200000 |
114 |
Correct |
1000 ms |
113228 KB |
n=200000 |
115 |
Correct |
1469 ms |
113228 KB |
n=200000 |
116 |
Correct |
996 ms |
113228 KB |
n=200000 |
117 |
Correct |
1425 ms |
113228 KB |
n=200000 |
118 |
Correct |
1468 ms |
113228 KB |
n=200000 |
119 |
Correct |
957 ms |
113228 KB |
n=200000 |
120 |
Correct |
1385 ms |
113228 KB |
n=200000 |
121 |
Correct |
1355 ms |
113228 KB |
n=200000 |
122 |
Correct |
1361 ms |
113576 KB |
n=200000 |
123 |
Correct |
547 ms |
113576 KB |
n=200000 |
124 |
Correct |
311 ms |
113576 KB |
n=25264 |