#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, q;
vector <int> adj[300005];
multiset <int> ms[300005];
int S[300005], E[300005], cnt = 1, par[20][200005], dep[200005];
void dfs(int x, int p, int d){
S[x] = cnt++;
par[0][x] = p;
dep[x] = d;
for(auto i : adj[x])if(i != p)dfs(i, x, d + 1);
E[x] = cnt - 1;
}
int lca(int u, int v){
if(dep[u] > dep[v])swap(u, v);
int df = dep[v] - dep[u];
for(int i=0;i<19;i++)if(df >> i & 1)v = par[i][v];
if(u == v)return u;
for(int i=19;i>=0;i--){
if(par[i][u] != par[i][v])u = par[i][u], v = par[i][v];
}
return par[0][u];
}
int back[300005];
struct node{
int s, e, m, mn, mx, mx2;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e)l = new node(s, m), r = new node(m+1, e);
mn = 1e9, mx = mx2 = 0;
}
void upd(int p, int v, int v2){
if(s == e)mn = mx2 = v, mx = v2;
else{
if(p <= m)l->upd(p, v, v2);
else r->upd(p, v, v2);
mn = min(l->mn, r->mn);
mx = max(l->mx, r->mx);
mx2 = max(l->mx2, r->mx2);
}
}
int q1(int a, int b){
if(a == s && b == e)return mn;
if(b <= m)return l->q1(a, b);
if(a > m)return r->q1(a, b);
return min(l->q1(a, m), r->q1(m+1, b));
}
int q2(int a, int b){
if(a == s && b == e)return mx;
if(b <= m)return l->q2(a, b);
if(a > m)return r->q2(a, b);
return max(l->q2(a, m), r->q2(m+1, b));
}
int q3(int a, int b){
if(a == s && b == e)return mx2;
if(b <= m)return l->q3(a, b);
if(a > m)return r->q3(a, b);
return max(l->q3(a, m), r->q3(m+1, b));
}
}*root;
int A[300005], f, v;
void dnc(int l, int r){
if(f || l > r)return;
int mid = (l + r) >> 1;
if(f)return;
int lo = mid + 1, hi = r, tmp = hi + 1;
while(lo <= hi){
int mm = (lo + hi) >> 1;
if(root->q1(mid + 1, mm) < S[v] || root->q2(mid + 1, mm) > E[v])tmp = mm, hi = mm - 1;
else lo = mm + 1;
}
tmp--;
lo = l, hi = mid;
int tmp2 = lo - 1;
while(lo <= hi){
int mm = (lo + hi) >> 1;
if(root->q1(mm, mid) < S[v] || root->q2(mm, mid) > E[v])tmp2 = mm, lo = mm + 1;
else hi = mm - 1;
}
tmp2++;
if(tmp2 > tmp)return;
hi = root->q1(tmp2, tmp); int hi2 = root->q3(tmp2, tmp);
//cerr << l << ' ' << r << ' ' << hi << ' ' << hi2 << '\n';
if(lca(back[hi], back[hi2]) == v){
cout << tmp2 << ' ' << tmp << '\n';
f = 1;
return;
}
dnc(l, tmp2 - 2); dnc(tmp + 2, r);
}
void solve(){
cin >> n >> m >> q;
for(int i=1;i<n;i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1, 1);
root = new node(1, m);
for(int i=1;i<=m;i++)cin >> A[i], root->upd(i, S[A[i]], E[A[i]]);
/*
root = new node(1, n);
for(int i=1;i<=n;i++){
if(!ms[i].empty()){
auto it = ms[i].begin(), it2 = --ms[i].end();
root->upd(S[i], *it, *it2);
}
}
*/
for(int i=1;i<=n;i++)back[S[i]] = i;
for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)par[i][j] = par[i-1][par[i-1][j]];
cerr << "ok\n";
while(q--){
int t; cin >> t;
if(t == 1){
int x, y; cin >> x >> y;
root->upd(x, S[y], E[y]);
}
else{
int l, r; cin >> l >> r >> v;
f = 0;
dnc(l, r);
if(!f)cout << -1 << ' ' << -1 << '\n';
}
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
treearray.cpp:149:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
149 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61528 KB |
n=5 |
2 |
Incorrect |
14 ms |
61532 KB |
Jury has the answer but participant has not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61528 KB |
n=5 |
2 |
Incorrect |
14 ms |
61532 KB |
Jury has the answer but participant has not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61528 KB |
n=5 |
2 |
Incorrect |
14 ms |
61532 KB |
Jury has the answer but participant has not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
61528 KB |
n=5 |
2 |
Incorrect |
14 ms |
61532 KB |
Jury has the answer but participant has not |
3 |
Halted |
0 ms |
0 KB |
- |