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.
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.
The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the followingn - 1 lines contains two space-separated numbersai,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 followingq lines contains two space-separated numbersci (1 ≤ ci ≤ 3),vi (1 ≤ vi ≤ n), whereci is the operation type (according to the numbering given in the statement), andvi is the vertex on which the operation is performed.
It is guaranteed that the given graph is a tree.
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.
大概意思是:给出一颗n个节点的树,起初每个节点都没有水。有2种操作,1 v :v节点及子孙节点充满水,2 v:v节点及祖先节点均放完水,1种查询 3 v :v节点是否有水?1(有):0(没水)。
思路:操作和查询均有线段树的气息。首先利用DFS按访问先后对节点编号,将树化为线性结构,并记录每个节点及子孙的左右界。对于操作二就非常容易了,简单的段更新,但是操作一区间不连续,该怎么办????经过一阵大脑风暴,发现一个信息:若v点是有水的 当且仅当 此时v及子孙节点都是有水的。每个节点维护两个信息:最迟的抽水时间和最迟的冲水时间 。 最后查询 v点的最迟冲水时间t1 和 v及子孙的 最迟抽水时间t2, t1>t2则无水,反之有水。
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
0 0 0 1 0 1 0 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <stack>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
#define maxn 500010
int mak[3][maxn<<2];
vector<int>v[maxn];
int vis[maxn];
int ll[maxn],rr[maxn];
int tim;
void dfs(int x)
{
vis[x]=1;
ll[x]=++tim;
for(int i=0;i<v[x].size();i++)
if(!vis[v[x][i]])
dfs(v[x][i]);
rr[x]=tim;
}
void pushdown(int rt)
{
if(mak[1][rt])
{
mak[1][rt<<1]=mak[1][rt<<1|1]=mak[1][rt];
mak[1][rt]=0;
}
}
void pushup(int rt)
{
mak[2][rt]=max(mak[2][rt<<1],mak[2][rt<<1|1]);
}
void update(int p, int L, int R, int id,int l, int r, int rt)
{
if(L<=l && R>=r)
{
mak[p][rt]=id;
return;
}
int m=(l+r)>>1;
if(p==1)pushdown(rt);
if(L<=m) update(p,L,R,id,l,m,rt<<1);
if(R>m) update(p,L,R,id,m+1,r,rt<<1|1);
if(p==2)pushup(rt);
}
int query(int p,int L, int R, int l, int r, int rt)
{
if(L<=l && R>=r) return mak[p][rt];
int m=(l+r)>>1;
if(p==1)pushdown(rt);
int mx=-1;
if(L<=m) mx=query(p,L,R,l,m,rt<<1);
if(R>m) mx=max(mx,query(p,L,R,m+1,r,rt<<1|1));
return mx;
}
int main()
{
int i,j,k,m,n;
int a,b;
while(scanf("%d",&n)==1)
{
for(i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b); v[b].push_back(a);
}
tim=0;
memset(vis,0,sizeof(vis));
dfs(1);
scanf("%d",&m);
memset(mak[1],0,sizeof(mak[1]));
memset(mak[2],0,sizeof(mak[2]));
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(a==1) update(1,ll[b],rr[b],i,1,n,1);
else if(a==2) update(2,ll[b],ll[b],i,1,n,1);
else
{
int aa=query(1,ll[b],ll[b],1,n,1);
int bb=query(2,ll[b],rr[b],1,n,1);
printf("%d\n",aa>bb?1:0);
}
}
}
}