#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
const int N=2e5+5;
int n,m,q,a[N],par[N][20],dep[N];
vector<int>adj[N];
void dfs(int x,int p){
par[x][0]=p;
for(int y:adj[x]){
if(y!=p){
dep[y]=dep[x]+1;
dfs(y,x);
}
}
}
int up(int x,int d){
for(int i=19;i>=0;i--){
if(d&(1<<i)){
x=par[x][i];
}
}
return x;
}
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
x=up(x,dep[x]-dep[y]);
for(int i=19;i>=0;i--){
if(par[x][i]!=par[y][i]){
x=par[x][i];
y=par[y][i];
}
}
if(x!=y) x=par[x][0];
return x;
}
void ask(int l,int r,int u){
for(int x=l;x<=r;x++){
int cur=a[x];
for(int y=x;y<=r;y++){
cur=lca(cur,a[y]);
if(cur==u){
cout<<x<<' '<<y<<"\n";
return;
}
if(dep[cur]<dep[u]) break;
}
}
cout<<"-1 -1\n";
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=m;i++){
cin>>a[i];
}
dep[1]=0;
dfs(1,1);
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
par[i][j]=par[par[i][j-1]][j-1];
}
}
while(q--){
int t;
cin>>t;
if(t==1){
int pos,v;
cin>>pos>>v;
a[pos]=v;
}
else{
int l,r,u;
cin>>l>>r>>u;
ask(l,r,u);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
n=5 |
2 |
Correct |
2 ms |
6748 KB |
n=100 |
3 |
Correct |
2 ms |
6748 KB |
n=100 |
4 |
Correct |
2 ms |
6748 KB |
n=100 |
5 |
Correct |
2 ms |
6748 KB |
n=100 |
6 |
Correct |
3 ms |
6744 KB |
n=100 |
7 |
Correct |
2 ms |
7000 KB |
n=100 |
8 |
Correct |
2 ms |
6748 KB |
n=100 |
9 |
Correct |
2 ms |
6748 KB |
n=100 |
10 |
Correct |
2 ms |
6748 KB |
n=100 |
11 |
Correct |
3 ms |
6744 KB |
n=100 |
12 |
Correct |
2 ms |
6904 KB |
n=100 |
13 |
Correct |
2 ms |
6744 KB |
n=100 |
14 |
Correct |
2 ms |
6748 KB |
n=100 |
15 |
Correct |
1 ms |
6748 KB |
n=100 |
16 |
Correct |
2 ms |
6748 KB |
n=100 |
17 |
Correct |
2 ms |
6744 KB |
n=100 |
18 |
Correct |
2 ms |
6744 KB |
n=100 |
19 |
Correct |
2 ms |
6744 KB |
n=100 |
20 |
Correct |
2 ms |
6748 KB |
n=100 |
21 |
Correct |
2 ms |
6748 KB |
n=100 |
22 |
Correct |
2 ms |
6748 KB |
n=100 |
23 |
Correct |
11 ms |
6800 KB |
n=100 |
24 |
Correct |
12 ms |
6748 KB |
n=100 |
25 |
Correct |
4 ms |
6748 KB |
n=100 |
26 |
Correct |
2 ms |
6744 KB |
n=12 |
27 |
Correct |
2 ms |
6748 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
n=5 |
2 |
Correct |
2 ms |
6748 KB |
n=100 |
3 |
Correct |
2 ms |
6748 KB |
n=100 |
4 |
Correct |
2 ms |
6748 KB |
n=100 |
5 |
Correct |
2 ms |
6748 KB |
n=100 |
6 |
Correct |
3 ms |
6744 KB |
n=100 |
7 |
Correct |
2 ms |
7000 KB |
n=100 |
8 |
Correct |
2 ms |
6748 KB |
n=100 |
9 |
Correct |
2 ms |
6748 KB |
n=100 |
10 |
Correct |
2 ms |
6748 KB |
n=100 |
11 |
Correct |
3 ms |
6744 KB |
n=100 |
12 |
Correct |
2 ms |
6904 KB |
n=100 |
13 |
Correct |
2 ms |
6744 KB |
n=100 |
14 |
Correct |
2 ms |
6748 KB |
n=100 |
15 |
Correct |
1 ms |
6748 KB |
n=100 |
16 |
Correct |
2 ms |
6748 KB |
n=100 |
17 |
Correct |
2 ms |
6744 KB |
n=100 |
18 |
Correct |
2 ms |
6744 KB |
n=100 |
19 |
Correct |
2 ms |
6744 KB |
n=100 |
20 |
Correct |
2 ms |
6748 KB |
n=100 |
21 |
Correct |
2 ms |
6748 KB |
n=100 |
22 |
Correct |
2 ms |
6748 KB |
n=100 |
23 |
Correct |
11 ms |
6800 KB |
n=100 |
24 |
Correct |
12 ms |
6748 KB |
n=100 |
25 |
Correct |
4 ms |
6748 KB |
n=100 |
26 |
Correct |
2 ms |
6744 KB |
n=12 |
27 |
Correct |
2 ms |
6748 KB |
n=100 |
28 |
Correct |
2 ms |
6748 KB |
n=500 |
29 |
Correct |
15 ms |
6748 KB |
n=500 |
30 |
Correct |
16 ms |
6748 KB |
n=500 |
31 |
Correct |
17 ms |
6848 KB |
n=500 |
32 |
Correct |
2 ms |
6748 KB |
n=500 |
33 |
Correct |
15 ms |
6756 KB |
n=500 |
34 |
Correct |
2 ms |
6748 KB |
n=500 |
35 |
Correct |
13 ms |
6748 KB |
n=500 |
36 |
Correct |
28 ms |
6744 KB |
n=500 |
37 |
Correct |
28 ms |
6744 KB |
n=500 |
38 |
Correct |
27 ms |
6812 KB |
n=500 |
39 |
Correct |
14 ms |
6744 KB |
n=500 |
40 |
Correct |
14 ms |
6748 KB |
n=500 |
41 |
Correct |
14 ms |
6748 KB |
n=500 |
42 |
Correct |
16 ms |
6828 KB |
n=500 |
43 |
Correct |
18 ms |
6832 KB |
n=500 |
44 |
Correct |
16 ms |
6748 KB |
n=500 |
45 |
Correct |
2 ms |
6748 KB |
n=500 |
46 |
Correct |
18 ms |
6748 KB |
n=500 |
47 |
Correct |
13 ms |
6808 KB |
n=500 |
48 |
Correct |
2 ms |
6800 KB |
n=500 |
49 |
Correct |
10 ms |
6748 KB |
n=500 |
50 |
Correct |
8 ms |
6748 KB |
n=500 |
51 |
Correct |
17 ms |
6744 KB |
n=500 |
52 |
Correct |
20 ms |
6744 KB |
n=500 |
53 |
Correct |
16 ms |
6748 KB |
n=500 |
54 |
Correct |
276 ms |
6832 KB |
n=500 |
55 |
Correct |
5 ms |
6944 KB |
n=278 |
56 |
Correct |
2632 ms |
6832 KB |
n=500 |
57 |
Correct |
2742 ms |
6836 KB |
n=500 |
58 |
Correct |
16 ms |
6744 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
n=5 |
2 |
Correct |
2 ms |
6748 KB |
n=100 |
3 |
Correct |
2 ms |
6748 KB |
n=100 |
4 |
Correct |
2 ms |
6748 KB |
n=100 |
5 |
Correct |
2 ms |
6748 KB |
n=100 |
6 |
Correct |
3 ms |
6744 KB |
n=100 |
7 |
Correct |
2 ms |
7000 KB |
n=100 |
8 |
Correct |
2 ms |
6748 KB |
n=100 |
9 |
Correct |
2 ms |
6748 KB |
n=100 |
10 |
Correct |
2 ms |
6748 KB |
n=100 |
11 |
Correct |
3 ms |
6744 KB |
n=100 |
12 |
Correct |
2 ms |
6904 KB |
n=100 |
13 |
Correct |
2 ms |
6744 KB |
n=100 |
14 |
Correct |
2 ms |
6748 KB |
n=100 |
15 |
Correct |
1 ms |
6748 KB |
n=100 |
16 |
Correct |
2 ms |
6748 KB |
n=100 |
17 |
Correct |
2 ms |
6744 KB |
n=100 |
18 |
Correct |
2 ms |
6744 KB |
n=100 |
19 |
Correct |
2 ms |
6744 KB |
n=100 |
20 |
Correct |
2 ms |
6748 KB |
n=100 |
21 |
Correct |
2 ms |
6748 KB |
n=100 |
22 |
Correct |
2 ms |
6748 KB |
n=100 |
23 |
Correct |
11 ms |
6800 KB |
n=100 |
24 |
Correct |
12 ms |
6748 KB |
n=100 |
25 |
Correct |
4 ms |
6748 KB |
n=100 |
26 |
Correct |
2 ms |
6744 KB |
n=12 |
27 |
Correct |
2 ms |
6748 KB |
n=100 |
28 |
Correct |
2 ms |
6748 KB |
n=500 |
29 |
Correct |
15 ms |
6748 KB |
n=500 |
30 |
Correct |
16 ms |
6748 KB |
n=500 |
31 |
Correct |
17 ms |
6848 KB |
n=500 |
32 |
Correct |
2 ms |
6748 KB |
n=500 |
33 |
Correct |
15 ms |
6756 KB |
n=500 |
34 |
Correct |
2 ms |
6748 KB |
n=500 |
35 |
Correct |
13 ms |
6748 KB |
n=500 |
36 |
Correct |
28 ms |
6744 KB |
n=500 |
37 |
Correct |
28 ms |
6744 KB |
n=500 |
38 |
Correct |
27 ms |
6812 KB |
n=500 |
39 |
Correct |
14 ms |
6744 KB |
n=500 |
40 |
Correct |
14 ms |
6748 KB |
n=500 |
41 |
Correct |
14 ms |
6748 KB |
n=500 |
42 |
Correct |
16 ms |
6828 KB |
n=500 |
43 |
Correct |
18 ms |
6832 KB |
n=500 |
44 |
Correct |
16 ms |
6748 KB |
n=500 |
45 |
Correct |
2 ms |
6748 KB |
n=500 |
46 |
Correct |
18 ms |
6748 KB |
n=500 |
47 |
Correct |
13 ms |
6808 KB |
n=500 |
48 |
Correct |
2 ms |
6800 KB |
n=500 |
49 |
Correct |
10 ms |
6748 KB |
n=500 |
50 |
Correct |
8 ms |
6748 KB |
n=500 |
51 |
Correct |
17 ms |
6744 KB |
n=500 |
52 |
Correct |
20 ms |
6744 KB |
n=500 |
53 |
Correct |
16 ms |
6748 KB |
n=500 |
54 |
Correct |
276 ms |
6832 KB |
n=500 |
55 |
Correct |
5 ms |
6944 KB |
n=278 |
56 |
Correct |
2632 ms |
6832 KB |
n=500 |
57 |
Correct |
2742 ms |
6836 KB |
n=500 |
58 |
Correct |
16 ms |
6744 KB |
n=500 |
59 |
Correct |
3 ms |
6748 KB |
n=2000 |
60 |
Correct |
333 ms |
7004 KB |
n=2000 |
61 |
Correct |
266 ms |
7188 KB |
n=2000 |
62 |
Correct |
315 ms |
6992 KB |
n=2000 |
63 |
Correct |
4 ms |
6748 KB |
n=2000 |
64 |
Correct |
303 ms |
6748 KB |
n=2000 |
65 |
Correct |
3 ms |
6748 KB |
n=2000 |
66 |
Correct |
377 ms |
6992 KB |
n=2000 |
67 |
Correct |
6 ms |
6744 KB |
n=2000 |
68 |
Correct |
301 ms |
6960 KB |
n=2000 |
69 |
Correct |
404 ms |
7164 KB |
n=2000 |
70 |
Correct |
397 ms |
6912 KB |
n=2000 |
71 |
Correct |
397 ms |
6808 KB |
n=2000 |
72 |
Correct |
204 ms |
6744 KB |
n=2000 |
73 |
Correct |
214 ms |
6996 KB |
n=2000 |
74 |
Correct |
2 ms |
6748 KB |
n=1844 |
75 |
Correct |
204 ms |
6904 KB |
n=2000 |
76 |
Correct |
235 ms |
6748 KB |
n=2000 |
77 |
Correct |
235 ms |
6908 KB |
n=2000 |
78 |
Correct |
252 ms |
6900 KB |
n=2000 |
79 |
Correct |
4 ms |
7004 KB |
n=2000 |
80 |
Correct |
228 ms |
7016 KB |
n=2000 |
81 |
Correct |
247 ms |
6748 KB |
n=2000 |
82 |
Correct |
3 ms |
6748 KB |
n=2000 |
83 |
Correct |
224 ms |
7000 KB |
n=2000 |
84 |
Correct |
99 ms |
6932 KB |
n=2000 |
85 |
Correct |
629 ms |
7204 KB |
n=2000 |
86 |
Correct |
269 ms |
6748 KB |
n=2000 |
87 |
Correct |
101 ms |
6748 KB |
n=2000 |
88 |
Execution timed out |
4040 ms |
7004 KB |
Time limit exceeded |
89 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
n=5 |
2 |
Correct |
2 ms |
6748 KB |
n=100 |
3 |
Correct |
2 ms |
6748 KB |
n=100 |
4 |
Correct |
2 ms |
6748 KB |
n=100 |
5 |
Correct |
2 ms |
6748 KB |
n=100 |
6 |
Correct |
3 ms |
6744 KB |
n=100 |
7 |
Correct |
2 ms |
7000 KB |
n=100 |
8 |
Correct |
2 ms |
6748 KB |
n=100 |
9 |
Correct |
2 ms |
6748 KB |
n=100 |
10 |
Correct |
2 ms |
6748 KB |
n=100 |
11 |
Correct |
3 ms |
6744 KB |
n=100 |
12 |
Correct |
2 ms |
6904 KB |
n=100 |
13 |
Correct |
2 ms |
6744 KB |
n=100 |
14 |
Correct |
2 ms |
6748 KB |
n=100 |
15 |
Correct |
1 ms |
6748 KB |
n=100 |
16 |
Correct |
2 ms |
6748 KB |
n=100 |
17 |
Correct |
2 ms |
6744 KB |
n=100 |
18 |
Correct |
2 ms |
6744 KB |
n=100 |
19 |
Correct |
2 ms |
6744 KB |
n=100 |
20 |
Correct |
2 ms |
6748 KB |
n=100 |
21 |
Correct |
2 ms |
6748 KB |
n=100 |
22 |
Correct |
2 ms |
6748 KB |
n=100 |
23 |
Correct |
11 ms |
6800 KB |
n=100 |
24 |
Correct |
12 ms |
6748 KB |
n=100 |
25 |
Correct |
4 ms |
6748 KB |
n=100 |
26 |
Correct |
2 ms |
6744 KB |
n=12 |
27 |
Correct |
2 ms |
6748 KB |
n=100 |
28 |
Correct |
2 ms |
6748 KB |
n=500 |
29 |
Correct |
15 ms |
6748 KB |
n=500 |
30 |
Correct |
16 ms |
6748 KB |
n=500 |
31 |
Correct |
17 ms |
6848 KB |
n=500 |
32 |
Correct |
2 ms |
6748 KB |
n=500 |
33 |
Correct |
15 ms |
6756 KB |
n=500 |
34 |
Correct |
2 ms |
6748 KB |
n=500 |
35 |
Correct |
13 ms |
6748 KB |
n=500 |
36 |
Correct |
28 ms |
6744 KB |
n=500 |
37 |
Correct |
28 ms |
6744 KB |
n=500 |
38 |
Correct |
27 ms |
6812 KB |
n=500 |
39 |
Correct |
14 ms |
6744 KB |
n=500 |
40 |
Correct |
14 ms |
6748 KB |
n=500 |
41 |
Correct |
14 ms |
6748 KB |
n=500 |
42 |
Correct |
16 ms |
6828 KB |
n=500 |
43 |
Correct |
18 ms |
6832 KB |
n=500 |
44 |
Correct |
16 ms |
6748 KB |
n=500 |
45 |
Correct |
2 ms |
6748 KB |
n=500 |
46 |
Correct |
18 ms |
6748 KB |
n=500 |
47 |
Correct |
13 ms |
6808 KB |
n=500 |
48 |
Correct |
2 ms |
6800 KB |
n=500 |
49 |
Correct |
10 ms |
6748 KB |
n=500 |
50 |
Correct |
8 ms |
6748 KB |
n=500 |
51 |
Correct |
17 ms |
6744 KB |
n=500 |
52 |
Correct |
20 ms |
6744 KB |
n=500 |
53 |
Correct |
16 ms |
6748 KB |
n=500 |
54 |
Correct |
276 ms |
6832 KB |
n=500 |
55 |
Correct |
5 ms |
6944 KB |
n=278 |
56 |
Correct |
2632 ms |
6832 KB |
n=500 |
57 |
Correct |
2742 ms |
6836 KB |
n=500 |
58 |
Correct |
16 ms |
6744 KB |
n=500 |
59 |
Correct |
3 ms |
6748 KB |
n=2000 |
60 |
Correct |
333 ms |
7004 KB |
n=2000 |
61 |
Correct |
266 ms |
7188 KB |
n=2000 |
62 |
Correct |
315 ms |
6992 KB |
n=2000 |
63 |
Correct |
4 ms |
6748 KB |
n=2000 |
64 |
Correct |
303 ms |
6748 KB |
n=2000 |
65 |
Correct |
3 ms |
6748 KB |
n=2000 |
66 |
Correct |
377 ms |
6992 KB |
n=2000 |
67 |
Correct |
6 ms |
6744 KB |
n=2000 |
68 |
Correct |
301 ms |
6960 KB |
n=2000 |
69 |
Correct |
404 ms |
7164 KB |
n=2000 |
70 |
Correct |
397 ms |
6912 KB |
n=2000 |
71 |
Correct |
397 ms |
6808 KB |
n=2000 |
72 |
Correct |
204 ms |
6744 KB |
n=2000 |
73 |
Correct |
214 ms |
6996 KB |
n=2000 |
74 |
Correct |
2 ms |
6748 KB |
n=1844 |
75 |
Correct |
204 ms |
6904 KB |
n=2000 |
76 |
Correct |
235 ms |
6748 KB |
n=2000 |
77 |
Correct |
235 ms |
6908 KB |
n=2000 |
78 |
Correct |
252 ms |
6900 KB |
n=2000 |
79 |
Correct |
4 ms |
7004 KB |
n=2000 |
80 |
Correct |
228 ms |
7016 KB |
n=2000 |
81 |
Correct |
247 ms |
6748 KB |
n=2000 |
82 |
Correct |
3 ms |
6748 KB |
n=2000 |
83 |
Correct |
224 ms |
7000 KB |
n=2000 |
84 |
Correct |
99 ms |
6932 KB |
n=2000 |
85 |
Correct |
629 ms |
7204 KB |
n=2000 |
86 |
Correct |
269 ms |
6748 KB |
n=2000 |
87 |
Correct |
101 ms |
6748 KB |
n=2000 |
88 |
Execution timed out |
4040 ms |
7004 KB |
Time limit exceeded |
89 |
Halted |
0 ms |
0 KB |
- |