# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
922892 | Aiperiii | Birthday gift (IZhO18_treearray) | C++14 | 0 ms | 0 KiB |
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 int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=2e5+5;
vector <int> g[N];
int p[N],d[N],a[N],jmp[N][20];
void dfs(int v,int par){
p[v]=par;
for(auto to : g[v]){
if(to!=par){
d[to]=d[v]+1;
dfs(to,v);
}
}
}
int lca(int a,int b){
if(d[a]<d[b])swap(a,b);
for(int i=19;i>=0;i--){
if(d[jmp[a][i]]>=d[b]){
a=jmp[a][i];
}
}
if(a==b)return a;
for(int i=19;i>=0;i--){
if(jmp[a][i]!=jmp[b][i]){
a=jmp[a][i];
b=jmp[b][i];
}
}
return p[a];
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m,q;
cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);g[v].pb(u);
}
dfs(1,1);
for(int i=1;i<=n;i++){
jmp[i][0]=p[i];
}
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
jmp[j][i]=jmp[jmp[j][i-1]][i-1];
}
}
for(int i=1;i<=m;i++)cin>>a[i];
set <int> pairs[n+1],ver[n+1];
for(int i=1;i<m;i++){
pairs[lca(a[i],a[i+1])].insert(i);
ver[a[i]].insert(i);
}
while(q--){
int type,l,r,v,pos;
cin>>type;
if(type==1){
cin>>pos>>v;
ver[a[pos]].erase(pos);
ver[v].insert(pos);
if(pos+1<=m){
pairs[lca(a[pos],a[pos+1])].erase(pos);
pairs[lca(v,a[pos+1])].insert(pos);
}
if(pos-1>=1){
pairs[lca(a[pos-1],a[pos])].erase(pos-1);
pairs[lca(a[paos-1],v)].insert(pos-1);
}
a[pos]=v;
}
else{
cin>>l>>r>>v;
int L=-1,R=-1;
auto it=pairs[v].lower_bound(l);
if(it!=pairs[v].end() && *it<r){
L=*it;R=*it+1;
}
it=ver[v].lower_bound(l);
if(it!=ver[v].end() && *it<=r){
L=*it;R=*it;
}
cout<<L<<" "<<R<<"\n";
}
}
}
/*
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
*/