#include "bits/stdc++.h"
using namespace std;
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define endl "\n"
#define pb push_back
/*
dont get stuck on one approach
write stuff on paper
edge cases(n=1?)
look for : dp,bs,bf,observations...
implement and debug smarter
*/
const int N = 2e5;
const int LOG = 20;
vector<int> v[N];
int lift[N][LOG];
int depth[N];
void dfs(int c,int p,int d){
depth[c]=d;
lift[c][0]=p;
for(int i=1;i<LOG;i++) lift[c][i]=lift[lift[c][i-1]][i-1];
for(int x:v[c]){
if(x==p) continue;
dfs(x,c,d+1);
}
}
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
int x = depth[a] - depth[b];
for(int i=0;i<LOG;i++) if(x&(1LL<<i)) a=lift[a][i];
if(a==b) return a;
for(int i=LOG-1;i>=0;i--){
if(lift[a][i]!=lift[b][i]){
a=lift[a][i];
b=lift[b][i];
}
}
return lift[a][0];
}
void solve(){
int n,m,q;
cin >> n >> m >> q;
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
dfs(1,0,0);
int ar[m+5];
for(int i=1;i<=m;i++) cin >> ar[i];
while(q--){
int op;
cin >> op;
if(op==1){
int ind,u;
cin >> ind >> u;
ar[ind]=u;
}
else{
int l,r,u;
cin >> l >> r >> u;
bool ok=0;
for(int i=l;i<=r;i++){
int cur = ar[i];
if(ok) break;
for(int j=i;j<=r;j++){
// cout << cur << " " << ar[j] << endl;
cur=lca(cur,ar[j]);
// cout << i << " " << j << " lca: " << cur << endl;
if(cur==u){
ok=1;
cout << i << " " << j << endl;
break;
}
}
if(ok) break;
}
if(ok==0) cout << -1 << " " << -1 << endl;
}
}
}
int32_t main(){
cin.tie(0);
ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
n=5 |
2 |
Correct |
3 ms |
9052 KB |
n=100 |
3 |
Correct |
2 ms |
9052 KB |
n=100 |
4 |
Correct |
2 ms |
9052 KB |
n=100 |
5 |
Correct |
2 ms |
9052 KB |
n=100 |
6 |
Correct |
10 ms |
9052 KB |
n=100 |
7 |
Correct |
11 ms |
9052 KB |
n=100 |
8 |
Correct |
3 ms |
9052 KB |
n=100 |
9 |
Correct |
3 ms |
9052 KB |
n=100 |
10 |
Correct |
6 ms |
9052 KB |
n=100 |
11 |
Correct |
6 ms |
9052 KB |
n=100 |
12 |
Correct |
2 ms |
9052 KB |
n=100 |
13 |
Correct |
2 ms |
9052 KB |
n=100 |
14 |
Correct |
2 ms |
9052 KB |
n=100 |
15 |
Correct |
2 ms |
9048 KB |
n=100 |
16 |
Correct |
2 ms |
9052 KB |
n=100 |
17 |
Correct |
3 ms |
9048 KB |
n=100 |
18 |
Correct |
2 ms |
9052 KB |
n=100 |
19 |
Correct |
2 ms |
9048 KB |
n=100 |
20 |
Correct |
2 ms |
9052 KB |
n=100 |
21 |
Correct |
2 ms |
9052 KB |
n=100 |
22 |
Correct |
2 ms |
9052 KB |
n=100 |
23 |
Correct |
4 ms |
9052 KB |
n=100 |
24 |
Correct |
4 ms |
9052 KB |
n=100 |
25 |
Correct |
3 ms |
9052 KB |
n=100 |
26 |
Correct |
2 ms |
9052 KB |
n=12 |
27 |
Correct |
5 ms |
9128 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
n=5 |
2 |
Correct |
3 ms |
9052 KB |
n=100 |
3 |
Correct |
2 ms |
9052 KB |
n=100 |
4 |
Correct |
2 ms |
9052 KB |
n=100 |
5 |
Correct |
2 ms |
9052 KB |
n=100 |
6 |
Correct |
10 ms |
9052 KB |
n=100 |
7 |
Correct |
11 ms |
9052 KB |
n=100 |
8 |
Correct |
3 ms |
9052 KB |
n=100 |
9 |
Correct |
3 ms |
9052 KB |
n=100 |
10 |
Correct |
6 ms |
9052 KB |
n=100 |
11 |
Correct |
6 ms |
9052 KB |
n=100 |
12 |
Correct |
2 ms |
9052 KB |
n=100 |
13 |
Correct |
2 ms |
9052 KB |
n=100 |
14 |
Correct |
2 ms |
9052 KB |
n=100 |
15 |
Correct |
2 ms |
9048 KB |
n=100 |
16 |
Correct |
2 ms |
9052 KB |
n=100 |
17 |
Correct |
3 ms |
9048 KB |
n=100 |
18 |
Correct |
2 ms |
9052 KB |
n=100 |
19 |
Correct |
2 ms |
9048 KB |
n=100 |
20 |
Correct |
2 ms |
9052 KB |
n=100 |
21 |
Correct |
2 ms |
9052 KB |
n=100 |
22 |
Correct |
2 ms |
9052 KB |
n=100 |
23 |
Correct |
4 ms |
9052 KB |
n=100 |
24 |
Correct |
4 ms |
9052 KB |
n=100 |
25 |
Correct |
3 ms |
9052 KB |
n=100 |
26 |
Correct |
2 ms |
9052 KB |
n=12 |
27 |
Correct |
5 ms |
9128 KB |
n=100 |
28 |
Correct |
3 ms |
9052 KB |
n=500 |
29 |
Correct |
114 ms |
9152 KB |
n=500 |
30 |
Correct |
83 ms |
9208 KB |
n=500 |
31 |
Correct |
91 ms |
9148 KB |
n=500 |
32 |
Correct |
5 ms |
9052 KB |
n=500 |
33 |
Correct |
70 ms |
9304 KB |
n=500 |
34 |
Correct |
2 ms |
9052 KB |
n=500 |
35 |
Correct |
105 ms |
9048 KB |
n=500 |
36 |
Correct |
1147 ms |
9136 KB |
n=500 |
37 |
Correct |
1199 ms |
9136 KB |
n=500 |
38 |
Correct |
1160 ms |
9304 KB |
n=500 |
39 |
Correct |
124 ms |
9048 KB |
n=500 |
40 |
Correct |
142 ms |
9140 KB |
n=500 |
41 |
Correct |
121 ms |
9144 KB |
n=500 |
42 |
Correct |
559 ms |
9136 KB |
n=500 |
43 |
Correct |
581 ms |
9052 KB |
n=500 |
44 |
Correct |
542 ms |
9052 KB |
n=500 |
45 |
Correct |
3 ms |
9128 KB |
n=500 |
46 |
Correct |
109 ms |
9156 KB |
n=500 |
47 |
Correct |
87 ms |
9308 KB |
n=500 |
48 |
Correct |
2 ms |
9052 KB |
n=500 |
49 |
Correct |
78 ms |
9052 KB |
n=500 |
50 |
Correct |
91 ms |
9048 KB |
n=500 |
51 |
Correct |
138 ms |
9052 KB |
n=500 |
52 |
Correct |
175 ms |
9052 KB |
n=500 |
53 |
Correct |
162 ms |
9048 KB |
n=500 |
54 |
Correct |
66 ms |
9052 KB |
n=500 |
55 |
Correct |
18 ms |
9048 KB |
n=278 |
56 |
Correct |
651 ms |
9152 KB |
n=500 |
57 |
Correct |
679 ms |
9148 KB |
n=500 |
58 |
Correct |
465 ms |
9296 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
n=5 |
2 |
Correct |
3 ms |
9052 KB |
n=100 |
3 |
Correct |
2 ms |
9052 KB |
n=100 |
4 |
Correct |
2 ms |
9052 KB |
n=100 |
5 |
Correct |
2 ms |
9052 KB |
n=100 |
6 |
Correct |
10 ms |
9052 KB |
n=100 |
7 |
Correct |
11 ms |
9052 KB |
n=100 |
8 |
Correct |
3 ms |
9052 KB |
n=100 |
9 |
Correct |
3 ms |
9052 KB |
n=100 |
10 |
Correct |
6 ms |
9052 KB |
n=100 |
11 |
Correct |
6 ms |
9052 KB |
n=100 |
12 |
Correct |
2 ms |
9052 KB |
n=100 |
13 |
Correct |
2 ms |
9052 KB |
n=100 |
14 |
Correct |
2 ms |
9052 KB |
n=100 |
15 |
Correct |
2 ms |
9048 KB |
n=100 |
16 |
Correct |
2 ms |
9052 KB |
n=100 |
17 |
Correct |
3 ms |
9048 KB |
n=100 |
18 |
Correct |
2 ms |
9052 KB |
n=100 |
19 |
Correct |
2 ms |
9048 KB |
n=100 |
20 |
Correct |
2 ms |
9052 KB |
n=100 |
21 |
Correct |
2 ms |
9052 KB |
n=100 |
22 |
Correct |
2 ms |
9052 KB |
n=100 |
23 |
Correct |
4 ms |
9052 KB |
n=100 |
24 |
Correct |
4 ms |
9052 KB |
n=100 |
25 |
Correct |
3 ms |
9052 KB |
n=100 |
26 |
Correct |
2 ms |
9052 KB |
n=12 |
27 |
Correct |
5 ms |
9128 KB |
n=100 |
28 |
Correct |
3 ms |
9052 KB |
n=500 |
29 |
Correct |
114 ms |
9152 KB |
n=500 |
30 |
Correct |
83 ms |
9208 KB |
n=500 |
31 |
Correct |
91 ms |
9148 KB |
n=500 |
32 |
Correct |
5 ms |
9052 KB |
n=500 |
33 |
Correct |
70 ms |
9304 KB |
n=500 |
34 |
Correct |
2 ms |
9052 KB |
n=500 |
35 |
Correct |
105 ms |
9048 KB |
n=500 |
36 |
Correct |
1147 ms |
9136 KB |
n=500 |
37 |
Correct |
1199 ms |
9136 KB |
n=500 |
38 |
Correct |
1160 ms |
9304 KB |
n=500 |
39 |
Correct |
124 ms |
9048 KB |
n=500 |
40 |
Correct |
142 ms |
9140 KB |
n=500 |
41 |
Correct |
121 ms |
9144 KB |
n=500 |
42 |
Correct |
559 ms |
9136 KB |
n=500 |
43 |
Correct |
581 ms |
9052 KB |
n=500 |
44 |
Correct |
542 ms |
9052 KB |
n=500 |
45 |
Correct |
3 ms |
9128 KB |
n=500 |
46 |
Correct |
109 ms |
9156 KB |
n=500 |
47 |
Correct |
87 ms |
9308 KB |
n=500 |
48 |
Correct |
2 ms |
9052 KB |
n=500 |
49 |
Correct |
78 ms |
9052 KB |
n=500 |
50 |
Correct |
91 ms |
9048 KB |
n=500 |
51 |
Correct |
138 ms |
9052 KB |
n=500 |
52 |
Correct |
175 ms |
9052 KB |
n=500 |
53 |
Correct |
162 ms |
9048 KB |
n=500 |
54 |
Correct |
66 ms |
9052 KB |
n=500 |
55 |
Correct |
18 ms |
9048 KB |
n=278 |
56 |
Correct |
651 ms |
9152 KB |
n=500 |
57 |
Correct |
679 ms |
9148 KB |
n=500 |
58 |
Correct |
465 ms |
9296 KB |
n=500 |
59 |
Correct |
60 ms |
9048 KB |
n=2000 |
60 |
Execution timed out |
4040 ms |
9320 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9052 KB |
n=5 |
2 |
Correct |
3 ms |
9052 KB |
n=100 |
3 |
Correct |
2 ms |
9052 KB |
n=100 |
4 |
Correct |
2 ms |
9052 KB |
n=100 |
5 |
Correct |
2 ms |
9052 KB |
n=100 |
6 |
Correct |
10 ms |
9052 KB |
n=100 |
7 |
Correct |
11 ms |
9052 KB |
n=100 |
8 |
Correct |
3 ms |
9052 KB |
n=100 |
9 |
Correct |
3 ms |
9052 KB |
n=100 |
10 |
Correct |
6 ms |
9052 KB |
n=100 |
11 |
Correct |
6 ms |
9052 KB |
n=100 |
12 |
Correct |
2 ms |
9052 KB |
n=100 |
13 |
Correct |
2 ms |
9052 KB |
n=100 |
14 |
Correct |
2 ms |
9052 KB |
n=100 |
15 |
Correct |
2 ms |
9048 KB |
n=100 |
16 |
Correct |
2 ms |
9052 KB |
n=100 |
17 |
Correct |
3 ms |
9048 KB |
n=100 |
18 |
Correct |
2 ms |
9052 KB |
n=100 |
19 |
Correct |
2 ms |
9048 KB |
n=100 |
20 |
Correct |
2 ms |
9052 KB |
n=100 |
21 |
Correct |
2 ms |
9052 KB |
n=100 |
22 |
Correct |
2 ms |
9052 KB |
n=100 |
23 |
Correct |
4 ms |
9052 KB |
n=100 |
24 |
Correct |
4 ms |
9052 KB |
n=100 |
25 |
Correct |
3 ms |
9052 KB |
n=100 |
26 |
Correct |
2 ms |
9052 KB |
n=12 |
27 |
Correct |
5 ms |
9128 KB |
n=100 |
28 |
Correct |
3 ms |
9052 KB |
n=500 |
29 |
Correct |
114 ms |
9152 KB |
n=500 |
30 |
Correct |
83 ms |
9208 KB |
n=500 |
31 |
Correct |
91 ms |
9148 KB |
n=500 |
32 |
Correct |
5 ms |
9052 KB |
n=500 |
33 |
Correct |
70 ms |
9304 KB |
n=500 |
34 |
Correct |
2 ms |
9052 KB |
n=500 |
35 |
Correct |
105 ms |
9048 KB |
n=500 |
36 |
Correct |
1147 ms |
9136 KB |
n=500 |
37 |
Correct |
1199 ms |
9136 KB |
n=500 |
38 |
Correct |
1160 ms |
9304 KB |
n=500 |
39 |
Correct |
124 ms |
9048 KB |
n=500 |
40 |
Correct |
142 ms |
9140 KB |
n=500 |
41 |
Correct |
121 ms |
9144 KB |
n=500 |
42 |
Correct |
559 ms |
9136 KB |
n=500 |
43 |
Correct |
581 ms |
9052 KB |
n=500 |
44 |
Correct |
542 ms |
9052 KB |
n=500 |
45 |
Correct |
3 ms |
9128 KB |
n=500 |
46 |
Correct |
109 ms |
9156 KB |
n=500 |
47 |
Correct |
87 ms |
9308 KB |
n=500 |
48 |
Correct |
2 ms |
9052 KB |
n=500 |
49 |
Correct |
78 ms |
9052 KB |
n=500 |
50 |
Correct |
91 ms |
9048 KB |
n=500 |
51 |
Correct |
138 ms |
9052 KB |
n=500 |
52 |
Correct |
175 ms |
9052 KB |
n=500 |
53 |
Correct |
162 ms |
9048 KB |
n=500 |
54 |
Correct |
66 ms |
9052 KB |
n=500 |
55 |
Correct |
18 ms |
9048 KB |
n=278 |
56 |
Correct |
651 ms |
9152 KB |
n=500 |
57 |
Correct |
679 ms |
9148 KB |
n=500 |
58 |
Correct |
465 ms |
9296 KB |
n=500 |
59 |
Correct |
60 ms |
9048 KB |
n=2000 |
60 |
Execution timed out |
4040 ms |
9320 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |