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;
typedef long long ll;
class nums{
public:
ll l,r,id;
};
bool operator<(const nums& a,const nums& b){
if(a.id==b.id){
if(a.l==b.l){
return a.r<b.r;
}
return a.l<b.l;
}
return a.id<b.id;
}
const ll Size=2e5+1;
ll tin[Size],height[Size],lca_tree[4*Size],visited[Size],a[Size];
set<nums> all_pairs[Size];
vector<ll> adj[Size],euler_tour;
void dfs(ll curr_node,ll depth){
tin[curr_node]=euler_tour.size();
height[curr_node]=depth;
visited[curr_node]=1;
euler_tour.push_back(curr_node);
for(auto itr:adj[curr_node]){
if(!visited[itr]){
dfs(itr,depth+1);
euler_tour.push_back(curr_node);
}
}
}
void build_lca(ll l,ll r,ll i){
if(l==r){
lca_tree[i]=euler_tour[r];
}else{
ll mid=(l+r)/2;
build_lca(l,mid,i*2);
build_lca(mid+1,r,i*2+1);
height[lca_tree[i*2]]<height[lca_tree[i*2+1]] ? lca_tree[i]=lca_tree[i*2] : lca_tree[i]=lca_tree[i*2+1];
}
}
ll query_lca(ll l,ll r,ll i,ll tl,ll tr){
if(r<tl or l>tr){
return -1;
}
if(l==tl and r==tr){
return lca_tree[i];
}else{
ll mid=(l+r)/2;
ll n1=query_lca(l,mid,i*2,tl,min(mid,tr));
ll n2=query_lca(mid+1,r,i*2+1,max(tl,mid+1),tr);
if(n1==-1)return n2;
if(n2==-1)return n1;
return height[n1]<height[n2] ? n1 : n2;
}
}
ll lca(ll a,ll b){
if(tin[a]>tin[b]) swap(a,b);
return query_lca(0,euler_tour.size()-1,1,tin[a],tin[b]);
}
void solve(){
ll n,m,q;
cin>>n>>m>>q;
for(ll i=0;i<n-1;i++){
ll from,to;
cin>>from>>to;
adj[from].push_back(to);
adj[to].push_back(from);
}
for(ll i=0;i<m;i++){
cin>>a[i];
}
dfs(1,1);
build_lca(0,euler_tour.size()-1,1);
for(ll i=0;i<m-1;i++){
ll ans=lca(a[i],a[i+1]);
nums tmp;
tmp.id=i;
tmp.l=i;
tmp.r=i+1;
all_pairs[ans].insert(tmp);
tmp.l=i;
tmp.r=i;
all_pairs[a[i]].insert(tmp);
}
nums tmpp;
tmpp.l=m-1;
tmpp.r=m-1;
tmpp.id=m-1;
all_pairs[a[m-1]].insert(tmpp);
for(ll i=0;i<q;i++){
ll type;
cin>>type;
if(type==1){
ll pos,val;
cin>>pos>>val;
pos--;
if(pos!=0){
nums tmp;
tmp.l=pos-1;
tmp.r=pos;
tmp.id=pos-1;
ll lca_of_pair=lca(a[pos-1],a[pos]);
auto found=all_pairs[lca_of_pair].find(tmp);
if(found==all_pairs[lca_of_pair].end()){
cout<<"wtf"<<endl;
return;
}else{
all_pairs[lca_of_pair].erase(tmp);
lca_of_pair=lca(val,a[pos-1]);
all_pairs[lca_of_pair].insert(tmp);
}
}
tmpp.id=pos;
tmpp.l=pos;
tmpp.r=pos;
all_pairs[a[pos]].erase(tmpp);
all_pairs[val].insert(tmpp);
if(pos!=n-1){
nums tmp;
tmp.l=pos;
tmp.r=pos+1;
tmp.id=pos;
ll lca_of_pair=lca(a[pos],a[pos+1]);
auto found=all_pairs[lca_of_pair].find(tmp);
if(found==all_pairs[lca_of_pair].end()){
cout<<"wtf"<<endl;
return;
}else{
all_pairs[lca_of_pair].erase(tmp);
lca_of_pair=lca(val,a[pos+1]);
all_pairs[lca_of_pair].insert(tmp);
}
}
a[pos]=val;
}
if(type==2){
ll l,r,v;
cin>>l>>r>>v;
l--; r--;
nums tmp;
tmp.id=r-1;
tmp.l=n;
tmp.r=n;
auto itr=all_pairs[v].upper_bound(tmp);
if(all_pairs[v].empty()){
cout<<-1<<" "<<-1<<endl;
continue;
}
itr--;
if((*itr).id<l){
cout<<-1<<" "<<-1<<endl;
continue;
}else{
cout<<(*itr).l+1<<" "<<(*itr).r+1<<endl;
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
while(t--){
solve();
}
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... |