#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=2e5+5;
vector <int> g[N];
int p[N],d[N],jmp[N][20];
void dfs(int v,int par){
p[v]=par;
for(auto to : g[v]){
if(to!=par){
d[to]=d[v]+1;
dfs(to,v);
}
}
}
int lca(int a,int b){
if(d[a]<d[b])swap(a,b);
for(int i=19;i>=0;i--){
if(d[jmp[a][i]]>=d[b]){
a=jmp[a][i];
}
}
if(a==b)return a;
for(int i=19;i>=0;i--){
if(jmp[a][i]!=jmp[b][i]){
a=jmp[a][i];
b=jmp[b][i];
}
}
return p[a];
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m,q;
cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);g[v].pb(u);
}
dfs(1,1);
for(int i=1;i<=n;i++){
jmp[i][0]=p[i];
}
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
jmp[j][i]=jmp[jmp[j][i-1]][i-1];
}
}
vector <int> a(m+1);
for(int i=1;i<=m;i++)cin>>a[i];
set <int> pairs[n+1],ver[n+1];
for(int i=1;i<m;i++){
pairs[lca(a[i],a[i+1])].insert(i);
}
for(int i=1;i<=m;i++){
ver[a[i]].insert(i);
}
while(q--){
int type,l,r,v,pos;
cin>>type;
if(type==1){
cin>>pos>>v;
ver[a[pos]].erase(pos);
ver[v].insert(pos);
if(pos+1<=m){
pairs[lca(a[pos],a[pos+1])].erase(pos);
pairs[lca(v,a[pos+1])].insert(pos);
}
if(pos-1>=1){
pairs[lca(a[pos-1],a[pos])].erase(pos-1);
pairs[lca(a[pos-1],v)].insert(pos-1);
}
a[pos]=v;
}
else{
cin>>l>>r>>v;
int L=-1,R=-1;
auto it=pairs[v].lower_bound(l);
if(it!=pairs[v].end() && *it+1<=r && *it>=l){
L=*it;R=*it+1;
}
it=ver[v].lower_bound(l);
if(it!=ver[v].end() && *it<=r && *it>=l){
L=*it;R=*it;
}
cout<<L<<" "<<R<<"\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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
n=5 |
2 |
Correct |
2 ms |
7772 KB |
n=100 |
3 |
Correct |
2 ms |
7768 KB |
n=100 |
4 |
Correct |
2 ms |
7768 KB |
n=100 |
5 |
Correct |
2 ms |
7772 KB |
n=100 |
6 |
Correct |
2 ms |
7768 KB |
n=100 |
7 |
Correct |
2 ms |
7772 KB |
n=100 |
8 |
Correct |
2 ms |
7772 KB |
n=100 |
9 |
Correct |
2 ms |
7772 KB |
n=100 |
10 |
Correct |
2 ms |
7772 KB |
n=100 |
11 |
Correct |
3 ms |
7772 KB |
n=100 |
12 |
Correct |
2 ms |
7768 KB |
n=100 |
13 |
Correct |
2 ms |
7772 KB |
n=100 |
14 |
Correct |
2 ms |
7772 KB |
n=100 |
15 |
Correct |
2 ms |
7772 KB |
n=100 |
16 |
Correct |
2 ms |
7772 KB |
n=100 |
17 |
Correct |
2 ms |
7772 KB |
n=100 |
18 |
Correct |
3 ms |
7772 KB |
n=100 |
19 |
Correct |
2 ms |
7772 KB |
n=100 |
20 |
Correct |
2 ms |
7772 KB |
n=100 |
21 |
Correct |
2 ms |
7772 KB |
n=100 |
22 |
Correct |
4 ms |
7924 KB |
n=100 |
23 |
Correct |
2 ms |
7772 KB |
n=100 |
24 |
Correct |
3 ms |
7772 KB |
n=100 |
25 |
Correct |
2 ms |
7772 KB |
n=100 |
26 |
Correct |
3 ms |
7772 KB |
n=12 |
27 |
Correct |
2 ms |
7768 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
n=5 |
2 |
Correct |
2 ms |
7772 KB |
n=100 |
3 |
Correct |
2 ms |
7768 KB |
n=100 |
4 |
Correct |
2 ms |
7768 KB |
n=100 |
5 |
Correct |
2 ms |
7772 KB |
n=100 |
6 |
Correct |
2 ms |
7768 KB |
n=100 |
7 |
Correct |
2 ms |
7772 KB |
n=100 |
8 |
Correct |
2 ms |
7772 KB |
n=100 |
9 |
Correct |
2 ms |
7772 KB |
n=100 |
10 |
Correct |
2 ms |
7772 KB |
n=100 |
11 |
Correct |
3 ms |
7772 KB |
n=100 |
12 |
Correct |
2 ms |
7768 KB |
n=100 |
13 |
Correct |
2 ms |
7772 KB |
n=100 |
14 |
Correct |
2 ms |
7772 KB |
n=100 |
15 |
Correct |
2 ms |
7772 KB |
n=100 |
16 |
Correct |
2 ms |
7772 KB |
n=100 |
17 |
Correct |
2 ms |
7772 KB |
n=100 |
18 |
Correct |
3 ms |
7772 KB |
n=100 |
19 |
Correct |
2 ms |
7772 KB |
n=100 |
20 |
Correct |
2 ms |
7772 KB |
n=100 |
21 |
Correct |
2 ms |
7772 KB |
n=100 |
22 |
Correct |
4 ms |
7924 KB |
n=100 |
23 |
Correct |
2 ms |
7772 KB |
n=100 |
24 |
Correct |
3 ms |
7772 KB |
n=100 |
25 |
Correct |
2 ms |
7772 KB |
n=100 |
26 |
Correct |
3 ms |
7772 KB |
n=12 |
27 |
Correct |
2 ms |
7768 KB |
n=100 |
28 |
Correct |
3 ms |
7772 KB |
n=500 |
29 |
Correct |
3 ms |
7772 KB |
n=500 |
30 |
Correct |
3 ms |
7772 KB |
n=500 |
31 |
Correct |
3 ms |
7772 KB |
n=500 |
32 |
Correct |
3 ms |
7768 KB |
n=500 |
33 |
Correct |
3 ms |
7768 KB |
n=500 |
34 |
Correct |
3 ms |
7772 KB |
n=500 |
35 |
Correct |
3 ms |
7772 KB |
n=500 |
36 |
Correct |
2 ms |
7772 KB |
n=500 |
37 |
Correct |
3 ms |
7772 KB |
n=500 |
38 |
Correct |
3 ms |
7768 KB |
n=500 |
39 |
Correct |
3 ms |
7828 KB |
n=500 |
40 |
Correct |
3 ms |
7772 KB |
n=500 |
41 |
Correct |
3 ms |
7768 KB |
n=500 |
42 |
Correct |
3 ms |
7772 KB |
n=500 |
43 |
Correct |
3 ms |
7772 KB |
n=500 |
44 |
Correct |
3 ms |
7768 KB |
n=500 |
45 |
Correct |
3 ms |
7772 KB |
n=500 |
46 |
Correct |
3 ms |
7768 KB |
n=500 |
47 |
Correct |
3 ms |
7768 KB |
n=500 |
48 |
Correct |
3 ms |
7772 KB |
n=500 |
49 |
Correct |
3 ms |
7772 KB |
n=500 |
50 |
Correct |
3 ms |
7772 KB |
n=500 |
51 |
Correct |
3 ms |
7768 KB |
n=500 |
52 |
Correct |
3 ms |
7772 KB |
n=500 |
53 |
Correct |
3 ms |
7772 KB |
n=500 |
54 |
Correct |
3 ms |
7772 KB |
n=500 |
55 |
Correct |
4 ms |
7772 KB |
n=278 |
56 |
Correct |
3 ms |
7772 KB |
n=500 |
57 |
Correct |
3 ms |
7772 KB |
n=500 |
58 |
Correct |
3 ms |
8184 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
n=5 |
2 |
Correct |
2 ms |
7772 KB |
n=100 |
3 |
Correct |
2 ms |
7768 KB |
n=100 |
4 |
Correct |
2 ms |
7768 KB |
n=100 |
5 |
Correct |
2 ms |
7772 KB |
n=100 |
6 |
Correct |
2 ms |
7768 KB |
n=100 |
7 |
Correct |
2 ms |
7772 KB |
n=100 |
8 |
Correct |
2 ms |
7772 KB |
n=100 |
9 |
Correct |
2 ms |
7772 KB |
n=100 |
10 |
Correct |
2 ms |
7772 KB |
n=100 |
11 |
Correct |
3 ms |
7772 KB |
n=100 |
12 |
Correct |
2 ms |
7768 KB |
n=100 |
13 |
Correct |
2 ms |
7772 KB |
n=100 |
14 |
Correct |
2 ms |
7772 KB |
n=100 |
15 |
Correct |
2 ms |
7772 KB |
n=100 |
16 |
Correct |
2 ms |
7772 KB |
n=100 |
17 |
Correct |
2 ms |
7772 KB |
n=100 |
18 |
Correct |
3 ms |
7772 KB |
n=100 |
19 |
Correct |
2 ms |
7772 KB |
n=100 |
20 |
Correct |
2 ms |
7772 KB |
n=100 |
21 |
Correct |
2 ms |
7772 KB |
n=100 |
22 |
Correct |
4 ms |
7924 KB |
n=100 |
23 |
Correct |
2 ms |
7772 KB |
n=100 |
24 |
Correct |
3 ms |
7772 KB |
n=100 |
25 |
Correct |
2 ms |
7772 KB |
n=100 |
26 |
Correct |
3 ms |
7772 KB |
n=12 |
27 |
Correct |
2 ms |
7768 KB |
n=100 |
28 |
Correct |
3 ms |
7772 KB |
n=500 |
29 |
Correct |
3 ms |
7772 KB |
n=500 |
30 |
Correct |
3 ms |
7772 KB |
n=500 |
31 |
Correct |
3 ms |
7772 KB |
n=500 |
32 |
Correct |
3 ms |
7768 KB |
n=500 |
33 |
Correct |
3 ms |
7768 KB |
n=500 |
34 |
Correct |
3 ms |
7772 KB |
n=500 |
35 |
Correct |
3 ms |
7772 KB |
n=500 |
36 |
Correct |
2 ms |
7772 KB |
n=500 |
37 |
Correct |
3 ms |
7772 KB |
n=500 |
38 |
Correct |
3 ms |
7768 KB |
n=500 |
39 |
Correct |
3 ms |
7828 KB |
n=500 |
40 |
Correct |
3 ms |
7772 KB |
n=500 |
41 |
Correct |
3 ms |
7768 KB |
n=500 |
42 |
Correct |
3 ms |
7772 KB |
n=500 |
43 |
Correct |
3 ms |
7772 KB |
n=500 |
44 |
Correct |
3 ms |
7768 KB |
n=500 |
45 |
Correct |
3 ms |
7772 KB |
n=500 |
46 |
Correct |
3 ms |
7768 KB |
n=500 |
47 |
Correct |
3 ms |
7768 KB |
n=500 |
48 |
Correct |
3 ms |
7772 KB |
n=500 |
49 |
Correct |
3 ms |
7772 KB |
n=500 |
50 |
Correct |
3 ms |
7772 KB |
n=500 |
51 |
Correct |
3 ms |
7768 KB |
n=500 |
52 |
Correct |
3 ms |
7772 KB |
n=500 |
53 |
Correct |
3 ms |
7772 KB |
n=500 |
54 |
Correct |
3 ms |
7772 KB |
n=500 |
55 |
Correct |
4 ms |
7772 KB |
n=278 |
56 |
Correct |
3 ms |
7772 KB |
n=500 |
57 |
Correct |
3 ms |
7772 KB |
n=500 |
58 |
Correct |
3 ms |
8184 KB |
n=500 |
59 |
Correct |
6 ms |
8284 KB |
n=2000 |
60 |
Correct |
6 ms |
8284 KB |
n=2000 |
61 |
Correct |
6 ms |
8368 KB |
n=2000 |
62 |
Correct |
6 ms |
8284 KB |
n=2000 |
63 |
Correct |
6 ms |
8284 KB |
n=2000 |
64 |
Correct |
6 ms |
8284 KB |
n=2000 |
65 |
Correct |
6 ms |
8284 KB |
n=2000 |
66 |
Correct |
7 ms |
8540 KB |
n=2000 |
67 |
Correct |
7 ms |
8280 KB |
n=2000 |
68 |
Correct |
6 ms |
8284 KB |
n=2000 |
69 |
Correct |
5 ms |
8280 KB |
n=2000 |
70 |
Correct |
5 ms |
8336 KB |
n=2000 |
71 |
Correct |
5 ms |
8284 KB |
n=2000 |
72 |
Correct |
5 ms |
8280 KB |
n=2000 |
73 |
Correct |
5 ms |
8284 KB |
n=2000 |
74 |
Correct |
5 ms |
8284 KB |
n=1844 |
75 |
Correct |
5 ms |
8284 KB |
n=2000 |
76 |
Correct |
6 ms |
8284 KB |
n=2000 |
77 |
Correct |
6 ms |
8284 KB |
n=2000 |
78 |
Correct |
6 ms |
8476 KB |
n=2000 |
79 |
Correct |
6 ms |
8536 KB |
n=2000 |
80 |
Correct |
6 ms |
8280 KB |
n=2000 |
81 |
Correct |
6 ms |
8284 KB |
n=2000 |
82 |
Correct |
7 ms |
8280 KB |
n=2000 |
83 |
Correct |
6 ms |
8284 KB |
n=2000 |
84 |
Correct |
6 ms |
8280 KB |
n=2000 |
85 |
Correct |
6 ms |
8284 KB |
n=2000 |
86 |
Correct |
6 ms |
8532 KB |
n=2000 |
87 |
Correct |
6 ms |
8284 KB |
n=2000 |
88 |
Correct |
6 ms |
8336 KB |
n=2000 |
89 |
Correct |
6 ms |
8284 KB |
n=2000 |
90 |
Correct |
6 ms |
8284 KB |
n=2000 |
91 |
Correct |
6 ms |
8284 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
n=5 |
2 |
Correct |
2 ms |
7772 KB |
n=100 |
3 |
Correct |
2 ms |
7768 KB |
n=100 |
4 |
Correct |
2 ms |
7768 KB |
n=100 |
5 |
Correct |
2 ms |
7772 KB |
n=100 |
6 |
Correct |
2 ms |
7768 KB |
n=100 |
7 |
Correct |
2 ms |
7772 KB |
n=100 |
8 |
Correct |
2 ms |
7772 KB |
n=100 |
9 |
Correct |
2 ms |
7772 KB |
n=100 |
10 |
Correct |
2 ms |
7772 KB |
n=100 |
11 |
Correct |
3 ms |
7772 KB |
n=100 |
12 |
Correct |
2 ms |
7768 KB |
n=100 |
13 |
Correct |
2 ms |
7772 KB |
n=100 |
14 |
Correct |
2 ms |
7772 KB |
n=100 |
15 |
Correct |
2 ms |
7772 KB |
n=100 |
16 |
Correct |
2 ms |
7772 KB |
n=100 |
17 |
Correct |
2 ms |
7772 KB |
n=100 |
18 |
Correct |
3 ms |
7772 KB |
n=100 |
19 |
Correct |
2 ms |
7772 KB |
n=100 |
20 |
Correct |
2 ms |
7772 KB |
n=100 |
21 |
Correct |
2 ms |
7772 KB |
n=100 |
22 |
Correct |
4 ms |
7924 KB |
n=100 |
23 |
Correct |
2 ms |
7772 KB |
n=100 |
24 |
Correct |
3 ms |
7772 KB |
n=100 |
25 |
Correct |
2 ms |
7772 KB |
n=100 |
26 |
Correct |
3 ms |
7772 KB |
n=12 |
27 |
Correct |
2 ms |
7768 KB |
n=100 |
28 |
Correct |
3 ms |
7772 KB |
n=500 |
29 |
Correct |
3 ms |
7772 KB |
n=500 |
30 |
Correct |
3 ms |
7772 KB |
n=500 |
31 |
Correct |
3 ms |
7772 KB |
n=500 |
32 |
Correct |
3 ms |
7768 KB |
n=500 |
33 |
Correct |
3 ms |
7768 KB |
n=500 |
34 |
Correct |
3 ms |
7772 KB |
n=500 |
35 |
Correct |
3 ms |
7772 KB |
n=500 |
36 |
Correct |
2 ms |
7772 KB |
n=500 |
37 |
Correct |
3 ms |
7772 KB |
n=500 |
38 |
Correct |
3 ms |
7768 KB |
n=500 |
39 |
Correct |
3 ms |
7828 KB |
n=500 |
40 |
Correct |
3 ms |
7772 KB |
n=500 |
41 |
Correct |
3 ms |
7768 KB |
n=500 |
42 |
Correct |
3 ms |
7772 KB |
n=500 |
43 |
Correct |
3 ms |
7772 KB |
n=500 |
44 |
Correct |
3 ms |
7768 KB |
n=500 |
45 |
Correct |
3 ms |
7772 KB |
n=500 |
46 |
Correct |
3 ms |
7768 KB |
n=500 |
47 |
Correct |
3 ms |
7768 KB |
n=500 |
48 |
Correct |
3 ms |
7772 KB |
n=500 |
49 |
Correct |
3 ms |
7772 KB |
n=500 |
50 |
Correct |
3 ms |
7772 KB |
n=500 |
51 |
Correct |
3 ms |
7768 KB |
n=500 |
52 |
Correct |
3 ms |
7772 KB |
n=500 |
53 |
Correct |
3 ms |
7772 KB |
n=500 |
54 |
Correct |
3 ms |
7772 KB |
n=500 |
55 |
Correct |
4 ms |
7772 KB |
n=278 |
56 |
Correct |
3 ms |
7772 KB |
n=500 |
57 |
Correct |
3 ms |
7772 KB |
n=500 |
58 |
Correct |
3 ms |
8184 KB |
n=500 |
59 |
Correct |
6 ms |
8284 KB |
n=2000 |
60 |
Correct |
6 ms |
8284 KB |
n=2000 |
61 |
Correct |
6 ms |
8368 KB |
n=2000 |
62 |
Correct |
6 ms |
8284 KB |
n=2000 |
63 |
Correct |
6 ms |
8284 KB |
n=2000 |
64 |
Correct |
6 ms |
8284 KB |
n=2000 |
65 |
Correct |
6 ms |
8284 KB |
n=2000 |
66 |
Correct |
7 ms |
8540 KB |
n=2000 |
67 |
Correct |
7 ms |
8280 KB |
n=2000 |
68 |
Correct |
6 ms |
8284 KB |
n=2000 |
69 |
Correct |
5 ms |
8280 KB |
n=2000 |
70 |
Correct |
5 ms |
8336 KB |
n=2000 |
71 |
Correct |
5 ms |
8284 KB |
n=2000 |
72 |
Correct |
5 ms |
8280 KB |
n=2000 |
73 |
Correct |
5 ms |
8284 KB |
n=2000 |
74 |
Correct |
5 ms |
8284 KB |
n=1844 |
75 |
Correct |
5 ms |
8284 KB |
n=2000 |
76 |
Correct |
6 ms |
8284 KB |
n=2000 |
77 |
Correct |
6 ms |
8284 KB |
n=2000 |
78 |
Correct |
6 ms |
8476 KB |
n=2000 |
79 |
Correct |
6 ms |
8536 KB |
n=2000 |
80 |
Correct |
6 ms |
8280 KB |
n=2000 |
81 |
Correct |
6 ms |
8284 KB |
n=2000 |
82 |
Correct |
7 ms |
8280 KB |
n=2000 |
83 |
Correct |
6 ms |
8284 KB |
n=2000 |
84 |
Correct |
6 ms |
8280 KB |
n=2000 |
85 |
Correct |
6 ms |
8284 KB |
n=2000 |
86 |
Correct |
6 ms |
8532 KB |
n=2000 |
87 |
Correct |
6 ms |
8284 KB |
n=2000 |
88 |
Correct |
6 ms |
8336 KB |
n=2000 |
89 |
Correct |
6 ms |
8284 KB |
n=2000 |
90 |
Correct |
6 ms |
8284 KB |
n=2000 |
91 |
Correct |
6 ms |
8284 KB |
n=2000 |
92 |
Correct |
757 ms |
74064 KB |
n=200000 |
93 |
Correct |
1117 ms |
75648 KB |
n=200000 |
94 |
Correct |
1100 ms |
75476 KB |
n=200000 |
95 |
Correct |
692 ms |
75216 KB |
n=200000 |
96 |
Correct |
715 ms |
75460 KB |
n=200000 |
97 |
Correct |
1070 ms |
75700 KB |
n=200000 |
98 |
Correct |
697 ms |
75204 KB |
n=200000 |
99 |
Correct |
882 ms |
75392 KB |
n=200000 |
100 |
Correct |
720 ms |
75308 KB |
n=200000 |
101 |
Correct |
1018 ms |
75592 KB |
n=200000 |
102 |
Correct |
437 ms |
76484 KB |
n=200000 |
103 |
Correct |
458 ms |
76368 KB |
n=200000 |
104 |
Correct |
451 ms |
76320 KB |
n=200000 |
105 |
Correct |
447 ms |
76880 KB |
n=200000 |
106 |
Correct |
470 ms |
76720 KB |
n=200000 |
107 |
Correct |
474 ms |
76816 KB |
n=200000 |
108 |
Correct |
781 ms |
75368 KB |
n=200000 |
109 |
Correct |
798 ms |
75292 KB |
n=200000 |
110 |
Correct |
803 ms |
75156 KB |
n=200000 |
111 |
Correct |
786 ms |
74876 KB |
n=200000 |
112 |
Correct |
1060 ms |
75664 KB |
n=200000 |
113 |
Correct |
1124 ms |
75872 KB |
n=200000 |
114 |
Correct |
807 ms |
74776 KB |
n=200000 |
115 |
Correct |
1086 ms |
75844 KB |
n=200000 |
116 |
Correct |
725 ms |
75416 KB |
n=200000 |
117 |
Correct |
1087 ms |
75316 KB |
n=200000 |
118 |
Correct |
1092 ms |
75660 KB |
n=200000 |
119 |
Correct |
686 ms |
75404 KB |
n=200000 |
120 |
Correct |
988 ms |
74364 KB |
n=200000 |
121 |
Correct |
1020 ms |
74312 KB |
n=200000 |
122 |
Correct |
997 ms |
74816 KB |
n=200000 |
123 |
Correct |
486 ms |
76712 KB |
n=200000 |
124 |
Correct |
173 ms |
24148 KB |
n=25264 |