#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n,m,q,u,v,a[N],par[N][20],h[N],type,l,r,id;
vector<int> adj[N];
set<int> pos1[N],pos2[N];
void dfs(int u,int p){
for(int v: adj[u]){
if(v != p){
h[v] = h[u] + 1;
par[v][0] = u;
dfs(v, u);
}
}
}
int lca(int u,int v){
if(h[u] < h[v]) swap(u, v);
for(int i=18;i>=0;i--){
if(h[u] - (1<<i) >= h[v]){
u = par[u][i];
}
}
if(u == v){
return u;
}else{
for(int i=18;i>=0;i--){
if(par[u][i] != -1 && par[v][i] != -1 && par[u][i] != par[v][i]){
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for(int i=1;i<n;i++){
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
h[1] = 1;
memset(par, -1, sizeof par);
dfs(1, 1);
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i<=n;i++){
if(par[i][j-1] != -1){
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
for(int i=1;i<=m;i++){
cin >> a[i];
pos1[a[i]].insert(i);
}
for(int i=1;i<m;i++){
u = lca(a[i], a[i+1]);
pos2[u].insert(i);
}
while(q--){
cin >> type;
if(type == 1){
cin >> id >> v;
pos1[a[id]].erase(id);
if(id > 1){
u = lca(a[id-1], a[id]);
pos2[u].erase(id-1);
}
if(id < m){
u = lca(a[id], a[id+1]);
pos2[u].erase(id);
}
a[id] = v;
pos1[a[id]].insert(id);
if(id > 1){
u = lca(a[id-1], a[id]);
pos2[u].insert(id-1);
}
if(id < m){
u = lca(a[id], a[id+1]);
pos2[u].insert(id);
}
}
if(type == 2){
cin >> l >> r >> v;
if(pos1[v].size() > 0){
auto it = pos1[v].lower_bound(l);
if(it != pos1[v].end() && *it <= r){
cout << *it << " " << *it << "\n";
continue;
}
}
if(pos2[v].size() > 0){
auto it1 = pos2[v].lower_bound(l);
if(it1 != pos2[v].end() && *it1 < r){
cout << *it1 << " " << (*it1) + 1 << "\n";
continue;
}
}
cout << "-1 -1\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39416 KB |
n=5 |
2 |
Correct |
38 ms |
39544 KB |
n=100 |
3 |
Correct |
38 ms |
39544 KB |
n=100 |
4 |
Correct |
39 ms |
39544 KB |
n=100 |
5 |
Correct |
38 ms |
39544 KB |
n=100 |
6 |
Correct |
38 ms |
39492 KB |
n=100 |
7 |
Correct |
38 ms |
39544 KB |
n=100 |
8 |
Correct |
38 ms |
39528 KB |
n=100 |
9 |
Correct |
39 ms |
39544 KB |
n=100 |
10 |
Correct |
39 ms |
39672 KB |
n=100 |
11 |
Correct |
38 ms |
39544 KB |
n=100 |
12 |
Correct |
39 ms |
39544 KB |
n=100 |
13 |
Correct |
38 ms |
39420 KB |
n=100 |
14 |
Correct |
38 ms |
39512 KB |
n=100 |
15 |
Correct |
39 ms |
39428 KB |
n=100 |
16 |
Correct |
38 ms |
39516 KB |
n=100 |
17 |
Correct |
38 ms |
39508 KB |
n=100 |
18 |
Correct |
39 ms |
39544 KB |
n=100 |
19 |
Correct |
38 ms |
39416 KB |
n=100 |
20 |
Correct |
38 ms |
39544 KB |
n=100 |
21 |
Correct |
38 ms |
39480 KB |
n=100 |
22 |
Correct |
38 ms |
39544 KB |
n=100 |
23 |
Correct |
39 ms |
39420 KB |
n=100 |
24 |
Correct |
39 ms |
39544 KB |
n=100 |
25 |
Correct |
38 ms |
39544 KB |
n=100 |
26 |
Correct |
38 ms |
39544 KB |
n=12 |
27 |
Correct |
38 ms |
39544 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39416 KB |
n=5 |
2 |
Correct |
38 ms |
39544 KB |
n=100 |
3 |
Correct |
38 ms |
39544 KB |
n=100 |
4 |
Correct |
39 ms |
39544 KB |
n=100 |
5 |
Correct |
38 ms |
39544 KB |
n=100 |
6 |
Correct |
38 ms |
39492 KB |
n=100 |
7 |
Correct |
38 ms |
39544 KB |
n=100 |
8 |
Correct |
38 ms |
39528 KB |
n=100 |
9 |
Correct |
39 ms |
39544 KB |
n=100 |
10 |
Correct |
39 ms |
39672 KB |
n=100 |
11 |
Correct |
38 ms |
39544 KB |
n=100 |
12 |
Correct |
39 ms |
39544 KB |
n=100 |
13 |
Correct |
38 ms |
39420 KB |
n=100 |
14 |
Correct |
38 ms |
39512 KB |
n=100 |
15 |
Correct |
39 ms |
39428 KB |
n=100 |
16 |
Correct |
38 ms |
39516 KB |
n=100 |
17 |
Correct |
38 ms |
39508 KB |
n=100 |
18 |
Correct |
39 ms |
39544 KB |
n=100 |
19 |
Correct |
38 ms |
39416 KB |
n=100 |
20 |
Correct |
38 ms |
39544 KB |
n=100 |
21 |
Correct |
38 ms |
39480 KB |
n=100 |
22 |
Correct |
38 ms |
39544 KB |
n=100 |
23 |
Correct |
39 ms |
39420 KB |
n=100 |
24 |
Correct |
39 ms |
39544 KB |
n=100 |
25 |
Correct |
38 ms |
39544 KB |
n=100 |
26 |
Correct |
38 ms |
39544 KB |
n=12 |
27 |
Correct |
38 ms |
39544 KB |
n=100 |
28 |
Correct |
40 ms |
39544 KB |
n=500 |
29 |
Correct |
39 ms |
39544 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
40 ms |
39672 KB |
n=500 |
32 |
Correct |
39 ms |
39548 KB |
n=500 |
33 |
Correct |
40 ms |
39544 KB |
n=500 |
34 |
Correct |
40 ms |
39568 KB |
n=500 |
35 |
Correct |
39 ms |
39544 KB |
n=500 |
36 |
Correct |
40 ms |
39544 KB |
n=500 |
37 |
Correct |
39 ms |
39568 KB |
n=500 |
38 |
Correct |
39 ms |
39544 KB |
n=500 |
39 |
Correct |
39 ms |
39520 KB |
n=500 |
40 |
Correct |
39 ms |
39648 KB |
n=500 |
41 |
Correct |
40 ms |
39672 KB |
n=500 |
42 |
Correct |
40 ms |
39544 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
40 ms |
39544 KB |
n=500 |
45 |
Correct |
39 ms |
39544 KB |
n=500 |
46 |
Correct |
39 ms |
39544 KB |
n=500 |
47 |
Correct |
39 ms |
39544 KB |
n=500 |
48 |
Correct |
40 ms |
39544 KB |
n=500 |
49 |
Correct |
39 ms |
39548 KB |
n=500 |
50 |
Correct |
39 ms |
39524 KB |
n=500 |
51 |
Correct |
39 ms |
39544 KB |
n=500 |
52 |
Correct |
39 ms |
39608 KB |
n=500 |
53 |
Correct |
40 ms |
39544 KB |
n=500 |
54 |
Correct |
39 ms |
39548 KB |
n=500 |
55 |
Correct |
39 ms |
39544 KB |
n=278 |
56 |
Correct |
39 ms |
39544 KB |
n=500 |
57 |
Correct |
39 ms |
39672 KB |
n=500 |
58 |
Correct |
39 ms |
39540 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39416 KB |
n=5 |
2 |
Correct |
38 ms |
39544 KB |
n=100 |
3 |
Correct |
38 ms |
39544 KB |
n=100 |
4 |
Correct |
39 ms |
39544 KB |
n=100 |
5 |
Correct |
38 ms |
39544 KB |
n=100 |
6 |
Correct |
38 ms |
39492 KB |
n=100 |
7 |
Correct |
38 ms |
39544 KB |
n=100 |
8 |
Correct |
38 ms |
39528 KB |
n=100 |
9 |
Correct |
39 ms |
39544 KB |
n=100 |
10 |
Correct |
39 ms |
39672 KB |
n=100 |
11 |
Correct |
38 ms |
39544 KB |
n=100 |
12 |
Correct |
39 ms |
39544 KB |
n=100 |
13 |
Correct |
38 ms |
39420 KB |
n=100 |
14 |
Correct |
38 ms |
39512 KB |
n=100 |
15 |
Correct |
39 ms |
39428 KB |
n=100 |
16 |
Correct |
38 ms |
39516 KB |
n=100 |
17 |
Correct |
38 ms |
39508 KB |
n=100 |
18 |
Correct |
39 ms |
39544 KB |
n=100 |
19 |
Correct |
38 ms |
39416 KB |
n=100 |
20 |
Correct |
38 ms |
39544 KB |
n=100 |
21 |
Correct |
38 ms |
39480 KB |
n=100 |
22 |
Correct |
38 ms |
39544 KB |
n=100 |
23 |
Correct |
39 ms |
39420 KB |
n=100 |
24 |
Correct |
39 ms |
39544 KB |
n=100 |
25 |
Correct |
38 ms |
39544 KB |
n=100 |
26 |
Correct |
38 ms |
39544 KB |
n=12 |
27 |
Correct |
38 ms |
39544 KB |
n=100 |
28 |
Correct |
40 ms |
39544 KB |
n=500 |
29 |
Correct |
39 ms |
39544 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
40 ms |
39672 KB |
n=500 |
32 |
Correct |
39 ms |
39548 KB |
n=500 |
33 |
Correct |
40 ms |
39544 KB |
n=500 |
34 |
Correct |
40 ms |
39568 KB |
n=500 |
35 |
Correct |
39 ms |
39544 KB |
n=500 |
36 |
Correct |
40 ms |
39544 KB |
n=500 |
37 |
Correct |
39 ms |
39568 KB |
n=500 |
38 |
Correct |
39 ms |
39544 KB |
n=500 |
39 |
Correct |
39 ms |
39520 KB |
n=500 |
40 |
Correct |
39 ms |
39648 KB |
n=500 |
41 |
Correct |
40 ms |
39672 KB |
n=500 |
42 |
Correct |
40 ms |
39544 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
40 ms |
39544 KB |
n=500 |
45 |
Correct |
39 ms |
39544 KB |
n=500 |
46 |
Correct |
39 ms |
39544 KB |
n=500 |
47 |
Correct |
39 ms |
39544 KB |
n=500 |
48 |
Correct |
40 ms |
39544 KB |
n=500 |
49 |
Correct |
39 ms |
39548 KB |
n=500 |
50 |
Correct |
39 ms |
39524 KB |
n=500 |
51 |
Correct |
39 ms |
39544 KB |
n=500 |
52 |
Correct |
39 ms |
39608 KB |
n=500 |
53 |
Correct |
40 ms |
39544 KB |
n=500 |
54 |
Correct |
39 ms |
39548 KB |
n=500 |
55 |
Correct |
39 ms |
39544 KB |
n=278 |
56 |
Correct |
39 ms |
39544 KB |
n=500 |
57 |
Correct |
39 ms |
39672 KB |
n=500 |
58 |
Correct |
39 ms |
39540 KB |
n=500 |
59 |
Correct |
42 ms |
39828 KB |
n=2000 |
60 |
Correct |
42 ms |
39900 KB |
n=2000 |
61 |
Correct |
43 ms |
39928 KB |
n=2000 |
62 |
Correct |
43 ms |
39808 KB |
n=2000 |
63 |
Correct |
43 ms |
39800 KB |
n=2000 |
64 |
Correct |
43 ms |
39800 KB |
n=2000 |
65 |
Correct |
43 ms |
39800 KB |
n=2000 |
66 |
Correct |
43 ms |
39928 KB |
n=2000 |
67 |
Correct |
43 ms |
39928 KB |
n=2000 |
68 |
Correct |
42 ms |
39800 KB |
n=2000 |
69 |
Correct |
41 ms |
39800 KB |
n=2000 |
70 |
Correct |
41 ms |
39800 KB |
n=2000 |
71 |
Correct |
41 ms |
39800 KB |
n=2000 |
72 |
Correct |
41 ms |
39800 KB |
n=2000 |
73 |
Correct |
40 ms |
39800 KB |
n=2000 |
74 |
Correct |
42 ms |
39804 KB |
n=1844 |
75 |
Correct |
41 ms |
39800 KB |
n=2000 |
76 |
Correct |
43 ms |
39800 KB |
n=2000 |
77 |
Correct |
42 ms |
39800 KB |
n=2000 |
78 |
Correct |
43 ms |
39904 KB |
n=2000 |
79 |
Correct |
42 ms |
39928 KB |
n=2000 |
80 |
Correct |
42 ms |
39972 KB |
n=2000 |
81 |
Correct |
43 ms |
39928 KB |
n=2000 |
82 |
Correct |
42 ms |
39800 KB |
n=2000 |
83 |
Correct |
42 ms |
39928 KB |
n=2000 |
84 |
Correct |
42 ms |
39800 KB |
n=2000 |
85 |
Correct |
42 ms |
39800 KB |
n=2000 |
86 |
Correct |
43 ms |
39800 KB |
n=2000 |
87 |
Correct |
42 ms |
39800 KB |
n=2000 |
88 |
Correct |
43 ms |
39928 KB |
n=2000 |
89 |
Correct |
42 ms |
39928 KB |
n=2000 |
90 |
Correct |
43 ms |
39928 KB |
n=2000 |
91 |
Correct |
42 ms |
39928 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39416 KB |
n=5 |
2 |
Correct |
38 ms |
39544 KB |
n=100 |
3 |
Correct |
38 ms |
39544 KB |
n=100 |
4 |
Correct |
39 ms |
39544 KB |
n=100 |
5 |
Correct |
38 ms |
39544 KB |
n=100 |
6 |
Correct |
38 ms |
39492 KB |
n=100 |
7 |
Correct |
38 ms |
39544 KB |
n=100 |
8 |
Correct |
38 ms |
39528 KB |
n=100 |
9 |
Correct |
39 ms |
39544 KB |
n=100 |
10 |
Correct |
39 ms |
39672 KB |
n=100 |
11 |
Correct |
38 ms |
39544 KB |
n=100 |
12 |
Correct |
39 ms |
39544 KB |
n=100 |
13 |
Correct |
38 ms |
39420 KB |
n=100 |
14 |
Correct |
38 ms |
39512 KB |
n=100 |
15 |
Correct |
39 ms |
39428 KB |
n=100 |
16 |
Correct |
38 ms |
39516 KB |
n=100 |
17 |
Correct |
38 ms |
39508 KB |
n=100 |
18 |
Correct |
39 ms |
39544 KB |
n=100 |
19 |
Correct |
38 ms |
39416 KB |
n=100 |
20 |
Correct |
38 ms |
39544 KB |
n=100 |
21 |
Correct |
38 ms |
39480 KB |
n=100 |
22 |
Correct |
38 ms |
39544 KB |
n=100 |
23 |
Correct |
39 ms |
39420 KB |
n=100 |
24 |
Correct |
39 ms |
39544 KB |
n=100 |
25 |
Correct |
38 ms |
39544 KB |
n=100 |
26 |
Correct |
38 ms |
39544 KB |
n=12 |
27 |
Correct |
38 ms |
39544 KB |
n=100 |
28 |
Correct |
40 ms |
39544 KB |
n=500 |
29 |
Correct |
39 ms |
39544 KB |
n=500 |
30 |
Correct |
40 ms |
39544 KB |
n=500 |
31 |
Correct |
40 ms |
39672 KB |
n=500 |
32 |
Correct |
39 ms |
39548 KB |
n=500 |
33 |
Correct |
40 ms |
39544 KB |
n=500 |
34 |
Correct |
40 ms |
39568 KB |
n=500 |
35 |
Correct |
39 ms |
39544 KB |
n=500 |
36 |
Correct |
40 ms |
39544 KB |
n=500 |
37 |
Correct |
39 ms |
39568 KB |
n=500 |
38 |
Correct |
39 ms |
39544 KB |
n=500 |
39 |
Correct |
39 ms |
39520 KB |
n=500 |
40 |
Correct |
39 ms |
39648 KB |
n=500 |
41 |
Correct |
40 ms |
39672 KB |
n=500 |
42 |
Correct |
40 ms |
39544 KB |
n=500 |
43 |
Correct |
40 ms |
39544 KB |
n=500 |
44 |
Correct |
40 ms |
39544 KB |
n=500 |
45 |
Correct |
39 ms |
39544 KB |
n=500 |
46 |
Correct |
39 ms |
39544 KB |
n=500 |
47 |
Correct |
39 ms |
39544 KB |
n=500 |
48 |
Correct |
40 ms |
39544 KB |
n=500 |
49 |
Correct |
39 ms |
39548 KB |
n=500 |
50 |
Correct |
39 ms |
39524 KB |
n=500 |
51 |
Correct |
39 ms |
39544 KB |
n=500 |
52 |
Correct |
39 ms |
39608 KB |
n=500 |
53 |
Correct |
40 ms |
39544 KB |
n=500 |
54 |
Correct |
39 ms |
39548 KB |
n=500 |
55 |
Correct |
39 ms |
39544 KB |
n=278 |
56 |
Correct |
39 ms |
39544 KB |
n=500 |
57 |
Correct |
39 ms |
39672 KB |
n=500 |
58 |
Correct |
39 ms |
39540 KB |
n=500 |
59 |
Correct |
42 ms |
39828 KB |
n=2000 |
60 |
Correct |
42 ms |
39900 KB |
n=2000 |
61 |
Correct |
43 ms |
39928 KB |
n=2000 |
62 |
Correct |
43 ms |
39808 KB |
n=2000 |
63 |
Correct |
43 ms |
39800 KB |
n=2000 |
64 |
Correct |
43 ms |
39800 KB |
n=2000 |
65 |
Correct |
43 ms |
39800 KB |
n=2000 |
66 |
Correct |
43 ms |
39928 KB |
n=2000 |
67 |
Correct |
43 ms |
39928 KB |
n=2000 |
68 |
Correct |
42 ms |
39800 KB |
n=2000 |
69 |
Correct |
41 ms |
39800 KB |
n=2000 |
70 |
Correct |
41 ms |
39800 KB |
n=2000 |
71 |
Correct |
41 ms |
39800 KB |
n=2000 |
72 |
Correct |
41 ms |
39800 KB |
n=2000 |
73 |
Correct |
40 ms |
39800 KB |
n=2000 |
74 |
Correct |
42 ms |
39804 KB |
n=1844 |
75 |
Correct |
41 ms |
39800 KB |
n=2000 |
76 |
Correct |
43 ms |
39800 KB |
n=2000 |
77 |
Correct |
42 ms |
39800 KB |
n=2000 |
78 |
Correct |
43 ms |
39904 KB |
n=2000 |
79 |
Correct |
42 ms |
39928 KB |
n=2000 |
80 |
Correct |
42 ms |
39972 KB |
n=2000 |
81 |
Correct |
43 ms |
39928 KB |
n=2000 |
82 |
Correct |
42 ms |
39800 KB |
n=2000 |
83 |
Correct |
42 ms |
39928 KB |
n=2000 |
84 |
Correct |
42 ms |
39800 KB |
n=2000 |
85 |
Correct |
42 ms |
39800 KB |
n=2000 |
86 |
Correct |
43 ms |
39800 KB |
n=2000 |
87 |
Correct |
42 ms |
39800 KB |
n=2000 |
88 |
Correct |
43 ms |
39928 KB |
n=2000 |
89 |
Correct |
42 ms |
39928 KB |
n=2000 |
90 |
Correct |
43 ms |
39928 KB |
n=2000 |
91 |
Correct |
42 ms |
39928 KB |
n=2000 |
92 |
Correct |
854 ms |
74632 KB |
n=200000 |
93 |
Correct |
1209 ms |
79528 KB |
n=200000 |
94 |
Correct |
1219 ms |
82940 KB |
n=200000 |
95 |
Correct |
874 ms |
74576 KB |
n=200000 |
96 |
Correct |
833 ms |
74476 KB |
n=200000 |
97 |
Correct |
1285 ms |
78712 KB |
n=200000 |
98 |
Correct |
913 ms |
74636 KB |
n=200000 |
99 |
Correct |
1071 ms |
74844 KB |
n=200000 |
100 |
Correct |
876 ms |
74680 KB |
n=200000 |
101 |
Correct |
1341 ms |
84296 KB |
n=200000 |
102 |
Correct |
553 ms |
75628 KB |
n=200000 |
103 |
Correct |
556 ms |
75652 KB |
n=200000 |
104 |
Correct |
588 ms |
75660 KB |
n=200000 |
105 |
Correct |
538 ms |
76048 KB |
n=200000 |
106 |
Correct |
558 ms |
76280 KB |
n=200000 |
107 |
Correct |
527 ms |
76096 KB |
n=200000 |
108 |
Correct |
985 ms |
74488 KB |
n=200000 |
109 |
Correct |
969 ms |
74452 KB |
n=200000 |
110 |
Correct |
960 ms |
74820 KB |
n=200000 |
111 |
Correct |
843 ms |
74092 KB |
n=200000 |
112 |
Correct |
1292 ms |
83208 KB |
n=200000 |
113 |
Correct |
1303 ms |
78748 KB |
n=200000 |
114 |
Correct |
879 ms |
73960 KB |
n=200000 |
115 |
Correct |
1279 ms |
76412 KB |
n=200000 |
116 |
Correct |
887 ms |
74620 KB |
n=200000 |
117 |
Correct |
1247 ms |
83640 KB |
n=200000 |
118 |
Correct |
1289 ms |
77452 KB |
n=200000 |
119 |
Correct |
872 ms |
74744 KB |
n=200000 |
120 |
Correct |
1182 ms |
82996 KB |
n=200000 |
121 |
Correct |
1161 ms |
83264 KB |
n=200000 |
122 |
Correct |
1170 ms |
83452 KB |
n=200000 |
123 |
Correct |
517 ms |
75896 KB |
n=200000 |
124 |
Correct |
303 ms |
52572 KB |
n=25264 |