#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
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;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5340 KB |
Output is correct |
2 |
Correct |
7 ms |
5388 KB |
Output is correct |
3 |
Correct |
7 ms |
5464 KB |
Output is correct |
4 |
Correct |
8 ms |
5464 KB |
Output is correct |
5 |
Correct |
7 ms |
5464 KB |
Output is correct |
6 |
Correct |
8 ms |
5476 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5520 KB |
Output is correct |
9 |
Correct |
7 ms |
5696 KB |
Output is correct |
10 |
Correct |
7 ms |
5696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5340 KB |
Output is correct |
2 |
Correct |
7 ms |
5388 KB |
Output is correct |
3 |
Correct |
7 ms |
5464 KB |
Output is correct |
4 |
Correct |
8 ms |
5464 KB |
Output is correct |
5 |
Correct |
7 ms |
5464 KB |
Output is correct |
6 |
Correct |
8 ms |
5476 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5520 KB |
Output is correct |
9 |
Correct |
7 ms |
5696 KB |
Output is correct |
10 |
Correct |
7 ms |
5696 KB |
Output is correct |
11 |
Correct |
7 ms |
5696 KB |
Output is correct |
12 |
Correct |
7 ms |
5696 KB |
Output is correct |
13 |
Correct |
7 ms |
5696 KB |
Output is correct |
14 |
Correct |
7 ms |
5708 KB |
Output is correct |
15 |
Correct |
7 ms |
5724 KB |
Output is correct |
16 |
Correct |
8 ms |
5740 KB |
Output is correct |
17 |
Correct |
7 ms |
5756 KB |
Output is correct |
18 |
Correct |
7 ms |
5772 KB |
Output is correct |
19 |
Correct |
7 ms |
5788 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
7 ms |
5908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5340 KB |
Output is correct |
2 |
Correct |
7 ms |
5388 KB |
Output is correct |
3 |
Correct |
7 ms |
5464 KB |
Output is correct |
4 |
Correct |
8 ms |
5464 KB |
Output is correct |
5 |
Correct |
7 ms |
5464 KB |
Output is correct |
6 |
Correct |
8 ms |
5476 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5520 KB |
Output is correct |
9 |
Correct |
7 ms |
5696 KB |
Output is correct |
10 |
Correct |
7 ms |
5696 KB |
Output is correct |
11 |
Correct |
7 ms |
5696 KB |
Output is correct |
12 |
Correct |
7 ms |
5696 KB |
Output is correct |
13 |
Correct |
7 ms |
5696 KB |
Output is correct |
14 |
Correct |
7 ms |
5708 KB |
Output is correct |
15 |
Correct |
7 ms |
5724 KB |
Output is correct |
16 |
Correct |
8 ms |
5740 KB |
Output is correct |
17 |
Correct |
7 ms |
5756 KB |
Output is correct |
18 |
Correct |
7 ms |
5772 KB |
Output is correct |
19 |
Correct |
7 ms |
5788 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
7 ms |
5908 KB |
Output is correct |
22 |
Correct |
188 ms |
6860 KB |
Output is correct |
23 |
Correct |
192 ms |
6892 KB |
Output is correct |
24 |
Correct |
189 ms |
6908 KB |
Output is correct |
25 |
Correct |
194 ms |
6916 KB |
Output is correct |
26 |
Correct |
199 ms |
6956 KB |
Output is correct |
27 |
Correct |
177 ms |
7068 KB |
Output is correct |
28 |
Correct |
175 ms |
7068 KB |
Output is correct |
29 |
Correct |
196 ms |
7072 KB |
Output is correct |
30 |
Correct |
161 ms |
7072 KB |
Output is correct |
31 |
Correct |
169 ms |
7112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5340 KB |
Output is correct |
2 |
Correct |
7 ms |
5388 KB |
Output is correct |
3 |
Correct |
7 ms |
5464 KB |
Output is correct |
4 |
Correct |
8 ms |
5464 KB |
Output is correct |
5 |
Correct |
7 ms |
5464 KB |
Output is correct |
6 |
Correct |
8 ms |
5476 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5520 KB |
Output is correct |
9 |
Correct |
7 ms |
5696 KB |
Output is correct |
10 |
Correct |
7 ms |
5696 KB |
Output is correct |
11 |
Correct |
439 ms |
29452 KB |
Output is correct |
12 |
Correct |
419 ms |
29600 KB |
Output is correct |
13 |
Correct |
467 ms |
29600 KB |
Output is correct |
14 |
Correct |
484 ms |
29692 KB |
Output is correct |
15 |
Correct |
423 ms |
29692 KB |
Output is correct |
16 |
Correct |
411 ms |
29692 KB |
Output is correct |
17 |
Correct |
429 ms |
29692 KB |
Output is correct |
18 |
Correct |
445 ms |
29692 KB |
Output is correct |
19 |
Correct |
380 ms |
29692 KB |
Output is correct |
20 |
Correct |
396 ms |
29692 KB |
Output is correct |
21 |
Correct |
367 ms |
29692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5340 KB |
Output is correct |
2 |
Correct |
7 ms |
5388 KB |
Output is correct |
3 |
Correct |
7 ms |
5464 KB |
Output is correct |
4 |
Correct |
8 ms |
5464 KB |
Output is correct |
5 |
Correct |
7 ms |
5464 KB |
Output is correct |
6 |
Correct |
8 ms |
5476 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
7 ms |
5520 KB |
Output is correct |
9 |
Correct |
7 ms |
5696 KB |
Output is correct |
10 |
Correct |
7 ms |
5696 KB |
Output is correct |
11 |
Correct |
7 ms |
5696 KB |
Output is correct |
12 |
Correct |
7 ms |
5696 KB |
Output is correct |
13 |
Correct |
7 ms |
5696 KB |
Output is correct |
14 |
Correct |
7 ms |
5708 KB |
Output is correct |
15 |
Correct |
7 ms |
5724 KB |
Output is correct |
16 |
Correct |
8 ms |
5740 KB |
Output is correct |
17 |
Correct |
7 ms |
5756 KB |
Output is correct |
18 |
Correct |
7 ms |
5772 KB |
Output is correct |
19 |
Correct |
7 ms |
5788 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
7 ms |
5908 KB |
Output is correct |
22 |
Correct |
188 ms |
6860 KB |
Output is correct |
23 |
Correct |
192 ms |
6892 KB |
Output is correct |
24 |
Correct |
189 ms |
6908 KB |
Output is correct |
25 |
Correct |
194 ms |
6916 KB |
Output is correct |
26 |
Correct |
199 ms |
6956 KB |
Output is correct |
27 |
Correct |
177 ms |
7068 KB |
Output is correct |
28 |
Correct |
175 ms |
7068 KB |
Output is correct |
29 |
Correct |
196 ms |
7072 KB |
Output is correct |
30 |
Correct |
161 ms |
7072 KB |
Output is correct |
31 |
Correct |
169 ms |
7112 KB |
Output is correct |
32 |
Correct |
439 ms |
29452 KB |
Output is correct |
33 |
Correct |
419 ms |
29600 KB |
Output is correct |
34 |
Correct |
467 ms |
29600 KB |
Output is correct |
35 |
Correct |
484 ms |
29692 KB |
Output is correct |
36 |
Correct |
423 ms |
29692 KB |
Output is correct |
37 |
Correct |
411 ms |
29692 KB |
Output is correct |
38 |
Correct |
429 ms |
29692 KB |
Output is correct |
39 |
Correct |
445 ms |
29692 KB |
Output is correct |
40 |
Correct |
380 ms |
29692 KB |
Output is correct |
41 |
Correct |
396 ms |
29692 KB |
Output is correct |
42 |
Correct |
367 ms |
29692 KB |
Output is correct |
43 |
Correct |
317 ms |
29692 KB |
Output is correct |
44 |
Correct |
335 ms |
29692 KB |
Output is correct |
45 |
Correct |
363 ms |
29692 KB |
Output is correct |
46 |
Correct |
313 ms |
29692 KB |
Output is correct |
47 |
Correct |
344 ms |
29692 KB |
Output is correct |
48 |
Correct |
254 ms |
29692 KB |
Output is correct |
49 |
Correct |
231 ms |
29692 KB |
Output is correct |
50 |
Correct |
243 ms |
29692 KB |
Output is correct |
51 |
Correct |
435 ms |
38244 KB |
Output is correct |