This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define double long double
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<int,int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(),x.end()
#define de(x) cout << #x << ' ' << x << '\n';
template<class T> void prt(T l,T r){
while(l!=r) cout << *l << ' ',l++;
cout << '\n';
}
const int N=2e5+5,M=1e9+7,INF=1e9;
vector<int> G[N];
int dep[N],fa[N],jump[N];
void dfs(int u){
if(dep[jump[fa[u]]]-dep[fa[u]]==dep[jump[jump[fa[u]]]]-dep[jump[fa[u]]]) jump[u]=jump[jump[fa[u]]];
else jump[u]=fa[u];
for(auto v:G[u]){
if(v==fa[u]) continue;
fa[v]=u;
dep[v]=dep[u]+1;
dfs(v);
}
}
int lca(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
while(dep[u]>dep[v]){
if(dep[jump[u]]>=dep[v]) u=jump[u];
else u=fa[u];
}
while(u!=v){
if(jump[u]!=jump[v]){
u=jump[u];
v=jump[v];
}else{
u=fa[u];
v=fa[v];
}
}
return u;
}
multiset<pii> s[4*N];
void ins(int l,int r,int cur,int ind,int val){
if(r-l<=1){
s[cur].insert(mp(val,ind));
return;
}
s[cur].insert(mp(val,ind));
int mid=(l+r)/2;
if(ind<mid) ins(l,mid,cur*2,ind,val);
else ins(mid,r,cur*2+1,ind,val);
}
void del(int l,int r,int cur,int ind,int val){
s[cur].erase(s[cur].lower_bound(mp(val,ind)));
if(r-l<=1){
return;
}
int mid=(l+r)/2;
if(ind<mid) del(l,mid,cur*2,ind,val);
else del(mid,r,cur*2+1,ind,val);
}
int fi(int l,int r,int cur,int ql,int qr,int val){
if(ql>=r||qr<=l) return 0;
if(ql<=l&&qr>=r){
auto it=s[cur].lower_bound(mp(val,0));
if(it->ff==val) return it->ss;
else return 0;
}
int mid=(l+r)/2;
return max(fi(l,mid,cur*2,ql,qr,val),fi(mid,r,cur*2+1,ql,qr,val));
}
signed main(){
IO
int n,m,q;cin >> n >> m >> q;
for(int i=0,u,v;i<n-1;i++){
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
fa[1]=jump[1]=dep[1]=1;
dfs(1);
vector<int> a(m+1),lc(m+1);
for(int i=1;i<=m;i++) cin >> a[i];
for(int i=1;i<m;i++){
lc[i]=lca(a[i],a[i+1]);
ins(1,m,1,i,lc[i]);
ins(1,m,1,i,a[i]);
}
int t,pos,l,r,v;
while(q--){
cin >> t;
if(t==1){
cin >> pos >> v;
if(pos!=1){
del(1,m,1,pos-1,lc[pos-1]);
}
if(pos!=m){
del(1,m,1,pos,lc[pos]);
del(1,m,1,pos,a[pos]);
}
a[pos]=v;
if(pos!=1){
lc[pos-1]=lca(a[pos-1],a[pos]);
ins(1,m,1,pos-1,lc[pos-1]);
}
if(pos!=m){
lc[pos]=lca(a[pos],a[pos+1]);
ins(1,m,1,pos,lc[pos]);
ins(1,m,1,pos,a[pos]);
}
}else{
cin >> l >> r >> v;
int x=fi(1,m,1,l,r,v);
if(a[r]==v) x=r;
if(x){
if(a[x]==v) cout << x << ' ' << x << '\n';
else cout << x << ' ' << x+1 << '\n';
}else cout << "-1 -1\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |