#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
#define _ '_'<<
const int mod = 1e9+7, N = 2e5+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int sz, n, m, k;
vector<int> edges[N];
int tin[N], tout[N];
int up[N][13];
int timer;
int depth[N];
void dfs(int u, int p, int d=0){
tin[u] = timer++;
depth[u] = d;
up[u][0] = p;
for(int i=1;i<13;i++)up[u][i] = up[up[u][i-1]][i-1];
for(auto v : edges[u]){
if(v != p){
dfs(v, u,d+1);
}
}tout[u] = timer++;
}
bool isancestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if(isancestor(u,v))return u;
if(isancestor(v,u))return v;
for(int i=12;i>=0;i--){
if(!isancestor(up[u][i], v))u = up[u][i];
}return up[u][0];
}
int t[N*4];
void update(int p, int val){
p += sz;
for(t[p] = val; p>1; p>>=1)t[p>>1] = lca(t[p], t[p^1]);
}
int rangelca(int l, int r){
int ans = 0;
for(l+=sz,r+=sz;l<=r;l>>=1,r>>=1){
if(l%2==1){
if(ans == 0)ans = t[l++];
else ans = lca(ans, t[l++]);
}if(r%2==0){
if(ans == 0)ans = t[r--];
else ans = lca(ans, t[r--]);
}
}return ans;
}
void solve(int test_case){
int i, j, q;
cin >> n >> m >> q;
for(i=1;i<n;i++){
int u, v;
cin >> u >> v;
edges[u].pb(v);
edges[v].pb(u);
}dfs(1,1);
sz=1;
while(sz <= m)sz<<=1;
for(i=0;i<m;i++){
cin >> t[i+sz];
}for(i=sz-1;i>0;i--){
t[i] = lca(t[i*2], t[i*2+1]);
}
while(q--){
int type;
cin >> type;
if(type == 1){
int pos, val;
cin >> pos >> val;
update(pos-1, val);
}else {
int l, r, v;
cin >> l >> r >> v;
l--,r--;
bool flag = true;
int L, R;
int ansl=-2, ansr=-2;
for(L=l; L <= r && flag; L++){
for(R=L; R <= r && flag; R++){
if(rangelca(L, R) == v){
ansl = L, ansr = R;
flag = false;
break;
}
}
}assert(ansl == -2 && ansr == -2 || ansl >= l && ansr <= r);
cout << ansl+1 <<" "<< ansr+1 << '\n';
/* int L=l, R=l;
for(;L <= r; L++){
R = max(R, L);
if(rangelca(L,R) == v){L++;break;}
while(R+1 <= r && depth[lca(v, rangelca(L, R+1))] <= depth[v]){
if(rangelca(L,R) == v){L++;break;}
R++;
}
if(rangelca(L,R) == v){L++;break;}
}L--;
// cout << rangelca(L, R) << '\n';
if(rangelca(L,R) == v)cout << L+1 << ' ' << R+1 << '\n';
else cout << "-1 -1\n";*/
}
}
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int ___T;
cin >> ___T;
for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
Compilation message
In file included from /usr/include/c++/9/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
from treearray.cpp:1:
treearray.cpp: In function 'void solve(int)':
treearray.cpp:99:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
99 | }assert(ansl == -2 && ansr == -2 || ansl >= l && ansr <= r);
| ~~~~~~~~~~~^~~~~~~~~~~~~
treearray.cpp:62:9: warning: unused variable 'j' [-Wunused-variable]
62 | int i, j, q;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
7 ms |
5100 KB |
n=100 |
3 |
Correct |
6 ms |
5100 KB |
n=100 |
4 |
Correct |
5 ms |
5100 KB |
n=100 |
5 |
Correct |
4 ms |
5100 KB |
n=100 |
6 |
Correct |
29 ms |
5100 KB |
n=100 |
7 |
Correct |
31 ms |
5228 KB |
n=100 |
8 |
Correct |
6 ms |
5100 KB |
n=100 |
9 |
Correct |
6 ms |
5100 KB |
n=100 |
10 |
Correct |
18 ms |
5100 KB |
n=100 |
11 |
Correct |
18 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
4 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
7 ms |
5100 KB |
n=100 |
18 |
Correct |
6 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
6 ms |
5100 KB |
n=100 |
21 |
Correct |
5 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
10 ms |
5100 KB |
n=100 |
24 |
Correct |
9 ms |
5100 KB |
n=100 |
25 |
Correct |
5 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
16 ms |
5100 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
7 ms |
5100 KB |
n=100 |
3 |
Correct |
6 ms |
5100 KB |
n=100 |
4 |
Correct |
5 ms |
5100 KB |
n=100 |
5 |
Correct |
4 ms |
5100 KB |
n=100 |
6 |
Correct |
29 ms |
5100 KB |
n=100 |
7 |
Correct |
31 ms |
5228 KB |
n=100 |
8 |
Correct |
6 ms |
5100 KB |
n=100 |
9 |
Correct |
6 ms |
5100 KB |
n=100 |
10 |
Correct |
18 ms |
5100 KB |
n=100 |
11 |
Correct |
18 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
4 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
7 ms |
5100 KB |
n=100 |
18 |
Correct |
6 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
6 ms |
5100 KB |
n=100 |
21 |
Correct |
5 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
10 ms |
5100 KB |
n=100 |
24 |
Correct |
9 ms |
5100 KB |
n=100 |
25 |
Correct |
5 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
16 ms |
5100 KB |
n=100 |
28 |
Correct |
11 ms |
5100 KB |
n=500 |
29 |
Correct |
332 ms |
5100 KB |
n=500 |
30 |
Correct |
262 ms |
5228 KB |
n=500 |
31 |
Correct |
295 ms |
5228 KB |
n=500 |
32 |
Correct |
16 ms |
5100 KB |
n=500 |
33 |
Correct |
261 ms |
5184 KB |
n=500 |
34 |
Correct |
4 ms |
5100 KB |
n=500 |
35 |
Correct |
279 ms |
5100 KB |
n=500 |
36 |
Execution timed out |
4078 ms |
5228 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
7 ms |
5100 KB |
n=100 |
3 |
Correct |
6 ms |
5100 KB |
n=100 |
4 |
Correct |
5 ms |
5100 KB |
n=100 |
5 |
Correct |
4 ms |
5100 KB |
n=100 |
6 |
Correct |
29 ms |
5100 KB |
n=100 |
7 |
Correct |
31 ms |
5228 KB |
n=100 |
8 |
Correct |
6 ms |
5100 KB |
n=100 |
9 |
Correct |
6 ms |
5100 KB |
n=100 |
10 |
Correct |
18 ms |
5100 KB |
n=100 |
11 |
Correct |
18 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
4 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
7 ms |
5100 KB |
n=100 |
18 |
Correct |
6 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
6 ms |
5100 KB |
n=100 |
21 |
Correct |
5 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
10 ms |
5100 KB |
n=100 |
24 |
Correct |
9 ms |
5100 KB |
n=100 |
25 |
Correct |
5 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
16 ms |
5100 KB |
n=100 |
28 |
Correct |
11 ms |
5100 KB |
n=500 |
29 |
Correct |
332 ms |
5100 KB |
n=500 |
30 |
Correct |
262 ms |
5228 KB |
n=500 |
31 |
Correct |
295 ms |
5228 KB |
n=500 |
32 |
Correct |
16 ms |
5100 KB |
n=500 |
33 |
Correct |
261 ms |
5184 KB |
n=500 |
34 |
Correct |
4 ms |
5100 KB |
n=500 |
35 |
Correct |
279 ms |
5100 KB |
n=500 |
36 |
Execution timed out |
4078 ms |
5228 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
n=5 |
2 |
Correct |
7 ms |
5100 KB |
n=100 |
3 |
Correct |
6 ms |
5100 KB |
n=100 |
4 |
Correct |
5 ms |
5100 KB |
n=100 |
5 |
Correct |
4 ms |
5100 KB |
n=100 |
6 |
Correct |
29 ms |
5100 KB |
n=100 |
7 |
Correct |
31 ms |
5228 KB |
n=100 |
8 |
Correct |
6 ms |
5100 KB |
n=100 |
9 |
Correct |
6 ms |
5100 KB |
n=100 |
10 |
Correct |
18 ms |
5100 KB |
n=100 |
11 |
Correct |
18 ms |
5100 KB |
n=100 |
12 |
Correct |
4 ms |
5100 KB |
n=100 |
13 |
Correct |
4 ms |
5100 KB |
n=100 |
14 |
Correct |
4 ms |
5100 KB |
n=100 |
15 |
Correct |
4 ms |
5100 KB |
n=100 |
16 |
Correct |
4 ms |
5100 KB |
n=100 |
17 |
Correct |
7 ms |
5100 KB |
n=100 |
18 |
Correct |
6 ms |
5100 KB |
n=100 |
19 |
Correct |
4 ms |
5100 KB |
n=100 |
20 |
Correct |
6 ms |
5100 KB |
n=100 |
21 |
Correct |
5 ms |
5100 KB |
n=100 |
22 |
Correct |
4 ms |
5100 KB |
n=100 |
23 |
Correct |
10 ms |
5100 KB |
n=100 |
24 |
Correct |
9 ms |
5100 KB |
n=100 |
25 |
Correct |
5 ms |
5100 KB |
n=100 |
26 |
Correct |
4 ms |
5100 KB |
n=12 |
27 |
Correct |
16 ms |
5100 KB |
n=100 |
28 |
Correct |
11 ms |
5100 KB |
n=500 |
29 |
Correct |
332 ms |
5100 KB |
n=500 |
30 |
Correct |
262 ms |
5228 KB |
n=500 |
31 |
Correct |
295 ms |
5228 KB |
n=500 |
32 |
Correct |
16 ms |
5100 KB |
n=500 |
33 |
Correct |
261 ms |
5184 KB |
n=500 |
34 |
Correct |
4 ms |
5100 KB |
n=500 |
35 |
Correct |
279 ms |
5100 KB |
n=500 |
36 |
Execution timed out |
4078 ms |
5228 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |