# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
334834 | juggernaut | Birthday gift (IZhO18_treearray) | C++14 | 18 ms | 23916 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>
using namespace std;
int n,m,q,a[200005],up[200005][18],tin[200005],tout[200005],timer;
vector<int>g[200005];
set<int>lc[200005],ar[200005];
set<int>::iterator it;
void dfs(int v,int p){
tin[v]=++timer;
up[v][0]=p;
for(int i=1;i<18;i++)up[v][i]=up[up[v][i-1]][i-1];
for(int to:g[v])if(to!=p)dfs(to,v);
tout[v]=++timer;
}
bool upper(int a,int b){
return tin[a]<=tin[b]&&tout[a]>=tout[b];
}
int lca(int a,int b){
if(upper(a,b))return a;
if(upper(b,a))return b;
for(int i=17;i>=0;i--)
if(!upper(up[a][i],b))a=up[a][i];
return up[a][0];
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,1);
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
ar[a[i]].insert(i);
}
for(int i=1;i<m;i++)
lc[lca(a[i],a[i+1])].insert(i);
while(q--){
int type;
scanf("%d",&type);
if(type&1){
int pos,val;
scanf("%d%d",&pos,&val);
ar[a[pos]].erase(pos);
if(pos>1)
lc[lca(a[pos],a[pos-1])].erase(pos-1);
if(pos<m)
lc[lca(a[pos],a[pos+1])].erase(pos);
a[pos]=val;
if(pos>1)
lc[lca(a[pos],a[pos-1])].insert(pos-1);
if(pos<m)
lc[lca(a[pos],a[pos+1])].insert(pos);
ar[a[pos]].insert(pos);
}else{
int l,r,v,al=-1,arr=-1;
scanf("%d%d%d",&l,&r,&v);
//Case al=arr
it=ar[v].lower_bound(l);
if(it!=ar[v].end())
if(*it<=r&&*it>=l)al=arr=*it;
//Case al+1=arr
it=lc[v].lower_bound(l);
if(it!=lc[v].end())
if(*it<=r&&*it>=l)al=*it,arr=al+1;
printf("%d %d\n",al,arr);
}
}
}
Compilation message (stderr)
# | 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... |