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>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;
const int maxn=2e5+10;
const int logg=20;
int n,m;
int niz[maxn];
vector<int> g[maxn];
pii vreme[maxn];
int vr=0;
int lift[maxn][logg];
set<int> nesto[maxn];
set<int> svi[maxn];
void dfs(int cvor,int prosli){
vreme[cvor].xx=vr++;
lift[cvor][0]=prosli;
for(int i=1;i<logg;i++){
if(lift[cvor][i-1]==-1) lift[cvor][i]=-1;
else lift[cvor][i]=lift[lift[cvor][i-1]][i-1];
}
for(int i:g[cvor]){
if(i==prosli) continue;
dfs(i,cvor);
}
vreme[cvor].yy=vr++;
}
bool iznad(int a,int b){
if(a==-1) return true;
if(vreme[a].xx<vreme[b].xx && vreme[b].yy<vreme[a].yy) return true;
return false;
}
int lca(int a,int b){
//cout<<a<<" "<<b<<" ";
//cout.flush();
for(int i=logg-1;i>=0;i--){
if(!iznad(lift[a][i],b)){
a=lift[a][i];
}
}
//cout<<lift[a][0]<<"\n";
return (iznad(a,b) ? a:lift[a][0]);
}
void skloni(int a){
if(a<0 || a>=m-1) return;
nesto[lca(niz[a],niz[a+1])].erase(a);
}
void dodaj(int a){
if(a<0 || a>=m-1) return;
nesto[lca(niz[a],niz[a+1])].emplace(a);
}
void solve(){
int q;
cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
dfs(1,-1);
for(int i=0;i<m;i++) cin>>niz[i];
for(int i=0;i<m;i++){
dodaj(i);
svi[niz[i]].emplace(i);
}
for(int i=0;i<q;i++){
int t;
cin>>t;
if(t==1){
int t,v;
cin>>t>>v;
t--;
svi[niz[t]].erase(t);
svi[v].emplace(t);
skloni(t-1);
skloni(t);
niz[t]=v;
dodaj(t-1);
dodaj(t);
}else{
int l,r,v;
cin>>l>>r>>v;
l--,r--;
auto it=nesto[v].lower_bound(l);
auto it2=svi[v].lower_bound(l);
if((it==nesto[v].end() || *it>=r)&&
(it2==svi[v].end() || *it2>r)){
cout<<"-1 -1\n";
}else{
if(it!=nesto[v].end() && *it<r)
cout<<*it+1<<" "<<*it+2<<"\n";
else
cout<<*it2+1<<" "<<*it2+1<<"\n";
}
}
}
}
int main(){
ios;
int t=1;
//cin>>t;
while(t--) solve();
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... |