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,treearray[MAXN],n,m,q;
ii vetorzao[2*MAXN],seg[8*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);
}
}
void build(int pos,int left,int right){
if(left == right){
seg[pos] = vetorzao[left];
return;
}
int mid = (left+right)/2;
build(2*pos,left,mid);
build(2*pos+1,mid+1,right);
seg[pos] = min(seg[2*pos],seg[2*pos+1]);
}
ii query(int pos,int left,int right,int i,int j){
if(left >= i && right <= j) return seg[pos];
int mid = (left+right)/2;
if(j <= mid) return query(2*pos,left,mid,i,j);
else if(i >= mid + 1) return query(2*pos+1,mid+1,right,i,j);
else return min(query(2*pos,left,mid,i,j),query(2*pos+1,mid+1,right,i,j));
}
ii func(int l,int r){
return query(1,1,dfsNum,l,r);
}
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);
build(1,1,dfsNum);
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);
deque<ii> minQ,maxQ;
int i = l;
for(int j = l;j<=r && !flag;j++){
ii novo = ii(treearray[j],j);
while(!minQ.empty() && minQ.back() >= novo) minQ.pop_back();
while(!maxQ.empty() && maxQ.back() <= novo) maxQ.pop_back();
minQ.push_back(novo);
maxQ.push_back(novo);
while(!minQ.empty() && !maxQ.empty() && func(minQ.front().first,maxQ.front().first).first < vetorzao[ini[v]].first){
if(minQ.front().second == i) minQ.pop_front();
if(maxQ.front().second == i) maxQ.pop_front();
i++;
}
if(!minQ.empty() && !maxQ.empty() && func(minQ.front().first,maxQ.front().first).second == v ){
printf("%d %d\n",i,j);
flag = 1;
}
}
if(!flag) printf("-1 -1\n");
}
}
return 0;
}
Compilation message (stderr)
treearray.cpp: In function 'int main()':
treearray.cpp:38:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&n,&m,&q);
^
treearray.cpp:41:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&u,&v);
^
treearray.cpp:48:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&treearray[i]);
^
treearray.cpp:53:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&operacao);
^
treearray.cpp:56:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&idx,&v);
^
treearray.cpp:61:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&l,&r,&v);
^
# | 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... |