# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
41675 | IvanC | Birthday gift (IZhO18_treearray) | C++14 | 4048 ms | 6908 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;
typedef pair<int,int> ii;
const int MAXN = 2*1e5 + 10;
vector<int> grafo[MAXN];
int ini[MAXN],dfsNum,leftmost[MAXN],rightmost[MAXN],treearray[MAXN],n,m,q;
ii vetorzao[2*MAXN];
void dfs(int v,int p,int depth){
ini[v] = ++dfsNum;
vetorzao[dfsNum] = ii(depth,v);
for(int u : grafo[v]){
if(u == p) continue;
dfs(u,v,depth+1);
vetorzao[++dfsNum] = ii(depth,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);
grafo[u].push_back(v);
grafo[v].push_back(u);
}
dfs(1,-1,0);
for(int v = 1;v<=n;v++){
int ptr = ini[v];
while(ptr + 1 <= dfsNum && ((vetorzao[ptr+1].first > vetorzao[ini[v]].first) || (vetorzao[ptr+1] == vetorzao[ini[v]]))){
ptr++;
}
rightmost[v] = ptr;
ptr = ini[v];
while(ptr - 1 >= 1 && ((vetorzao[ptr-1].first > vetorzao[ini[v]].first) || (vetorzao[ptr-1] == vetorzao[ini[v]]))){
ptr--;
}
leftmost[v] = ptr;
}
for(int i = 1;i<=m;i++){
scanf("%d",&treearray[i]);
treearray[i] = ini[treearray[i]];
}
for(int pergunta = 1;pergunta<=q;pergunta++){
int operacao;
scanf("%d",&operacao);
if(operacao == 1){
int idx,v;
scanf("%d %d",&idx,&v);
treearray[idx] = ini[v];
}
else{
int l,r,v,flag = 0;
scanf("%d %d %d",&l,&r,&v);
for(int i = l;i<=r && !flag;i++){
int minimo = treearray[i],maximo = treearray[i],lo = treearray[i],hi = treearray[i];
ii ancestral = ii(vetorzao[minimo]);
for(int j = i;j<=r && !flag;j++){
minimo = min(minimo,treearray[j]);
maximo = max(maximo,treearray[j]);
while(hi +1 <= maximo){
ancestral = min(ancestral,vetorzao[hi+1]);
hi++;
}
while(lo - 1 >= minimo){
ancestral = min(ancestral,vetorzao[lo-1]);
lo--;
}
if(ancestral.second == v){
printf("%d %d\n",i,j);
flag = 1;
}
else if(ancestral.first < vetorzao[ini[v]].first){
break;
}
}
}
if(!flag) printf("-1 -1\n");
}
}
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... |