#include<bits/stdc++.h>
using namespace std;
int n,m,q;
const int maxn=200000+10;
vector<int>adj[maxn];
int timea=0;
pair<int,int>stf[maxn];
int lca[19][maxn];
void dfs(int u,int par=0){
timea++;
stf[u].first=timea;
lca[0][u]=par;
for(auto x:adj[u]){
if(x==par){
continue;
}
dfs(x,u);
}
stf[u].second=timea;
}
void callca(){
for(int i=1;i<19;i++){
for(int j=1;j<=n;j++){
lca[i][j]=lca[i-1][lca[i-1][j]];
}
}
}
bool zird(int u,int v){
if(stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second){
return 1;
}
return 0;
}
int getlca(int u,int v){
if(zird(u,v)){
return v;
}
if(zird(v,u)){
return u;
}
for(int i=18;i>=0;i--){
if(lca[i][u]!=0&&!zird(v,lca[i][u])){
u=lca[i][u];
}
}
u=lca[0][u];
return u;
}
int kaf=(1<<18);
struct segment{
map<int,int>seg[(1<<19)];
void upd(int i,int v,int wher){
if(i==0){
return ;
}
if((i<<1)>=(1<<19)){
seg[i][v]=wher;
return upd((i>>1),v,wher);
}
if(seg[(i<<1)].count(v)==0){
seg[i][v]=seg[(i<<1)^1][v];
}
else if(seg[(i<<1)^1].count(v)==0){
seg[i][v]=seg[(i<<1)][v];
}
else{
seg[i][v]=min(seg[(i<<1)][v],seg[(i<<1)^1][v]);
}
return upd((i>>1),v,wher);
}
int pors(int i,int l,int r,int tl,int tr,int v){
if(l>r||l>tr||r<tl||seg[i].count(v)==0){
return m+2;
}
if(l>=tl&&r<=tr){
return seg[i][v];
}
int ma=(l+r)>>1;
int ret=pors((i<<1),l,ma,tl,tr,v);
if(ret==m+2){
ret=pors((i<<1)^1,ma+1,r,tl,tr,v);
}
return ret;
}
}seg;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
callca();
vector<int>allm(m);
for(int i=0;i<m;i++){
cin>>allm[i];
}
for(int i=0;i<m;i++){
seg.upd(kaf+i,allm[i],i);
if(i!=m-1){
seg.upd(kaf+i,getlca(allm[i],allm[i+1]),i);
}
}
for(int i=0;i<q;i++){
int qq;
cin>>qq;
if(qq==1){
int ind,v;
cin>>ind>>v;
ind--;
seg.upd(kaf+ind,allm[ind],m+2);
if(ind!=m-1){
seg.upd(kaf+ind,getlca(allm[ind],allm[ind+1]),m+2);
}
if(ind!=0){
seg.upd(kaf+ind-1,getlca(allm[ind],allm[ind-1]),m+2);
}
allm[ind]=v;
seg.upd(kaf+ind,allm[ind],ind);
if(ind!=m-1){
seg.upd(kaf+ind,getlca(allm[ind],allm[ind+1]),ind);
}
if(ind!=0){
seg.upd(kaf+ind-1,getlca(allm[ind],allm[ind-1]),ind-1);
}
}
else{
int l,r,v;
cin>>l>>r>>v;
l--;
r--;
if(allm[r]==v){
cout<<r+1<<" "<<r+1<<"\n";
continue;
}
int f=0;
for(int h=l;h<r;h++){
if(allm[h]==v){
f=1;
cout<<h+1<<" "<<h+1<<"\n";
break;
}
if(getlca(allm[h],allm[h+1])==v){
f=1;
cout<<h+1<<" "<<h+2<<"\n";
break;
}
}
if(f==0)
{
cout<<-1<<" "<<-1<<"\n";
}
/*
int w=seg.pors(1,0,kaf-1,l,r,v);
if(w==m+2){
cout<<-1<<" "<<-1<<"\n";
continue;
}
if(allm[w]==v){
cout<<w+1<<" "<<w+1<<"\n";
}
else{
cout<<w+1<<" "<<w+1+1<<"\n";
}*/
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
n=5 |
2 |
Correct |
18 ms |
29780 KB |
n=100 |
3 |
Correct |
18 ms |
29776 KB |
n=100 |
4 |
Correct |
17 ms |
29880 KB |
n=100 |
5 |
Correct |
16 ms |
29844 KB |
n=100 |
6 |
Correct |
18 ms |
29772 KB |
n=100 |
7 |
Correct |
15 ms |
29844 KB |
n=100 |
8 |
Correct |
16 ms |
29804 KB |
n=100 |
9 |
Correct |
15 ms |
29780 KB |
n=100 |
10 |
Correct |
16 ms |
29844 KB |
n=100 |
11 |
Correct |
16 ms |
29764 KB |
n=100 |
12 |
Correct |
18 ms |
29804 KB |
n=100 |
13 |
Correct |
17 ms |
29828 KB |
n=100 |
14 |
Correct |
17 ms |
29768 KB |
n=100 |
15 |
Correct |
16 ms |
29780 KB |
n=100 |
16 |
Correct |
16 ms |
29780 KB |
n=100 |
17 |
Correct |
16 ms |
29780 KB |
n=100 |
18 |
Correct |
18 ms |
29780 KB |
n=100 |
19 |
Correct |
17 ms |
29912 KB |
n=100 |
20 |
Correct |
19 ms |
29780 KB |
n=100 |
21 |
Correct |
17 ms |
29760 KB |
n=100 |
22 |
Correct |
15 ms |
29780 KB |
n=100 |
23 |
Correct |
16 ms |
29780 KB |
n=100 |
24 |
Correct |
18 ms |
29868 KB |
n=100 |
25 |
Correct |
17 ms |
29736 KB |
n=100 |
26 |
Correct |
16 ms |
29776 KB |
n=12 |
27 |
Correct |
16 ms |
29844 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
n=5 |
2 |
Correct |
18 ms |
29780 KB |
n=100 |
3 |
Correct |
18 ms |
29776 KB |
n=100 |
4 |
Correct |
17 ms |
29880 KB |
n=100 |
5 |
Correct |
16 ms |
29844 KB |
n=100 |
6 |
Correct |
18 ms |
29772 KB |
n=100 |
7 |
Correct |
15 ms |
29844 KB |
n=100 |
8 |
Correct |
16 ms |
29804 KB |
n=100 |
9 |
Correct |
15 ms |
29780 KB |
n=100 |
10 |
Correct |
16 ms |
29844 KB |
n=100 |
11 |
Correct |
16 ms |
29764 KB |
n=100 |
12 |
Correct |
18 ms |
29804 KB |
n=100 |
13 |
Correct |
17 ms |
29828 KB |
n=100 |
14 |
Correct |
17 ms |
29768 KB |
n=100 |
15 |
Correct |
16 ms |
29780 KB |
n=100 |
16 |
Correct |
16 ms |
29780 KB |
n=100 |
17 |
Correct |
16 ms |
29780 KB |
n=100 |
18 |
Correct |
18 ms |
29780 KB |
n=100 |
19 |
Correct |
17 ms |
29912 KB |
n=100 |
20 |
Correct |
19 ms |
29780 KB |
n=100 |
21 |
Correct |
17 ms |
29760 KB |
n=100 |
22 |
Correct |
15 ms |
29780 KB |
n=100 |
23 |
Correct |
16 ms |
29780 KB |
n=100 |
24 |
Correct |
18 ms |
29868 KB |
n=100 |
25 |
Correct |
17 ms |
29736 KB |
n=100 |
26 |
Correct |
16 ms |
29776 KB |
n=12 |
27 |
Correct |
16 ms |
29844 KB |
n=100 |
28 |
Correct |
20 ms |
30312 KB |
n=500 |
29 |
Correct |
20 ms |
30300 KB |
n=500 |
30 |
Correct |
22 ms |
30556 KB |
n=500 |
31 |
Correct |
23 ms |
30336 KB |
n=500 |
32 |
Correct |
20 ms |
30308 KB |
n=500 |
33 |
Correct |
25 ms |
30428 KB |
n=500 |
34 |
Correct |
23 ms |
30324 KB |
n=500 |
35 |
Correct |
22 ms |
30292 KB |
n=500 |
36 |
Correct |
21 ms |
30232 KB |
n=500 |
37 |
Correct |
21 ms |
30172 KB |
n=500 |
38 |
Correct |
25 ms |
30164 KB |
n=500 |
39 |
Correct |
20 ms |
30244 KB |
n=500 |
40 |
Correct |
20 ms |
30188 KB |
n=500 |
41 |
Correct |
20 ms |
30164 KB |
n=500 |
42 |
Correct |
23 ms |
30300 KB |
n=500 |
43 |
Correct |
28 ms |
30292 KB |
n=500 |
44 |
Correct |
23 ms |
30292 KB |
n=500 |
45 |
Correct |
18 ms |
30292 KB |
n=500 |
46 |
Correct |
20 ms |
30304 KB |
n=500 |
47 |
Correct |
20 ms |
30364 KB |
n=500 |
48 |
Correct |
20 ms |
30172 KB |
n=500 |
49 |
Correct |
26 ms |
30284 KB |
n=500 |
50 |
Correct |
27 ms |
30292 KB |
n=500 |
51 |
Correct |
22 ms |
30428 KB |
n=500 |
52 |
Correct |
22 ms |
30276 KB |
n=500 |
53 |
Correct |
22 ms |
30432 KB |
n=500 |
54 |
Correct |
22 ms |
30300 KB |
n=500 |
55 |
Correct |
18 ms |
30164 KB |
n=278 |
56 |
Correct |
22 ms |
30292 KB |
n=500 |
57 |
Correct |
25 ms |
30300 KB |
n=500 |
58 |
Correct |
22 ms |
30164 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
n=5 |
2 |
Correct |
18 ms |
29780 KB |
n=100 |
3 |
Correct |
18 ms |
29776 KB |
n=100 |
4 |
Correct |
17 ms |
29880 KB |
n=100 |
5 |
Correct |
16 ms |
29844 KB |
n=100 |
6 |
Correct |
18 ms |
29772 KB |
n=100 |
7 |
Correct |
15 ms |
29844 KB |
n=100 |
8 |
Correct |
16 ms |
29804 KB |
n=100 |
9 |
Correct |
15 ms |
29780 KB |
n=100 |
10 |
Correct |
16 ms |
29844 KB |
n=100 |
11 |
Correct |
16 ms |
29764 KB |
n=100 |
12 |
Correct |
18 ms |
29804 KB |
n=100 |
13 |
Correct |
17 ms |
29828 KB |
n=100 |
14 |
Correct |
17 ms |
29768 KB |
n=100 |
15 |
Correct |
16 ms |
29780 KB |
n=100 |
16 |
Correct |
16 ms |
29780 KB |
n=100 |
17 |
Correct |
16 ms |
29780 KB |
n=100 |
18 |
Correct |
18 ms |
29780 KB |
n=100 |
19 |
Correct |
17 ms |
29912 KB |
n=100 |
20 |
Correct |
19 ms |
29780 KB |
n=100 |
21 |
Correct |
17 ms |
29760 KB |
n=100 |
22 |
Correct |
15 ms |
29780 KB |
n=100 |
23 |
Correct |
16 ms |
29780 KB |
n=100 |
24 |
Correct |
18 ms |
29868 KB |
n=100 |
25 |
Correct |
17 ms |
29736 KB |
n=100 |
26 |
Correct |
16 ms |
29776 KB |
n=12 |
27 |
Correct |
16 ms |
29844 KB |
n=100 |
28 |
Correct |
20 ms |
30312 KB |
n=500 |
29 |
Correct |
20 ms |
30300 KB |
n=500 |
30 |
Correct |
22 ms |
30556 KB |
n=500 |
31 |
Correct |
23 ms |
30336 KB |
n=500 |
32 |
Correct |
20 ms |
30308 KB |
n=500 |
33 |
Correct |
25 ms |
30428 KB |
n=500 |
34 |
Correct |
23 ms |
30324 KB |
n=500 |
35 |
Correct |
22 ms |
30292 KB |
n=500 |
36 |
Correct |
21 ms |
30232 KB |
n=500 |
37 |
Correct |
21 ms |
30172 KB |
n=500 |
38 |
Correct |
25 ms |
30164 KB |
n=500 |
39 |
Correct |
20 ms |
30244 KB |
n=500 |
40 |
Correct |
20 ms |
30188 KB |
n=500 |
41 |
Correct |
20 ms |
30164 KB |
n=500 |
42 |
Correct |
23 ms |
30300 KB |
n=500 |
43 |
Correct |
28 ms |
30292 KB |
n=500 |
44 |
Correct |
23 ms |
30292 KB |
n=500 |
45 |
Correct |
18 ms |
30292 KB |
n=500 |
46 |
Correct |
20 ms |
30304 KB |
n=500 |
47 |
Correct |
20 ms |
30364 KB |
n=500 |
48 |
Correct |
20 ms |
30172 KB |
n=500 |
49 |
Correct |
26 ms |
30284 KB |
n=500 |
50 |
Correct |
27 ms |
30292 KB |
n=500 |
51 |
Correct |
22 ms |
30428 KB |
n=500 |
52 |
Correct |
22 ms |
30276 KB |
n=500 |
53 |
Correct |
22 ms |
30432 KB |
n=500 |
54 |
Correct |
22 ms |
30300 KB |
n=500 |
55 |
Correct |
18 ms |
30164 KB |
n=278 |
56 |
Correct |
22 ms |
30292 KB |
n=500 |
57 |
Correct |
25 ms |
30300 KB |
n=500 |
58 |
Correct |
22 ms |
30164 KB |
n=500 |
59 |
Correct |
39 ms |
32268 KB |
n=2000 |
60 |
Correct |
50 ms |
32252 KB |
n=2000 |
61 |
Correct |
60 ms |
32616 KB |
n=2000 |
62 |
Correct |
59 ms |
32720 KB |
n=2000 |
63 |
Correct |
43 ms |
32212 KB |
n=2000 |
64 |
Correct |
61 ms |
32572 KB |
n=2000 |
65 |
Correct |
40 ms |
32264 KB |
n=2000 |
66 |
Correct |
56 ms |
32460 KB |
n=2000 |
67 |
Correct |
54 ms |
32524 KB |
n=2000 |
68 |
Correct |
61 ms |
32628 KB |
n=2000 |
69 |
Correct |
115 ms |
31700 KB |
n=2000 |
70 |
Correct |
108 ms |
31696 KB |
n=2000 |
71 |
Correct |
135 ms |
31712 KB |
n=2000 |
72 |
Correct |
74 ms |
31600 KB |
n=2000 |
73 |
Correct |
84 ms |
31676 KB |
n=2000 |
74 |
Correct |
34 ms |
32084 KB |
n=1844 |
75 |
Correct |
71 ms |
31712 KB |
n=2000 |
76 |
Correct |
92 ms |
32456 KB |
n=2000 |
77 |
Correct |
104 ms |
32452 KB |
n=2000 |
78 |
Correct |
95 ms |
32416 KB |
n=2000 |
79 |
Correct |
37 ms |
31816 KB |
n=2000 |
80 |
Correct |
53 ms |
32336 KB |
n=2000 |
81 |
Correct |
68 ms |
32568 KB |
n=2000 |
82 |
Correct |
38 ms |
31820 KB |
n=2000 |
83 |
Correct |
83 ms |
32464 KB |
n=2000 |
84 |
Correct |
64 ms |
32220 KB |
n=2000 |
85 |
Correct |
84 ms |
32612 KB |
n=2000 |
86 |
Correct |
86 ms |
32700 KB |
n=2000 |
87 |
Correct |
63 ms |
32276 KB |
n=2000 |
88 |
Correct |
62 ms |
32300 KB |
n=2000 |
89 |
Correct |
58 ms |
32352 KB |
n=2000 |
90 |
Correct |
54 ms |
32336 KB |
n=2000 |
91 |
Correct |
107 ms |
31840 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
n=5 |
2 |
Correct |
18 ms |
29780 KB |
n=100 |
3 |
Correct |
18 ms |
29776 KB |
n=100 |
4 |
Correct |
17 ms |
29880 KB |
n=100 |
5 |
Correct |
16 ms |
29844 KB |
n=100 |
6 |
Correct |
18 ms |
29772 KB |
n=100 |
7 |
Correct |
15 ms |
29844 KB |
n=100 |
8 |
Correct |
16 ms |
29804 KB |
n=100 |
9 |
Correct |
15 ms |
29780 KB |
n=100 |
10 |
Correct |
16 ms |
29844 KB |
n=100 |
11 |
Correct |
16 ms |
29764 KB |
n=100 |
12 |
Correct |
18 ms |
29804 KB |
n=100 |
13 |
Correct |
17 ms |
29828 KB |
n=100 |
14 |
Correct |
17 ms |
29768 KB |
n=100 |
15 |
Correct |
16 ms |
29780 KB |
n=100 |
16 |
Correct |
16 ms |
29780 KB |
n=100 |
17 |
Correct |
16 ms |
29780 KB |
n=100 |
18 |
Correct |
18 ms |
29780 KB |
n=100 |
19 |
Correct |
17 ms |
29912 KB |
n=100 |
20 |
Correct |
19 ms |
29780 KB |
n=100 |
21 |
Correct |
17 ms |
29760 KB |
n=100 |
22 |
Correct |
15 ms |
29780 KB |
n=100 |
23 |
Correct |
16 ms |
29780 KB |
n=100 |
24 |
Correct |
18 ms |
29868 KB |
n=100 |
25 |
Correct |
17 ms |
29736 KB |
n=100 |
26 |
Correct |
16 ms |
29776 KB |
n=12 |
27 |
Correct |
16 ms |
29844 KB |
n=100 |
28 |
Correct |
20 ms |
30312 KB |
n=500 |
29 |
Correct |
20 ms |
30300 KB |
n=500 |
30 |
Correct |
22 ms |
30556 KB |
n=500 |
31 |
Correct |
23 ms |
30336 KB |
n=500 |
32 |
Correct |
20 ms |
30308 KB |
n=500 |
33 |
Correct |
25 ms |
30428 KB |
n=500 |
34 |
Correct |
23 ms |
30324 KB |
n=500 |
35 |
Correct |
22 ms |
30292 KB |
n=500 |
36 |
Correct |
21 ms |
30232 KB |
n=500 |
37 |
Correct |
21 ms |
30172 KB |
n=500 |
38 |
Correct |
25 ms |
30164 KB |
n=500 |
39 |
Correct |
20 ms |
30244 KB |
n=500 |
40 |
Correct |
20 ms |
30188 KB |
n=500 |
41 |
Correct |
20 ms |
30164 KB |
n=500 |
42 |
Correct |
23 ms |
30300 KB |
n=500 |
43 |
Correct |
28 ms |
30292 KB |
n=500 |
44 |
Correct |
23 ms |
30292 KB |
n=500 |
45 |
Correct |
18 ms |
30292 KB |
n=500 |
46 |
Correct |
20 ms |
30304 KB |
n=500 |
47 |
Correct |
20 ms |
30364 KB |
n=500 |
48 |
Correct |
20 ms |
30172 KB |
n=500 |
49 |
Correct |
26 ms |
30284 KB |
n=500 |
50 |
Correct |
27 ms |
30292 KB |
n=500 |
51 |
Correct |
22 ms |
30428 KB |
n=500 |
52 |
Correct |
22 ms |
30276 KB |
n=500 |
53 |
Correct |
22 ms |
30432 KB |
n=500 |
54 |
Correct |
22 ms |
30300 KB |
n=500 |
55 |
Correct |
18 ms |
30164 KB |
n=278 |
56 |
Correct |
22 ms |
30292 KB |
n=500 |
57 |
Correct |
25 ms |
30300 KB |
n=500 |
58 |
Correct |
22 ms |
30164 KB |
n=500 |
59 |
Correct |
39 ms |
32268 KB |
n=2000 |
60 |
Correct |
50 ms |
32252 KB |
n=2000 |
61 |
Correct |
60 ms |
32616 KB |
n=2000 |
62 |
Correct |
59 ms |
32720 KB |
n=2000 |
63 |
Correct |
43 ms |
32212 KB |
n=2000 |
64 |
Correct |
61 ms |
32572 KB |
n=2000 |
65 |
Correct |
40 ms |
32264 KB |
n=2000 |
66 |
Correct |
56 ms |
32460 KB |
n=2000 |
67 |
Correct |
54 ms |
32524 KB |
n=2000 |
68 |
Correct |
61 ms |
32628 KB |
n=2000 |
69 |
Correct |
115 ms |
31700 KB |
n=2000 |
70 |
Correct |
108 ms |
31696 KB |
n=2000 |
71 |
Correct |
135 ms |
31712 KB |
n=2000 |
72 |
Correct |
74 ms |
31600 KB |
n=2000 |
73 |
Correct |
84 ms |
31676 KB |
n=2000 |
74 |
Correct |
34 ms |
32084 KB |
n=1844 |
75 |
Correct |
71 ms |
31712 KB |
n=2000 |
76 |
Correct |
92 ms |
32456 KB |
n=2000 |
77 |
Correct |
104 ms |
32452 KB |
n=2000 |
78 |
Correct |
95 ms |
32416 KB |
n=2000 |
79 |
Correct |
37 ms |
31816 KB |
n=2000 |
80 |
Correct |
53 ms |
32336 KB |
n=2000 |
81 |
Correct |
68 ms |
32568 KB |
n=2000 |
82 |
Correct |
38 ms |
31820 KB |
n=2000 |
83 |
Correct |
83 ms |
32464 KB |
n=2000 |
84 |
Correct |
64 ms |
32220 KB |
n=2000 |
85 |
Correct |
84 ms |
32612 KB |
n=2000 |
86 |
Correct |
86 ms |
32700 KB |
n=2000 |
87 |
Correct |
63 ms |
32276 KB |
n=2000 |
88 |
Correct |
62 ms |
32300 KB |
n=2000 |
89 |
Correct |
58 ms |
32352 KB |
n=2000 |
90 |
Correct |
54 ms |
32336 KB |
n=2000 |
91 |
Correct |
107 ms |
31840 KB |
n=2000 |
92 |
Runtime error |
3421 ms |
262144 KB |
Execution killed with signal 9 |
93 |
Halted |
0 ms |
0 KB |
- |