#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
const int LOG=18;
set<pair<int,int> > lis[N];
vector<int> adj[N];
int level[N],ancestor[LOG][N],ar[N],lef,rig;
void dfs(int x,int p){
for(int i=1;i<LOG;i++){
ancestor[i][x]=ancestor[i-1][ancestor[i-1][x]];
}
for(auto i:adj[x]){
if(i!=p){
level[i]=level[x]+1;
ancestor[0][i]=x;
dfs(i,x);
}
}
}
int findd(int x,int k){
while(k){
int y=__lg(k);
x=ancestor[y][x];
k-=(1<<y);
}
return x;
}
int LCA(int x,int y){
if(level[x]>level[y]){
x=findd(x,level[x]-level[y]);
}
y=findd(y,level[y]-level[x]);
if(x==y){
return x;
}
for(int i=LOG-1;i>-1;i--){
if(ancestor[i][x]!=ancestor[i][y]){
x=ancestor[i][x],y=ancestor[i][y];
}
}
return ancestor[0][x];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,i,j,k,l,m,q;
cin>>n>>m>>q;
for(i=1;i<n;i++){
cin>>j>>k;
adj[j].push_back(k);
adj[k].push_back(j);
}
dfs(1,1);
for(i=1;i<=m;i++){
cin>>ar[i];
lis[ar[i]].insert({i,i});
if(i>1){
lis[LCA(ar[i-1],ar[i])].insert({i-1,i});
}
}
for(i=1;i<=q;i++){
cin>>j;
if(j==1){
cin>>k>>l;
lis[ar[k]].erase({k,k});
if(k>1){
lis[LCA(ar[k-1],ar[k])].erase({k-1,k});
}
if(k<m){
lis[LCA(ar[k],ar[k+1])].erase({k,k+1});
}
ar[k]=l;
lis[ar[k]].insert({k,k});
if(k>1){
lis[LCA(ar[k-1],ar[k])].insert({k-1,k});
}
if(k<m){
lis[LCA(ar[k],ar[k+1])].insert({k,k+1});
}
}
else{
cin>>j>>k>>l;
auto ite=lis[l].lower_bound({j,0});
if(ite==lis[l].end()){
lef=rig=-1;
}
else{
if(ite->second<=k){
lef=ite->first;
rig=ite->second;
}
else{
lef=rig=-1;
}
}
cout<<lef<<' '<<rig<<'\n';
}
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14584 KB |
n=5 |
2 |
Correct |
14 ms |
14584 KB |
n=100 |
3 |
Correct |
14 ms |
14584 KB |
n=100 |
4 |
Correct |
14 ms |
14584 KB |
n=100 |
5 |
Correct |
14 ms |
14584 KB |
n=100 |
6 |
Correct |
14 ms |
14584 KB |
n=100 |
7 |
Correct |
14 ms |
14584 KB |
n=100 |
8 |
Correct |
14 ms |
14584 KB |
n=100 |
9 |
Correct |
14 ms |
14584 KB |
n=100 |
10 |
Correct |
14 ms |
14584 KB |
n=100 |
11 |
Correct |
15 ms |
14592 KB |
n=100 |
12 |
Correct |
14 ms |
14588 KB |
n=100 |
13 |
Correct |
14 ms |
14584 KB |
n=100 |
14 |
Correct |
14 ms |
14584 KB |
n=100 |
15 |
Correct |
14 ms |
14584 KB |
n=100 |
16 |
Correct |
14 ms |
14584 KB |
n=100 |
17 |
Correct |
14 ms |
14584 KB |
n=100 |
18 |
Correct |
15 ms |
14584 KB |
n=100 |
19 |
Correct |
14 ms |
14584 KB |
n=100 |
20 |
Correct |
14 ms |
14584 KB |
n=100 |
21 |
Correct |
14 ms |
14584 KB |
n=100 |
22 |
Correct |
14 ms |
14588 KB |
n=100 |
23 |
Correct |
14 ms |
14584 KB |
n=100 |
24 |
Correct |
14 ms |
14584 KB |
n=100 |
25 |
Correct |
14 ms |
14584 KB |
n=100 |
26 |
Correct |
14 ms |
14584 KB |
n=12 |
27 |
Correct |
14 ms |
14584 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14584 KB |
n=5 |
2 |
Correct |
14 ms |
14584 KB |
n=100 |
3 |
Correct |
14 ms |
14584 KB |
n=100 |
4 |
Correct |
14 ms |
14584 KB |
n=100 |
5 |
Correct |
14 ms |
14584 KB |
n=100 |
6 |
Correct |
14 ms |
14584 KB |
n=100 |
7 |
Correct |
14 ms |
14584 KB |
n=100 |
8 |
Correct |
14 ms |
14584 KB |
n=100 |
9 |
Correct |
14 ms |
14584 KB |
n=100 |
10 |
Correct |
14 ms |
14584 KB |
n=100 |
11 |
Correct |
15 ms |
14592 KB |
n=100 |
12 |
Correct |
14 ms |
14588 KB |
n=100 |
13 |
Correct |
14 ms |
14584 KB |
n=100 |
14 |
Correct |
14 ms |
14584 KB |
n=100 |
15 |
Correct |
14 ms |
14584 KB |
n=100 |
16 |
Correct |
14 ms |
14584 KB |
n=100 |
17 |
Correct |
14 ms |
14584 KB |
n=100 |
18 |
Correct |
15 ms |
14584 KB |
n=100 |
19 |
Correct |
14 ms |
14584 KB |
n=100 |
20 |
Correct |
14 ms |
14584 KB |
n=100 |
21 |
Correct |
14 ms |
14584 KB |
n=100 |
22 |
Correct |
14 ms |
14588 KB |
n=100 |
23 |
Correct |
14 ms |
14584 KB |
n=100 |
24 |
Correct |
14 ms |
14584 KB |
n=100 |
25 |
Correct |
14 ms |
14584 KB |
n=100 |
26 |
Correct |
14 ms |
14584 KB |
n=12 |
27 |
Correct |
14 ms |
14584 KB |
n=100 |
28 |
Correct |
15 ms |
14584 KB |
n=500 |
29 |
Correct |
15 ms |
14712 KB |
n=500 |
30 |
Correct |
15 ms |
14688 KB |
n=500 |
31 |
Correct |
15 ms |
14584 KB |
n=500 |
32 |
Correct |
15 ms |
14712 KB |
n=500 |
33 |
Correct |
15 ms |
14584 KB |
n=500 |
34 |
Correct |
14 ms |
14584 KB |
n=500 |
35 |
Correct |
15 ms |
14840 KB |
n=500 |
36 |
Correct |
15 ms |
14740 KB |
n=500 |
37 |
Correct |
15 ms |
14712 KB |
n=500 |
38 |
Correct |
15 ms |
14712 KB |
n=500 |
39 |
Correct |
15 ms |
14712 KB |
n=500 |
40 |
Correct |
15 ms |
14684 KB |
n=500 |
41 |
Correct |
15 ms |
14584 KB |
n=500 |
42 |
Correct |
15 ms |
14712 KB |
n=500 |
43 |
Correct |
15 ms |
14584 KB |
n=500 |
44 |
Correct |
15 ms |
14588 KB |
n=500 |
45 |
Correct |
15 ms |
14712 KB |
n=500 |
46 |
Correct |
15 ms |
14584 KB |
n=500 |
47 |
Correct |
15 ms |
14584 KB |
n=500 |
48 |
Correct |
15 ms |
14584 KB |
n=500 |
49 |
Correct |
15 ms |
14584 KB |
n=500 |
50 |
Correct |
15 ms |
14712 KB |
n=500 |
51 |
Correct |
15 ms |
14596 KB |
n=500 |
52 |
Correct |
15 ms |
14716 KB |
n=500 |
53 |
Correct |
15 ms |
14712 KB |
n=500 |
54 |
Correct |
15 ms |
14712 KB |
n=500 |
55 |
Correct |
15 ms |
14584 KB |
n=278 |
56 |
Correct |
15 ms |
14712 KB |
n=500 |
57 |
Correct |
15 ms |
14712 KB |
n=500 |
58 |
Correct |
15 ms |
14584 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14584 KB |
n=5 |
2 |
Correct |
14 ms |
14584 KB |
n=100 |
3 |
Correct |
14 ms |
14584 KB |
n=100 |
4 |
Correct |
14 ms |
14584 KB |
n=100 |
5 |
Correct |
14 ms |
14584 KB |
n=100 |
6 |
Correct |
14 ms |
14584 KB |
n=100 |
7 |
Correct |
14 ms |
14584 KB |
n=100 |
8 |
Correct |
14 ms |
14584 KB |
n=100 |
9 |
Correct |
14 ms |
14584 KB |
n=100 |
10 |
Correct |
14 ms |
14584 KB |
n=100 |
11 |
Correct |
15 ms |
14592 KB |
n=100 |
12 |
Correct |
14 ms |
14588 KB |
n=100 |
13 |
Correct |
14 ms |
14584 KB |
n=100 |
14 |
Correct |
14 ms |
14584 KB |
n=100 |
15 |
Correct |
14 ms |
14584 KB |
n=100 |
16 |
Correct |
14 ms |
14584 KB |
n=100 |
17 |
Correct |
14 ms |
14584 KB |
n=100 |
18 |
Correct |
15 ms |
14584 KB |
n=100 |
19 |
Correct |
14 ms |
14584 KB |
n=100 |
20 |
Correct |
14 ms |
14584 KB |
n=100 |
21 |
Correct |
14 ms |
14584 KB |
n=100 |
22 |
Correct |
14 ms |
14588 KB |
n=100 |
23 |
Correct |
14 ms |
14584 KB |
n=100 |
24 |
Correct |
14 ms |
14584 KB |
n=100 |
25 |
Correct |
14 ms |
14584 KB |
n=100 |
26 |
Correct |
14 ms |
14584 KB |
n=12 |
27 |
Correct |
14 ms |
14584 KB |
n=100 |
28 |
Correct |
15 ms |
14584 KB |
n=500 |
29 |
Correct |
15 ms |
14712 KB |
n=500 |
30 |
Correct |
15 ms |
14688 KB |
n=500 |
31 |
Correct |
15 ms |
14584 KB |
n=500 |
32 |
Correct |
15 ms |
14712 KB |
n=500 |
33 |
Correct |
15 ms |
14584 KB |
n=500 |
34 |
Correct |
14 ms |
14584 KB |
n=500 |
35 |
Correct |
15 ms |
14840 KB |
n=500 |
36 |
Correct |
15 ms |
14740 KB |
n=500 |
37 |
Correct |
15 ms |
14712 KB |
n=500 |
38 |
Correct |
15 ms |
14712 KB |
n=500 |
39 |
Correct |
15 ms |
14712 KB |
n=500 |
40 |
Correct |
15 ms |
14684 KB |
n=500 |
41 |
Correct |
15 ms |
14584 KB |
n=500 |
42 |
Correct |
15 ms |
14712 KB |
n=500 |
43 |
Correct |
15 ms |
14584 KB |
n=500 |
44 |
Correct |
15 ms |
14588 KB |
n=500 |
45 |
Correct |
15 ms |
14712 KB |
n=500 |
46 |
Correct |
15 ms |
14584 KB |
n=500 |
47 |
Correct |
15 ms |
14584 KB |
n=500 |
48 |
Correct |
15 ms |
14584 KB |
n=500 |
49 |
Correct |
15 ms |
14584 KB |
n=500 |
50 |
Correct |
15 ms |
14712 KB |
n=500 |
51 |
Correct |
15 ms |
14596 KB |
n=500 |
52 |
Correct |
15 ms |
14716 KB |
n=500 |
53 |
Correct |
15 ms |
14712 KB |
n=500 |
54 |
Correct |
15 ms |
14712 KB |
n=500 |
55 |
Correct |
15 ms |
14584 KB |
n=278 |
56 |
Correct |
15 ms |
14712 KB |
n=500 |
57 |
Correct |
15 ms |
14712 KB |
n=500 |
58 |
Correct |
15 ms |
14584 KB |
n=500 |
59 |
Correct |
18 ms |
14968 KB |
n=2000 |
60 |
Correct |
18 ms |
15016 KB |
n=2000 |
61 |
Correct |
18 ms |
14968 KB |
n=2000 |
62 |
Correct |
19 ms |
14972 KB |
n=2000 |
63 |
Correct |
18 ms |
14888 KB |
n=2000 |
64 |
Correct |
18 ms |
14968 KB |
n=2000 |
65 |
Correct |
18 ms |
14968 KB |
n=2000 |
66 |
Correct |
18 ms |
15096 KB |
n=2000 |
67 |
Correct |
19 ms |
14968 KB |
n=2000 |
68 |
Correct |
18 ms |
14968 KB |
n=2000 |
69 |
Correct |
17 ms |
14940 KB |
n=2000 |
70 |
Correct |
17 ms |
14968 KB |
n=2000 |
71 |
Correct |
17 ms |
14968 KB |
n=2000 |
72 |
Correct |
17 ms |
14968 KB |
n=2000 |
73 |
Correct |
17 ms |
14968 KB |
n=2000 |
74 |
Correct |
18 ms |
14968 KB |
n=1844 |
75 |
Correct |
17 ms |
14968 KB |
n=2000 |
76 |
Correct |
19 ms |
14968 KB |
n=2000 |
77 |
Correct |
18 ms |
14992 KB |
n=2000 |
78 |
Correct |
18 ms |
15096 KB |
n=2000 |
79 |
Correct |
18 ms |
14968 KB |
n=2000 |
80 |
Correct |
17 ms |
14968 KB |
n=2000 |
81 |
Correct |
18 ms |
15000 KB |
n=2000 |
82 |
Correct |
17 ms |
14968 KB |
n=2000 |
83 |
Correct |
18 ms |
15016 KB |
n=2000 |
84 |
Correct |
18 ms |
14968 KB |
n=2000 |
85 |
Correct |
18 ms |
15016 KB |
n=2000 |
86 |
Correct |
18 ms |
14968 KB |
n=2000 |
87 |
Correct |
19 ms |
14940 KB |
n=2000 |
88 |
Correct |
18 ms |
15096 KB |
n=2000 |
89 |
Correct |
18 ms |
15072 KB |
n=2000 |
90 |
Correct |
18 ms |
14968 KB |
n=2000 |
91 |
Correct |
17 ms |
14968 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14584 KB |
n=5 |
2 |
Correct |
14 ms |
14584 KB |
n=100 |
3 |
Correct |
14 ms |
14584 KB |
n=100 |
4 |
Correct |
14 ms |
14584 KB |
n=100 |
5 |
Correct |
14 ms |
14584 KB |
n=100 |
6 |
Correct |
14 ms |
14584 KB |
n=100 |
7 |
Correct |
14 ms |
14584 KB |
n=100 |
8 |
Correct |
14 ms |
14584 KB |
n=100 |
9 |
Correct |
14 ms |
14584 KB |
n=100 |
10 |
Correct |
14 ms |
14584 KB |
n=100 |
11 |
Correct |
15 ms |
14592 KB |
n=100 |
12 |
Correct |
14 ms |
14588 KB |
n=100 |
13 |
Correct |
14 ms |
14584 KB |
n=100 |
14 |
Correct |
14 ms |
14584 KB |
n=100 |
15 |
Correct |
14 ms |
14584 KB |
n=100 |
16 |
Correct |
14 ms |
14584 KB |
n=100 |
17 |
Correct |
14 ms |
14584 KB |
n=100 |
18 |
Correct |
15 ms |
14584 KB |
n=100 |
19 |
Correct |
14 ms |
14584 KB |
n=100 |
20 |
Correct |
14 ms |
14584 KB |
n=100 |
21 |
Correct |
14 ms |
14584 KB |
n=100 |
22 |
Correct |
14 ms |
14588 KB |
n=100 |
23 |
Correct |
14 ms |
14584 KB |
n=100 |
24 |
Correct |
14 ms |
14584 KB |
n=100 |
25 |
Correct |
14 ms |
14584 KB |
n=100 |
26 |
Correct |
14 ms |
14584 KB |
n=12 |
27 |
Correct |
14 ms |
14584 KB |
n=100 |
28 |
Correct |
15 ms |
14584 KB |
n=500 |
29 |
Correct |
15 ms |
14712 KB |
n=500 |
30 |
Correct |
15 ms |
14688 KB |
n=500 |
31 |
Correct |
15 ms |
14584 KB |
n=500 |
32 |
Correct |
15 ms |
14712 KB |
n=500 |
33 |
Correct |
15 ms |
14584 KB |
n=500 |
34 |
Correct |
14 ms |
14584 KB |
n=500 |
35 |
Correct |
15 ms |
14840 KB |
n=500 |
36 |
Correct |
15 ms |
14740 KB |
n=500 |
37 |
Correct |
15 ms |
14712 KB |
n=500 |
38 |
Correct |
15 ms |
14712 KB |
n=500 |
39 |
Correct |
15 ms |
14712 KB |
n=500 |
40 |
Correct |
15 ms |
14684 KB |
n=500 |
41 |
Correct |
15 ms |
14584 KB |
n=500 |
42 |
Correct |
15 ms |
14712 KB |
n=500 |
43 |
Correct |
15 ms |
14584 KB |
n=500 |
44 |
Correct |
15 ms |
14588 KB |
n=500 |
45 |
Correct |
15 ms |
14712 KB |
n=500 |
46 |
Correct |
15 ms |
14584 KB |
n=500 |
47 |
Correct |
15 ms |
14584 KB |
n=500 |
48 |
Correct |
15 ms |
14584 KB |
n=500 |
49 |
Correct |
15 ms |
14584 KB |
n=500 |
50 |
Correct |
15 ms |
14712 KB |
n=500 |
51 |
Correct |
15 ms |
14596 KB |
n=500 |
52 |
Correct |
15 ms |
14716 KB |
n=500 |
53 |
Correct |
15 ms |
14712 KB |
n=500 |
54 |
Correct |
15 ms |
14712 KB |
n=500 |
55 |
Correct |
15 ms |
14584 KB |
n=278 |
56 |
Correct |
15 ms |
14712 KB |
n=500 |
57 |
Correct |
15 ms |
14712 KB |
n=500 |
58 |
Correct |
15 ms |
14584 KB |
n=500 |
59 |
Correct |
18 ms |
14968 KB |
n=2000 |
60 |
Correct |
18 ms |
15016 KB |
n=2000 |
61 |
Correct |
18 ms |
14968 KB |
n=2000 |
62 |
Correct |
19 ms |
14972 KB |
n=2000 |
63 |
Correct |
18 ms |
14888 KB |
n=2000 |
64 |
Correct |
18 ms |
14968 KB |
n=2000 |
65 |
Correct |
18 ms |
14968 KB |
n=2000 |
66 |
Correct |
18 ms |
15096 KB |
n=2000 |
67 |
Correct |
19 ms |
14968 KB |
n=2000 |
68 |
Correct |
18 ms |
14968 KB |
n=2000 |
69 |
Correct |
17 ms |
14940 KB |
n=2000 |
70 |
Correct |
17 ms |
14968 KB |
n=2000 |
71 |
Correct |
17 ms |
14968 KB |
n=2000 |
72 |
Correct |
17 ms |
14968 KB |
n=2000 |
73 |
Correct |
17 ms |
14968 KB |
n=2000 |
74 |
Correct |
18 ms |
14968 KB |
n=1844 |
75 |
Correct |
17 ms |
14968 KB |
n=2000 |
76 |
Correct |
19 ms |
14968 KB |
n=2000 |
77 |
Correct |
18 ms |
14992 KB |
n=2000 |
78 |
Correct |
18 ms |
15096 KB |
n=2000 |
79 |
Correct |
18 ms |
14968 KB |
n=2000 |
80 |
Correct |
17 ms |
14968 KB |
n=2000 |
81 |
Correct |
18 ms |
15000 KB |
n=2000 |
82 |
Correct |
17 ms |
14968 KB |
n=2000 |
83 |
Correct |
18 ms |
15016 KB |
n=2000 |
84 |
Correct |
18 ms |
14968 KB |
n=2000 |
85 |
Correct |
18 ms |
15016 KB |
n=2000 |
86 |
Correct |
18 ms |
14968 KB |
n=2000 |
87 |
Correct |
19 ms |
14940 KB |
n=2000 |
88 |
Correct |
18 ms |
15096 KB |
n=2000 |
89 |
Correct |
18 ms |
15072 KB |
n=2000 |
90 |
Correct |
18 ms |
14968 KB |
n=2000 |
91 |
Correct |
17 ms |
14968 KB |
n=2000 |
92 |
Correct |
926 ms |
60652 KB |
n=200000 |
93 |
Correct |
1342 ms |
68324 KB |
n=200000 |
94 |
Correct |
1231 ms |
71316 KB |
n=200000 |
95 |
Correct |
968 ms |
61232 KB |
n=200000 |
96 |
Correct |
914 ms |
61116 KB |
n=200000 |
97 |
Correct |
1130 ms |
67420 KB |
n=200000 |
98 |
Correct |
895 ms |
61548 KB |
n=200000 |
99 |
Correct |
1219 ms |
63896 KB |
n=200000 |
100 |
Correct |
932 ms |
62960 KB |
n=200000 |
101 |
Correct |
1016 ms |
73208 KB |
n=200000 |
102 |
Correct |
590 ms |
64860 KB |
n=200000 |
103 |
Correct |
607 ms |
64760 KB |
n=200000 |
104 |
Correct |
524 ms |
64760 KB |
n=200000 |
105 |
Correct |
563 ms |
65144 KB |
n=200000 |
106 |
Correct |
571 ms |
65016 KB |
n=200000 |
107 |
Correct |
568 ms |
65144 KB |
n=200000 |
108 |
Correct |
970 ms |
63580 KB |
n=200000 |
109 |
Correct |
1031 ms |
63548 KB |
n=200000 |
110 |
Correct |
1006 ms |
63480 KB |
n=200000 |
111 |
Correct |
873 ms |
63008 KB |
n=200000 |
112 |
Correct |
1102 ms |
72184 KB |
n=200000 |
113 |
Correct |
1171 ms |
67704 KB |
n=200000 |
114 |
Correct |
881 ms |
63080 KB |
n=200000 |
115 |
Correct |
1295 ms |
65400 KB |
n=200000 |
116 |
Correct |
832 ms |
63160 KB |
n=200000 |
117 |
Correct |
1149 ms |
72496 KB |
n=200000 |
118 |
Correct |
1182 ms |
66552 KB |
n=200000 |
119 |
Correct |
815 ms |
63592 KB |
n=200000 |
120 |
Correct |
1031 ms |
72056 KB |
n=200000 |
121 |
Correct |
1112 ms |
72132 KB |
n=200000 |
122 |
Correct |
1035 ms |
72528 KB |
n=200000 |
123 |
Correct |
584 ms |
64888 KB |
n=200000 |
124 |
Correct |
211 ms |
29304 KB |
n=25264 |