#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define pb push_back
#define fi first
#define se second
const ll N = 2e5+5;
const ll LOG = 20;
ll n,m,q,u,v;
ll l,r,mid,ans1,ans2,val;
ll var,x,y,upd,event;
bool ans;
ll depth[N],a[N];
ll anc[N][LOG];
vector<ll> adj[N];
void dfs(ll x, ll y) {
anc[x][0] = y;
depth[x] = depth[y] + 1;
for(int i=1; i<LOG; i++) {
anc[x][i] = anc[anc[x][i-1]][i-1];
}
for(auto i : adj[x]) {
if(i == y) continue;
dfs(i,x);
}
}
ll lca(ll x, ll y) {
if(depth[x] < depth[y]) swap(x,y);
for(int i=LOG-1; i>=0; i--) {
if(depth[anc[x][i]] >= depth[y]) {
x = anc[x][i];
}
}
if(x == y) return x;
for(int i=LOG-1; i>=0; i--) {
if(anc[x][i] != anc[y][i]) {
x = anc[x][i];
y = anc[y][i];
}
}
return anc[x][0];
}
ll check(ll x, ll y) {
ll temp = a[x];
for(int i=x+1; i<=y; i++) {
temp = lca(temp,a[i]);
}
return temp;
}
int main() {
cin >> n >> m >> q;
for(int i=1; i<n; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,0);
for(int i=1; i<=m; i++) {
cin >> a[i];
}
while(q--) {
cin >> event;
if(event == 1) {
cin >> upd >> val;
a[upd] = val;
}
else {
ans = false;
cin >> x >> y >> var;
ans1 = -1; ans2 = -1;
for(int i=x; i<=y; i++) {
l = i;
r = y;
while(l <= r) {
mid = (l+r)/2;
ll ret = check(i,mid);
if(depth[ret] < depth[var]) {
r = mid - 1;
}
else if(depth[ret] > depth[var]) {
l = mid + 1;
}
else if(ret == var) {
ans1 = i;
ans2 = mid;
ans = true;
break;
}
else break;
}
if(ans) break;
}
cout << ans1 << " " << ans2 << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
n=5 |
2 |
Correct |
6 ms |
8792 KB |
n=100 |
3 |
Correct |
5 ms |
8792 KB |
n=100 |
4 |
Correct |
4 ms |
8792 KB |
n=100 |
5 |
Correct |
4 ms |
8792 KB |
n=100 |
6 |
Correct |
37 ms |
8796 KB |
n=100 |
7 |
Correct |
37 ms |
8796 KB |
n=100 |
8 |
Correct |
6 ms |
8792 KB |
n=100 |
9 |
Correct |
6 ms |
8792 KB |
n=100 |
10 |
Correct |
21 ms |
8792 KB |
n=100 |
11 |
Correct |
20 ms |
8792 KB |
n=100 |
12 |
Correct |
2 ms |
8796 KB |
n=100 |
13 |
Correct |
3 ms |
8792 KB |
n=100 |
14 |
Correct |
2 ms |
8792 KB |
n=100 |
15 |
Correct |
2 ms |
8792 KB |
n=100 |
16 |
Correct |
3 ms |
8792 KB |
n=100 |
17 |
Correct |
7 ms |
8792 KB |
n=100 |
18 |
Correct |
6 ms |
8792 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
5 ms |
8792 KB |
n=100 |
21 |
Correct |
5 ms |
8792 KB |
n=100 |
22 |
Correct |
2 ms |
8792 KB |
n=100 |
23 |
Correct |
47 ms |
8792 KB |
n=100 |
24 |
Correct |
51 ms |
8792 KB |
n=100 |
25 |
Correct |
12 ms |
8792 KB |
n=100 |
26 |
Correct |
2 ms |
8792 KB |
n=12 |
27 |
Correct |
16 ms |
8792 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
n=5 |
2 |
Correct |
6 ms |
8792 KB |
n=100 |
3 |
Correct |
5 ms |
8792 KB |
n=100 |
4 |
Correct |
4 ms |
8792 KB |
n=100 |
5 |
Correct |
4 ms |
8792 KB |
n=100 |
6 |
Correct |
37 ms |
8796 KB |
n=100 |
7 |
Correct |
37 ms |
8796 KB |
n=100 |
8 |
Correct |
6 ms |
8792 KB |
n=100 |
9 |
Correct |
6 ms |
8792 KB |
n=100 |
10 |
Correct |
21 ms |
8792 KB |
n=100 |
11 |
Correct |
20 ms |
8792 KB |
n=100 |
12 |
Correct |
2 ms |
8796 KB |
n=100 |
13 |
Correct |
3 ms |
8792 KB |
n=100 |
14 |
Correct |
2 ms |
8792 KB |
n=100 |
15 |
Correct |
2 ms |
8792 KB |
n=100 |
16 |
Correct |
3 ms |
8792 KB |
n=100 |
17 |
Correct |
7 ms |
8792 KB |
n=100 |
18 |
Correct |
6 ms |
8792 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
5 ms |
8792 KB |
n=100 |
21 |
Correct |
5 ms |
8792 KB |
n=100 |
22 |
Correct |
2 ms |
8792 KB |
n=100 |
23 |
Correct |
47 ms |
8792 KB |
n=100 |
24 |
Correct |
51 ms |
8792 KB |
n=100 |
25 |
Correct |
12 ms |
8792 KB |
n=100 |
26 |
Correct |
2 ms |
8792 KB |
n=12 |
27 |
Correct |
16 ms |
8792 KB |
n=100 |
28 |
Correct |
11 ms |
8792 KB |
n=500 |
29 |
Correct |
466 ms |
8792 KB |
n=500 |
30 |
Correct |
351 ms |
8792 KB |
n=500 |
31 |
Correct |
413 ms |
8792 KB |
n=500 |
32 |
Correct |
19 ms |
8828 KB |
n=500 |
33 |
Correct |
341 ms |
8836 KB |
n=500 |
34 |
Correct |
6 ms |
8792 KB |
n=500 |
35 |
Correct |
393 ms |
8796 KB |
n=500 |
36 |
Execution timed out |
4011 ms |
8792 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
n=5 |
2 |
Correct |
6 ms |
8792 KB |
n=100 |
3 |
Correct |
5 ms |
8792 KB |
n=100 |
4 |
Correct |
4 ms |
8792 KB |
n=100 |
5 |
Correct |
4 ms |
8792 KB |
n=100 |
6 |
Correct |
37 ms |
8796 KB |
n=100 |
7 |
Correct |
37 ms |
8796 KB |
n=100 |
8 |
Correct |
6 ms |
8792 KB |
n=100 |
9 |
Correct |
6 ms |
8792 KB |
n=100 |
10 |
Correct |
21 ms |
8792 KB |
n=100 |
11 |
Correct |
20 ms |
8792 KB |
n=100 |
12 |
Correct |
2 ms |
8796 KB |
n=100 |
13 |
Correct |
3 ms |
8792 KB |
n=100 |
14 |
Correct |
2 ms |
8792 KB |
n=100 |
15 |
Correct |
2 ms |
8792 KB |
n=100 |
16 |
Correct |
3 ms |
8792 KB |
n=100 |
17 |
Correct |
7 ms |
8792 KB |
n=100 |
18 |
Correct |
6 ms |
8792 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
5 ms |
8792 KB |
n=100 |
21 |
Correct |
5 ms |
8792 KB |
n=100 |
22 |
Correct |
2 ms |
8792 KB |
n=100 |
23 |
Correct |
47 ms |
8792 KB |
n=100 |
24 |
Correct |
51 ms |
8792 KB |
n=100 |
25 |
Correct |
12 ms |
8792 KB |
n=100 |
26 |
Correct |
2 ms |
8792 KB |
n=12 |
27 |
Correct |
16 ms |
8792 KB |
n=100 |
28 |
Correct |
11 ms |
8792 KB |
n=500 |
29 |
Correct |
466 ms |
8792 KB |
n=500 |
30 |
Correct |
351 ms |
8792 KB |
n=500 |
31 |
Correct |
413 ms |
8792 KB |
n=500 |
32 |
Correct |
19 ms |
8828 KB |
n=500 |
33 |
Correct |
341 ms |
8836 KB |
n=500 |
34 |
Correct |
6 ms |
8792 KB |
n=500 |
35 |
Correct |
393 ms |
8796 KB |
n=500 |
36 |
Execution timed out |
4011 ms |
8792 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
n=5 |
2 |
Correct |
6 ms |
8792 KB |
n=100 |
3 |
Correct |
5 ms |
8792 KB |
n=100 |
4 |
Correct |
4 ms |
8792 KB |
n=100 |
5 |
Correct |
4 ms |
8792 KB |
n=100 |
6 |
Correct |
37 ms |
8796 KB |
n=100 |
7 |
Correct |
37 ms |
8796 KB |
n=100 |
8 |
Correct |
6 ms |
8792 KB |
n=100 |
9 |
Correct |
6 ms |
8792 KB |
n=100 |
10 |
Correct |
21 ms |
8792 KB |
n=100 |
11 |
Correct |
20 ms |
8792 KB |
n=100 |
12 |
Correct |
2 ms |
8796 KB |
n=100 |
13 |
Correct |
3 ms |
8792 KB |
n=100 |
14 |
Correct |
2 ms |
8792 KB |
n=100 |
15 |
Correct |
2 ms |
8792 KB |
n=100 |
16 |
Correct |
3 ms |
8792 KB |
n=100 |
17 |
Correct |
7 ms |
8792 KB |
n=100 |
18 |
Correct |
6 ms |
8792 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
5 ms |
8792 KB |
n=100 |
21 |
Correct |
5 ms |
8792 KB |
n=100 |
22 |
Correct |
2 ms |
8792 KB |
n=100 |
23 |
Correct |
47 ms |
8792 KB |
n=100 |
24 |
Correct |
51 ms |
8792 KB |
n=100 |
25 |
Correct |
12 ms |
8792 KB |
n=100 |
26 |
Correct |
2 ms |
8792 KB |
n=12 |
27 |
Correct |
16 ms |
8792 KB |
n=100 |
28 |
Correct |
11 ms |
8792 KB |
n=500 |
29 |
Correct |
466 ms |
8792 KB |
n=500 |
30 |
Correct |
351 ms |
8792 KB |
n=500 |
31 |
Correct |
413 ms |
8792 KB |
n=500 |
32 |
Correct |
19 ms |
8828 KB |
n=500 |
33 |
Correct |
341 ms |
8836 KB |
n=500 |
34 |
Correct |
6 ms |
8792 KB |
n=500 |
35 |
Correct |
393 ms |
8796 KB |
n=500 |
36 |
Execution timed out |
4011 ms |
8792 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |