D. Water Tree
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.
The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.
Mike wants to do the following operations with the tree:
Fill vertex v with water. Then v and all its children are filled with water.
Empty vertex v. Then v and all its ancestors are emptied.
Determine whether vertex v is filled with water at the moment.
Initially all vertices of the tree are empty.
Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the edges of the tree.
The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.
It is guaranteed that the given graph is a tree.
Output
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
sample input
5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5
output
0
0
0
1
0
1
0
1
题意:给一棵树
1、把v点及其子树灌上水
2、把v点及v到根的路径去掉水
3、询问v点是否有水
思路:
1.dfs建树,可处理区间;
2.线段树灌水(区间更新)+lazy处理;
3.去水单点更新;
注意:
1.有水条件 (push—up);
2.灌水如果区间中有空的————父亲节点必为空(判断+单点更新)!!!!
上代码
#include<iostream>
#include<vector>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
int n;
int in[550000];
int out[550000];
int tree[550000*8];
int pos[550000];
int lazy[550000*8];
int num=0;
vector<int> lin[550000];
bool flag=false;
void dfs(int x,int pre)
{
in[x]=++num;
for(int i=0;i<lin[x].size();i++)
{
int v=lin[x][i];
if(v!=pre)
{
pos[v]=x;
dfs(v,x);
}
}
out[x]=num;
}
void push_down(int id,int l,int mid,int r)
{
if(lazy[id]==0) return ;
tree[lson]=tree[rson]=lazy[lson]=lazy[rson]=1;
lazy[id]=0;
}
void push_up(int id)
{
if(tree[lson]+tree[rson]==2) tree[id]=1;
else tree[id]=0;
}
void add(int id,int l,int r,int L,int R,int V)
{
if(l==0) return ;
if(l>R||r<L) return ;
if(l>=L&&r<=R)
{
if(tree[id]==0) flag=1;
tree[id]=V;
if(V==1) lazy[id]=V;
return ;
}
int mid=(l+r)/2;
push_down(id,l,mid,r);
add(lson,l,mid,L,R,V);
add(rson,mid+1,r,L,R,V);
push_up(id);
}
void query(int id,int l,int r,int L,int R)
{
if(l>R||r<L) return ;
if(l>=L&&r<=R)
{
if(tree[id]==0) flag=1;
return ;
}
int mid=(l+r)>>1;
push_down(id,l,mid,l);
query(lson,l,mid,L,R);
query(rson,mid+1,r,L,R);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
lin[aa].push_back(bb);
lin[bb].push_back(aa);
}
pos[1]=0;
dfs(1,0);
int Q;
scanf("%d",&Q);
for(int i=1;i<=Q;i++)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
if(aa==1)
{
flag=false;//2.灌水如果区间中有空的————父亲节点空!
add(1,1,n,in[bb],out[bb],1);
if(flag==true)
{
add(1,1,n,in[pos[bb]],in[pos[bb]],0);
}
}
else
if(aa==2)
{
add(1,1,n,in[bb],in[bb],0);
}
if(aa==3)
{
flag=false;
query(1,1,n,in[bb],out[bb]);
if(flag==true) printf("0\n");
else printf("1\n");
}
}
}