题意:题意:给你一颗以点1为根的数,有两种操作,一种是把x及其子树的所有点都灌满水,一种是把x及其所有祖先都放空水,一种是询问,问某个点里有没有水?
思路:线段树维护最晚浇水时间和最晚清空时间。
#include <bits/stdc++.h>
#define lc l,mid,x<<1
#define rc mid+1,r,x<<1|1
#define ls x<<1
#define rs x<<1|1
using namespace std;
const int maxn = 500005;
const int maxm = 1000005;
int ver[maxm],he[maxn],ne[maxm],tot;
void add( int x,int y ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
}
int num = 0,st[maxn],ed[maxn];
void dfs( int x,int f ){
st[x] = ++num;
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure];
if( y == f ) continue;
dfs( y,x );
}
ed[x] = num;
}
int tree[2][4*maxn],laz[2][4*maxn];
void push_up(int x){
tree[0][x] = max( tree[0][ls],tree[0][rs] );
tree[1][x] = max( tree[1][ls],tree[0][rs] );
}
void push_down( int x ){
if( laz[0][x] ){
tree[0][ls] = tree[0][rs] = laz[0][x];
laz[0][ls] = laz[0][rs] = laz[0][x];
laz[0][x] = 0;
}
if( laz[1][x] ){
tree[1][ls] = tree[1][rs] = laz[1][x];
laz[1][ls] = laz[1][rs] = laz[1][x];
laz[1][x] = 0;
}
}
void update( int left,int right,int v,int p,int l,int r,int x ){
if( left <= l && right >= r ){
tree[p][x] = laz[p][x] = v;
return;
}
push_down(x);
int mid = l+r>>1;
if( left <= mid ) update( left,right,v,p,lc );
if( right > mid ) update( left,right,v,p,rc );
push_up(x);
}
int query( int left,int right,int p,int l,int r,int x ){
if( left <= l && right >= r ){
return tree[p][x];
}
push_down(x);
int mid = l+r>>1;
int res = 0;
if( left <= mid ) res = max( res,query( left,right,p,lc ) );
if( right > mid ) res = max( res,query( left,right,p,rc ) );
push_up(x);
return res;
}
int main(){
int n,x,y;
scanf("%d",&n);
for( int i = 1; i < n;i++ ){
scanf("%d%d",&x,&y);
add( x,y );add( y,x );
}
dfs(1,0);
int m;
scanf("%d",&m);
for( int i = 1;i <= m;i++ ){
int op,x;
scanf("%d%d",&op,&x);
if( op == 1 ){
update( st[x],ed[x],i,1,1,n,1 );
}else if( op == 2 ){
update( st[x],st[x],i,0,1,n,1 );
}else{
int v0 = query( st[x],ed[x],0,1,n,1 );
int v1 = query( st[x],st[x],1,1,n,1 );
if( v1 > v0 ){
puts("1");
}else puts("0");
}
}
return 0;
}