# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
140742 | rzbt | Evacuation plan (IZhO18_plan) | C++14 | 588 ms | 524292 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 mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 200005
using namespace std;
int n,m,q;
vector<int> niz[MAXN];
int predak[MAXN][21];
int dubina[MAXN];
int a[MAXN];
set<int> s[MAXN],sp[MAXN];
void dfs(int t,int o,int h){
predak[t][0]=o;
dubina[t]=h;
for(auto x:niz[t])
if(x!=o)
dfs(x,t,h+1);
}
int lca(int a, int b){
if(dubina[b]>dubina[a])swap(a,b);
for(int j=20;j>=0;j--){
if(dubina[predak[a][j]]>=dubina[b])a=predak[a][j];
}
if(a==b)return a;
for(int j=20;j>=0;j--){
if(predak[a][j]!=predak[b][j]){
a=predak[a][j];
b=predak[b][j];
}
}
return predak[a][0];
}
int main()
{
scanf("%d %d %d", &n, &m, &q);
for(int i=1;i<n;i++){
int t1,t2;
scanf("%d %d", &t1, &t2);
niz[t1].pb(t2);
niz[t2].pb(t1);
}
dfs(1,0,1);
for(int j=1;j<21;j++)
for(int i=1;i<=n;i++)
predak[i][j]=predak[i][predak[i][j-1]];
for(int i=1;i<=m;i++)
scanf("%d",a+i);
for(int i=1;i<m;i++)
s[lca(a[i],a[i+1])].insert(i);
for(int i=1;i<=m;i++)
sp[a[i]].insert(i);
while(q--){
int qt; //FITTER HAPPIER MORE PRODUCTIVE
scanf("%d", &qt);
if(qt==1){
int pos,x;
scanf("%d %d", &pos,&x);
if(pos!=1)s[lca(a[pos],a[pos-1])].erase(pos-1);
if(pos!=m)s[lca(a[pos],a[pos+1])].erase(pos);
sp[a[pos]].erase(pos);
a[pos]=x;
if(pos!=1)s[lca(a[pos],a[pos-1])].insert(pos-1);
if(pos!=m)s[lca(a[pos],a[pos+1])].insert(pos);
sp[a[pos]].insert(pos);
}else if(qt==2){
int l,r,x;
scanf("%d %d %d", &l, &r, &x);
//printf(" %d %d\n",sp[x].size(),s[x].size());
int t1=0,t2=0;
if(!sp[x].empty() && sp[x].lower_bound(l)!=sp[x].end())t1=*(sp[x].lower_bound(l));
if(!s[x].empty() && s[x].lower_bound(l)!=s[x].end())t2=*(s[x].lower_bound(l));
//printf(" %d %d %d %d\n",sp[x].size(),s[x].size(),t1,t2);
if(t1<=r && t1)printf("%d %d\n",t1,t1);
else if(t2<r && t2)printf("%d %d\n",t2,t2+1);
else printf("-1 -1\n");
}
else{
int x,y;
scanf("%d %d", &x, &y);
printf(" %d\n",lca(a[x],a[y]));
}
}
return 0;
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |