# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
422237 | Runtime_error_ | Birthday gift (IZhO18_treearray) | C++14 | 1822 ms | 83372 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 pb push_back
#define pi pair<int,int>
using namespace std;
const int inf = 2e5+9,lg = 19;
int n,m,q,a[inf],par[lg][inf],level[inf];
vector<int> adj[inf];
set<int> ans[inf],idx[inf];
void dfs(int u,int p){
level[u] = level[p]+1;
par[0][u] = p;
for(int j=1;j<lg;j++)
par[j][u] = par[j-1][ par[j-1][u] ];
for(auto v:adj[u])
if(v != p)
dfs(v,u);
}
int LCA(int u,int v){
if(level[v] < level[u])
swap(u,v);
int d = level[v] - level[u];
for(int j=lg-1;j>=0;j--)
if(d & (1<<j))
v = par[j][v];
if(u == v)return u;
for(int j=lg-1;j>=0;j--)
if(par[j][v] != par[j][u])
v = par[j][v],u = par[j][u];
return (u == v?v:par[0][v]);
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,0);
for(int i=1;i<=m;i++){
scanf("%d",a+i);
if(i>1)
ans[ LCA(a[i-1],a[i]) ].insert(i-1);
idx[a[i]].insert(i);
}
while(q--){
int type,i,v,l,r;
scanf("%d",&type);
if(type == 1){
scanf("%d%d",&i,&v);
if(i<m){
ans[ LCA(a[i],a[i+1]) ].erase(i);
ans[ LCA(v,a[i+1]) ].insert(i);
}
if(i>1){
ans[ LCA(a[i-1],a[i]) ].erase(i-1);
ans[ LCA(a[i-1],v) ].insert(i-1);
}
idx[a[i]].erase(i);
a[i] = v;
idx[a[i]].insert(i);
}
else {
scanf("%d%d%d",&l,&r,&v);
set<int> ::iterator it;
if(idx[v].size() > 0 && *idx[v].rbegin() >= l){
it = idx[v].lower_bound(l);
if(*it <= r){
printf("%d %d\n",*it,*it);
continue;
}
}
if(ans[v].size() >0 && *ans[v].rbegin() >=l){
it = ans[v].lower_bound(l);
if(*it < r){
printf("%d %d\n",*it,*it+1);
continue;
}
}
puts("-1 -1");
}
}
}
/*
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
*/
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... |