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>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll N=2e5+5;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define lwb lower_bound
ll depth[N],anc[N][20];
ll lca(ll nda,ll ndb){
if(depth[nda]<depth[ndb])swap(nda,ndb);
for(int i=19;i>=0;i--){
if(depth[anc[nda][i]]>=depth[ndb]){
nda=anc[nda][i];
}
}
if(nda==ndb)return nda;
for(int i=19;i>=0;i--){
if(anc[nda][i]!=anc[ndb][i]){
nda=anc[nda][i];
ndb=anc[ndb][i];
}
}
return anc[nda][0];
}
ll n,m,x,y,q,u[N],as[N];
vector<ll> v[N];
set<ll> loc[N],is[N];
void build_LCA(ll nd,ll pa){
depth[nd]=depth[pa]+1;
anc[nd][0]=pa;
REP1(i,19)anc[nd][i]=anc[anc[nd][i-1]][i-1];
for(ll i:v[nd]){
if(i==pa)continue;
build_LCA(i,nd);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m>>q;
REP(i,n-1){
cin>>x>>y;
v[x].pb(y);v[y].pb(x);
}
build_LCA(1,0);
REP1(i,m)cin>>u[i],is[u[i]].insert(i);
REP1(i,m-1)as[i]=lca(u[i],u[i+1]),loc[as[i]].insert(i);
while(q--){
ll ty,now;
cin>>ty;
if(ty==1){
cin>>x>>y;
if(x>1)loc[as[x-1]].erase(x-1);
if(x<m)loc[as[x]].erase(x);
is[u[x]].erase(x);
u[x]=y;
is[u[x]].insert(x);
if(x>1)as[x-1]=lca(u[x-1],u[x]),loc[as[x-1]].insert(x-1);
if(x<m)as[x]=lca(u[x],u[x+1]),loc[as[x]].insert(x);
}else{
cin>>x>>y>>now;
auto it=loc[now].lwb(x),where=is[now].lwb(x);
if((it==loc[now].end()||(*it)>=y)&&(where==is[now].end()||*where>y)){
cout<<"-1 -1\n";
}else{
if(where!=is[now].end()&&*where<=y){
cout<<*where<<" "<<*where<<"\n";
}else{
cout<<*it<<" "<<*it+1<<"\n";
}
}
}
}
return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# | 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... |