#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld long double
#define _all(T) T.begin(),T.end()
const int mxn = 2e5+10;
vector<int> paths[mxn];
int fa[mxn],link_top[mxn],bs[mxn],sz[mxn],dep[mxn],arr[mxn],adj[mxn];
int segtree[mxn*4+4];
void dfs1(int now,int par){
fa[now] = par;
sz[now] = 1;
for(auto nxt:paths[now]){
if(nxt == par)continue;
dep[nxt] = dep[now]+1;
dfs1(nxt,now);
if(bs[now] == 0||sz[bs[now]] < sz[nxt])bs[now] = nxt;
sz[now] += sz[nxt];
}
return;
}
void dfs2(int now,int top){
link_top[now] = top;
if(bs[now])dfs2(bs[now],top);
for(auto nxt:paths[now]){
if(nxt == fa[now]||nxt == bs[now])continue;
dfs2(nxt,nxt);
}
return;
}
int lca(int a,int b){
int ta = link_top[a],tb = link_top[b];
while(ta != tb){
if(dep[ta]<dep[tb]){
swap(ta,tb);
swap(a,b);
}
a = fa[ta];
ta = link_top[a];
}
if(dep[a]>dep[b])return b;
else return a;
}
void modify(int now,int l,int r,int p,int v){
if(l == r){
segtree[now] = v;
return;
}
int mid = (l+r)>>1;
if(mid>=p)modify(now*2+1,l,mid,p,v);
else modify(now*2+2,mid+1,r,p,v);
segtree[now] = min(segtree[now*2+1],segtree[now*2+2]);
return;
}
int getval(int now,int l,int r,int s,int e){
if(s<=l&&e>=r)return segtree[now];
int mid = (l+r)>>1;
if(mid>=e)return getval(now*2+1,l,mid,s,e);
else if(mid<s)return getval(now*2+2,mid+1,r,s,e);
else return min(getval(now*2+1,l,mid,s,e),getval(now*2+2,mid+1,r,s,e));
}
bool flag = false;
int main(){
io
int n,m,q;
cin>>n>>m>>q;
// if(m*m*q*__lg(n)>=1e9)return 0;
for(int i = 0;i<n-1;i++){
int a,b;
cin>>a>>b;
paths[a].push_back(b);
paths[b].push_back(a);
}
dfs1(1,1);
dfs2(1,1);
for(int i = 1;i<=m;i++){
cin>>arr[i];
}
for(int i = 2;i<=m;i++){
adj[i] = lca(arr[i],arr[i-1]);
modify(0,0,mxn,i,dep[adj[i]]);
}
// cout<<dep[lca(arr[1],arr[2])]<<' '<<getval(0,0,mxn,2,2);return 0;
while(q--){
int t;
cin>>t;
if(t == 1){
int p,v;
cin>>p>>v;
arr[p] = v;
if(p != 1)adj[p] = lca(arr[p],arr[p-1]);
if(p != m)adj[p+1] = lca(arr[p],arr[p+1]);
if(p != 1)modify(0,0,mxn,p,dep[adj[p]]);
if(p != m)modify(0,0,mxn,p+1,dep[adj[p+1]]);
}
else{
int a,b,c;
pii ans = {-1,-1};
cin>>a>>b>>c;
if(a == 1&&b == 3&&c == 1)flag = true;
for(int i = a;i<b;i++){
int l = i+1,r = b;
while(l < r){
int mid= (l+r)>>1;
// if(flag)cout<<l<<' '<<r<<' '<<getval(0,0,mxn,i+1,mid)<<endl;
if(getval(0,0,mxn,i+1,mid)<=dep[c])r = mid;
else l = mid+1;
}
if(getval(0,0,mxn,i+1,r) == dep[c]&&lca(arr[i],c) == c){
ans = {i,r};
}
if(arr[i] == c)ans = {i,i};
}
if(arr[b] == c)ans = {b,b};
flag = false;
cout<<ans.fs<<' '<<ans.sc<<'\n';
}
}
return 0;
}
/*
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 |
3 ms |
5076 KB |
n=5 |
2 |
Correct |
4 ms |
5076 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
5 ms |
5028 KB |
n=100 |
5 |
Correct |
4 ms |
5076 KB |
n=100 |
6 |
Correct |
7 ms |
5124 KB |
n=100 |
7 |
Correct |
7 ms |
5028 KB |
n=100 |
8 |
Correct |
3 ms |
5076 KB |
n=100 |
9 |
Correct |
4 ms |
5076 KB |
n=100 |
10 |
Correct |
5 ms |
5076 KB |
n=100 |
11 |
Correct |
5 ms |
5076 KB |
n=100 |
12 |
Correct |
4 ms |
5076 KB |
n=100 |
13 |
Correct |
4 ms |
5076 KB |
n=100 |
14 |
Correct |
4 ms |
5068 KB |
n=100 |
15 |
Correct |
4 ms |
5024 KB |
n=100 |
16 |
Correct |
4 ms |
5076 KB |
n=100 |
17 |
Correct |
4 ms |
5076 KB |
n=100 |
18 |
Correct |
4 ms |
5108 KB |
n=100 |
19 |
Correct |
4 ms |
5076 KB |
n=100 |
20 |
Correct |
3 ms |
5028 KB |
n=100 |
21 |
Correct |
4 ms |
5024 KB |
n=100 |
22 |
Correct |
4 ms |
5076 KB |
n=100 |
23 |
Correct |
4 ms |
5076 KB |
n=100 |
24 |
Correct |
4 ms |
5076 KB |
n=100 |
25 |
Correct |
5 ms |
5076 KB |
n=100 |
26 |
Correct |
3 ms |
5024 KB |
n=12 |
27 |
Correct |
5 ms |
5076 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
n=5 |
2 |
Correct |
4 ms |
5076 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
5 ms |
5028 KB |
n=100 |
5 |
Correct |
4 ms |
5076 KB |
n=100 |
6 |
Correct |
7 ms |
5124 KB |
n=100 |
7 |
Correct |
7 ms |
5028 KB |
n=100 |
8 |
Correct |
3 ms |
5076 KB |
n=100 |
9 |
Correct |
4 ms |
5076 KB |
n=100 |
10 |
Correct |
5 ms |
5076 KB |
n=100 |
11 |
Correct |
5 ms |
5076 KB |
n=100 |
12 |
Correct |
4 ms |
5076 KB |
n=100 |
13 |
Correct |
4 ms |
5076 KB |
n=100 |
14 |
Correct |
4 ms |
5068 KB |
n=100 |
15 |
Correct |
4 ms |
5024 KB |
n=100 |
16 |
Correct |
4 ms |
5076 KB |
n=100 |
17 |
Correct |
4 ms |
5076 KB |
n=100 |
18 |
Correct |
4 ms |
5108 KB |
n=100 |
19 |
Correct |
4 ms |
5076 KB |
n=100 |
20 |
Correct |
3 ms |
5028 KB |
n=100 |
21 |
Correct |
4 ms |
5024 KB |
n=100 |
22 |
Correct |
4 ms |
5076 KB |
n=100 |
23 |
Correct |
4 ms |
5076 KB |
n=100 |
24 |
Correct |
4 ms |
5076 KB |
n=100 |
25 |
Correct |
5 ms |
5076 KB |
n=100 |
26 |
Correct |
3 ms |
5024 KB |
n=12 |
27 |
Correct |
5 ms |
5076 KB |
n=100 |
28 |
Correct |
32 ms |
5076 KB |
n=500 |
29 |
Correct |
35 ms |
5076 KB |
n=500 |
30 |
Correct |
30 ms |
5168 KB |
n=500 |
31 |
Correct |
33 ms |
5168 KB |
n=500 |
32 |
Correct |
34 ms |
5076 KB |
n=500 |
33 |
Correct |
33 ms |
5156 KB |
n=500 |
34 |
Correct |
34 ms |
5140 KB |
n=500 |
35 |
Correct |
32 ms |
5076 KB |
n=500 |
36 |
Correct |
175 ms |
5136 KB |
n=500 |
37 |
Correct |
169 ms |
5076 KB |
n=500 |
38 |
Correct |
172 ms |
5140 KB |
n=500 |
39 |
Correct |
35 ms |
5076 KB |
n=500 |
40 |
Correct |
33 ms |
5164 KB |
n=500 |
41 |
Correct |
33 ms |
5164 KB |
n=500 |
42 |
Correct |
86 ms |
5140 KB |
n=500 |
43 |
Correct |
87 ms |
5140 KB |
n=500 |
44 |
Correct |
85 ms |
5076 KB |
n=500 |
45 |
Correct |
29 ms |
5144 KB |
n=500 |
46 |
Correct |
32 ms |
5076 KB |
n=500 |
47 |
Correct |
29 ms |
5160 KB |
n=500 |
48 |
Correct |
27 ms |
5076 KB |
n=500 |
49 |
Correct |
29 ms |
5168 KB |
n=500 |
50 |
Correct |
29 ms |
5076 KB |
n=500 |
51 |
Correct |
31 ms |
5168 KB |
n=500 |
52 |
Correct |
35 ms |
5076 KB |
n=500 |
53 |
Correct |
33 ms |
5076 KB |
n=500 |
54 |
Correct |
83 ms |
5176 KB |
n=500 |
55 |
Correct |
13 ms |
5144 KB |
n=278 |
56 |
Correct |
83 ms |
5172 KB |
n=500 |
57 |
Correct |
86 ms |
5172 KB |
n=500 |
58 |
Correct |
74 ms |
5044 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
n=5 |
2 |
Correct |
4 ms |
5076 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
5 ms |
5028 KB |
n=100 |
5 |
Correct |
4 ms |
5076 KB |
n=100 |
6 |
Correct |
7 ms |
5124 KB |
n=100 |
7 |
Correct |
7 ms |
5028 KB |
n=100 |
8 |
Correct |
3 ms |
5076 KB |
n=100 |
9 |
Correct |
4 ms |
5076 KB |
n=100 |
10 |
Correct |
5 ms |
5076 KB |
n=100 |
11 |
Correct |
5 ms |
5076 KB |
n=100 |
12 |
Correct |
4 ms |
5076 KB |
n=100 |
13 |
Correct |
4 ms |
5076 KB |
n=100 |
14 |
Correct |
4 ms |
5068 KB |
n=100 |
15 |
Correct |
4 ms |
5024 KB |
n=100 |
16 |
Correct |
4 ms |
5076 KB |
n=100 |
17 |
Correct |
4 ms |
5076 KB |
n=100 |
18 |
Correct |
4 ms |
5108 KB |
n=100 |
19 |
Correct |
4 ms |
5076 KB |
n=100 |
20 |
Correct |
3 ms |
5028 KB |
n=100 |
21 |
Correct |
4 ms |
5024 KB |
n=100 |
22 |
Correct |
4 ms |
5076 KB |
n=100 |
23 |
Correct |
4 ms |
5076 KB |
n=100 |
24 |
Correct |
4 ms |
5076 KB |
n=100 |
25 |
Correct |
5 ms |
5076 KB |
n=100 |
26 |
Correct |
3 ms |
5024 KB |
n=12 |
27 |
Correct |
5 ms |
5076 KB |
n=100 |
28 |
Correct |
32 ms |
5076 KB |
n=500 |
29 |
Correct |
35 ms |
5076 KB |
n=500 |
30 |
Correct |
30 ms |
5168 KB |
n=500 |
31 |
Correct |
33 ms |
5168 KB |
n=500 |
32 |
Correct |
34 ms |
5076 KB |
n=500 |
33 |
Correct |
33 ms |
5156 KB |
n=500 |
34 |
Correct |
34 ms |
5140 KB |
n=500 |
35 |
Correct |
32 ms |
5076 KB |
n=500 |
36 |
Correct |
175 ms |
5136 KB |
n=500 |
37 |
Correct |
169 ms |
5076 KB |
n=500 |
38 |
Correct |
172 ms |
5140 KB |
n=500 |
39 |
Correct |
35 ms |
5076 KB |
n=500 |
40 |
Correct |
33 ms |
5164 KB |
n=500 |
41 |
Correct |
33 ms |
5164 KB |
n=500 |
42 |
Correct |
86 ms |
5140 KB |
n=500 |
43 |
Correct |
87 ms |
5140 KB |
n=500 |
44 |
Correct |
85 ms |
5076 KB |
n=500 |
45 |
Correct |
29 ms |
5144 KB |
n=500 |
46 |
Correct |
32 ms |
5076 KB |
n=500 |
47 |
Correct |
29 ms |
5160 KB |
n=500 |
48 |
Correct |
27 ms |
5076 KB |
n=500 |
49 |
Correct |
29 ms |
5168 KB |
n=500 |
50 |
Correct |
29 ms |
5076 KB |
n=500 |
51 |
Correct |
31 ms |
5168 KB |
n=500 |
52 |
Correct |
35 ms |
5076 KB |
n=500 |
53 |
Correct |
33 ms |
5076 KB |
n=500 |
54 |
Correct |
83 ms |
5176 KB |
n=500 |
55 |
Correct |
13 ms |
5144 KB |
n=278 |
56 |
Correct |
83 ms |
5172 KB |
n=500 |
57 |
Correct |
86 ms |
5172 KB |
n=500 |
58 |
Correct |
74 ms |
5044 KB |
n=500 |
59 |
Correct |
638 ms |
5408 KB |
n=2000 |
60 |
Correct |
641 ms |
5436 KB |
n=2000 |
61 |
Correct |
665 ms |
5376 KB |
n=2000 |
62 |
Correct |
674 ms |
5332 KB |
n=2000 |
63 |
Correct |
711 ms |
5284 KB |
n=2000 |
64 |
Correct |
735 ms |
5344 KB |
n=2000 |
65 |
Correct |
727 ms |
5416 KB |
n=2000 |
66 |
Correct |
699 ms |
5292 KB |
n=2000 |
67 |
Correct |
677 ms |
5408 KB |
n=2000 |
68 |
Correct |
659 ms |
5360 KB |
n=2000 |
69 |
Correct |
3869 ms |
5296 KB |
n=2000 |
70 |
Correct |
3849 ms |
5452 KB |
n=2000 |
71 |
Correct |
3860 ms |
5404 KB |
n=2000 |
72 |
Correct |
759 ms |
5292 KB |
n=2000 |
73 |
Correct |
742 ms |
5288 KB |
n=2000 |
74 |
Correct |
525 ms |
5268 KB |
n=1844 |
75 |
Correct |
757 ms |
5324 KB |
n=2000 |
76 |
Correct |
1941 ms |
5284 KB |
n=2000 |
77 |
Correct |
1916 ms |
5280 KB |
n=2000 |
78 |
Correct |
2056 ms |
5408 KB |
n=2000 |
79 |
Correct |
571 ms |
5272 KB |
n=2000 |
80 |
Correct |
575 ms |
5408 KB |
n=2000 |
81 |
Correct |
579 ms |
5332 KB |
n=2000 |
82 |
Correct |
566 ms |
5292 KB |
n=2000 |
83 |
Correct |
558 ms |
5392 KB |
n=2000 |
84 |
Correct |
669 ms |
5284 KB |
n=2000 |
85 |
Correct |
679 ms |
5336 KB |
n=2000 |
86 |
Correct |
716 ms |
5332 KB |
n=2000 |
87 |
Correct |
686 ms |
5300 KB |
n=2000 |
88 |
Correct |
2260 ms |
5432 KB |
n=2000 |
89 |
Correct |
2340 ms |
5436 KB |
n=2000 |
90 |
Correct |
1736 ms |
5560 KB |
n=2000 |
91 |
Correct |
1715 ms |
5416 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
n=5 |
2 |
Correct |
4 ms |
5076 KB |
n=100 |
3 |
Correct |
4 ms |
5076 KB |
n=100 |
4 |
Correct |
5 ms |
5028 KB |
n=100 |
5 |
Correct |
4 ms |
5076 KB |
n=100 |
6 |
Correct |
7 ms |
5124 KB |
n=100 |
7 |
Correct |
7 ms |
5028 KB |
n=100 |
8 |
Correct |
3 ms |
5076 KB |
n=100 |
9 |
Correct |
4 ms |
5076 KB |
n=100 |
10 |
Correct |
5 ms |
5076 KB |
n=100 |
11 |
Correct |
5 ms |
5076 KB |
n=100 |
12 |
Correct |
4 ms |
5076 KB |
n=100 |
13 |
Correct |
4 ms |
5076 KB |
n=100 |
14 |
Correct |
4 ms |
5068 KB |
n=100 |
15 |
Correct |
4 ms |
5024 KB |
n=100 |
16 |
Correct |
4 ms |
5076 KB |
n=100 |
17 |
Correct |
4 ms |
5076 KB |
n=100 |
18 |
Correct |
4 ms |
5108 KB |
n=100 |
19 |
Correct |
4 ms |
5076 KB |
n=100 |
20 |
Correct |
3 ms |
5028 KB |
n=100 |
21 |
Correct |
4 ms |
5024 KB |
n=100 |
22 |
Correct |
4 ms |
5076 KB |
n=100 |
23 |
Correct |
4 ms |
5076 KB |
n=100 |
24 |
Correct |
4 ms |
5076 KB |
n=100 |
25 |
Correct |
5 ms |
5076 KB |
n=100 |
26 |
Correct |
3 ms |
5024 KB |
n=12 |
27 |
Correct |
5 ms |
5076 KB |
n=100 |
28 |
Correct |
32 ms |
5076 KB |
n=500 |
29 |
Correct |
35 ms |
5076 KB |
n=500 |
30 |
Correct |
30 ms |
5168 KB |
n=500 |
31 |
Correct |
33 ms |
5168 KB |
n=500 |
32 |
Correct |
34 ms |
5076 KB |
n=500 |
33 |
Correct |
33 ms |
5156 KB |
n=500 |
34 |
Correct |
34 ms |
5140 KB |
n=500 |
35 |
Correct |
32 ms |
5076 KB |
n=500 |
36 |
Correct |
175 ms |
5136 KB |
n=500 |
37 |
Correct |
169 ms |
5076 KB |
n=500 |
38 |
Correct |
172 ms |
5140 KB |
n=500 |
39 |
Correct |
35 ms |
5076 KB |
n=500 |
40 |
Correct |
33 ms |
5164 KB |
n=500 |
41 |
Correct |
33 ms |
5164 KB |
n=500 |
42 |
Correct |
86 ms |
5140 KB |
n=500 |
43 |
Correct |
87 ms |
5140 KB |
n=500 |
44 |
Correct |
85 ms |
5076 KB |
n=500 |
45 |
Correct |
29 ms |
5144 KB |
n=500 |
46 |
Correct |
32 ms |
5076 KB |
n=500 |
47 |
Correct |
29 ms |
5160 KB |
n=500 |
48 |
Correct |
27 ms |
5076 KB |
n=500 |
49 |
Correct |
29 ms |
5168 KB |
n=500 |
50 |
Correct |
29 ms |
5076 KB |
n=500 |
51 |
Correct |
31 ms |
5168 KB |
n=500 |
52 |
Correct |
35 ms |
5076 KB |
n=500 |
53 |
Correct |
33 ms |
5076 KB |
n=500 |
54 |
Correct |
83 ms |
5176 KB |
n=500 |
55 |
Correct |
13 ms |
5144 KB |
n=278 |
56 |
Correct |
83 ms |
5172 KB |
n=500 |
57 |
Correct |
86 ms |
5172 KB |
n=500 |
58 |
Correct |
74 ms |
5044 KB |
n=500 |
59 |
Correct |
638 ms |
5408 KB |
n=2000 |
60 |
Correct |
641 ms |
5436 KB |
n=2000 |
61 |
Correct |
665 ms |
5376 KB |
n=2000 |
62 |
Correct |
674 ms |
5332 KB |
n=2000 |
63 |
Correct |
711 ms |
5284 KB |
n=2000 |
64 |
Correct |
735 ms |
5344 KB |
n=2000 |
65 |
Correct |
727 ms |
5416 KB |
n=2000 |
66 |
Correct |
699 ms |
5292 KB |
n=2000 |
67 |
Correct |
677 ms |
5408 KB |
n=2000 |
68 |
Correct |
659 ms |
5360 KB |
n=2000 |
69 |
Correct |
3869 ms |
5296 KB |
n=2000 |
70 |
Correct |
3849 ms |
5452 KB |
n=2000 |
71 |
Correct |
3860 ms |
5404 KB |
n=2000 |
72 |
Correct |
759 ms |
5292 KB |
n=2000 |
73 |
Correct |
742 ms |
5288 KB |
n=2000 |
74 |
Correct |
525 ms |
5268 KB |
n=1844 |
75 |
Correct |
757 ms |
5324 KB |
n=2000 |
76 |
Correct |
1941 ms |
5284 KB |
n=2000 |
77 |
Correct |
1916 ms |
5280 KB |
n=2000 |
78 |
Correct |
2056 ms |
5408 KB |
n=2000 |
79 |
Correct |
571 ms |
5272 KB |
n=2000 |
80 |
Correct |
575 ms |
5408 KB |
n=2000 |
81 |
Correct |
579 ms |
5332 KB |
n=2000 |
82 |
Correct |
566 ms |
5292 KB |
n=2000 |
83 |
Correct |
558 ms |
5392 KB |
n=2000 |
84 |
Correct |
669 ms |
5284 KB |
n=2000 |
85 |
Correct |
679 ms |
5336 KB |
n=2000 |
86 |
Correct |
716 ms |
5332 KB |
n=2000 |
87 |
Correct |
686 ms |
5300 KB |
n=2000 |
88 |
Correct |
2260 ms |
5432 KB |
n=2000 |
89 |
Correct |
2340 ms |
5436 KB |
n=2000 |
90 |
Correct |
1736 ms |
5560 KB |
n=2000 |
91 |
Correct |
1715 ms |
5416 KB |
n=2000 |
92 |
Execution timed out |
4065 ms |
19108 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |