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<iostream>
#include<vector>
using namespace std;
const int MAXN=2e5+10;
const int n=524288;
int in[MAXN],out[MAXN],dp[MAXN];
int ecnt;
vector<int>adj[MAXN];
void ET(const int&X,int dep)
{
in[X]=++ecnt;
dp[X]=dep++;
for(const int&i:adj[X])
ET(i,dep);
out[X]=++ecnt;
return;
}
inline int mn(const int&x,const int&y)
{
return dp[x]>dp[y]?x:y;
}
int ST[n*2+10],lz[n*2+10];
int query(const int&N,const int&i,const int&x,const int&y)
{
if(x==y)
return mn(ST[N],lz[N]);
int l=N<<1;
int r=l+1;
if(dp[ST[N]]<dp[lz[N]])
{
ST[N]=lz[N];
lz[l]=mn(lz[l],lz[N]);
lz[r]=mn(lz[r],lz[N]);
}
int mid=x+y>>1;
if(i>mid)
return query(r,i,mid+1,y);
return query(l,i,x,mid);
}
void lazy(const int&N,const int&i,const int&j,const int&x,const int&y,const int&X)
{
if(j<x||y<i)
return;
if(i<=x&&y<=j)
{
lz[N]=mn(X,lz[N]);
return;
}
int l=N<<1;
int r=l+1;
if(dp[ST[N]]<dp[lz[N]])
{
ST[N]=lz[N];
lz[l]=mn(lz[l],lz[N]);
lz[r]=mn(lz[r],lz[N]);
}
int mid=x+y>>1;
lazy(l,i,j,x,mid,X);
lazy(r,i,j,mid+1,y,X);
return;
}
inline int MC(const int&X)
{
return query(1,in[X],0,n-1);
}
inline void cut(const int&X)
{
lazy(1,in[X],out[X],0,n-1,X);
return;
}
int N,Q,i;
bool flag;
int A,B,C;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>N>>Q;
for(i=1;i<N;i++)
{
cin>>A;
adj[A-1].push_back(i);
}
ET(0,0);
for(i=0;i<N;i++)
adj[i].clear();
while(Q-->0)
{
cin>>A>>B>>C;
flag=MC(--A)==MC(--B);
cout<<(flag?"YES":"NO")<<'\n';
if(C==1)
cut(flag?A:B);
}
cout<<endl;
return 0;
}
Compilation message (stderr)
tree.cpp: In function 'int query(const int&, const int&, const int&, const int&)':
tree.cpp:35:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=x+y>>1;
~^~
tree.cpp: In function 'void lazy(const int&, const int&, const int&, const int&, const int&, const int&)':
tree.cpp:57:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=x+y>>1;
~^~
# | 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... |