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:
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.
Examples
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即可,如果不满足,那么说明这个子树上有空,那么它一定为空。在区间更新时,也进行一次查询,如果发现不满足全1,就先把它的父亲也置0。
代码:
#include
#include
using namespace std;
const int maxn=5e5+7;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
int n;
int L[maxn],R[maxn];
int dfn;
int head[maxn],num;
int f[maxn];
struct Edge
{
int u,v,next;
}edge[maxn<<2];
void addEdge(int u,int v)
{
edge[num].u=u;
edge[num].v=v;
edge[num].next=head[u];
head[u]=num++;
}
void dfs(int u,int pre)
{
L[u]=++dfn;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==pre) continue;
f[v]=u;
dfs(v,u);
}
R[u]=dfn;
}
void init()
{
dfn=0,num=0;
memset(head,-1,sizeof(head));
}
struct Tree
{
int l,r,lazy,sum;
}tree[maxn<<2];
void push_up(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void push_down(int rt)
{
if(tree[rt].lazy!=-1)
{
int tmp=tree[rt].lazy;
tree[rt<<1].sum=tmp*(tree[rt<<1].r-tree[rt<<1].l+1);
tree[rt<<1].lazy=tmp;
tree[rt<<1|1].sum=tmp*(tree[rt<<1|1].r-tree[rt<<1|1].l+1);
tree[rt<<1|1].lazy=tmp;
tree[rt].lazy=-1;
}
}
void Build(int l,int r,int rt)
{
tree[rt].l=l,tree[rt].r=r;
tree[rt].lazy=-1;
tree[rt].sum=0;
if(l==r) return;
int m=(l+r)>>1;
Build(Lson);
Build(Rson);
}
void updata(int L,int R,int f,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
tree[rt].sum=f*(tree[rt].r-tree[rt].l+1);
tree[rt].lazy=f;
return;
}
push_down(rt);
int m=(l+r)>>1;
if(L<=m) updata(L,R,f,Lson);
if(R>m) updata(L,R,f,Rson);
push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return tree[rt].sum;
}
push_down(rt);
int m=(l+r)>>1;
int ans=0;
if(R>m) ans+=query(L,R,Rson);
if(L<=m) ans+=query(L,R,Lson);
push_up(rt);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
init();
for(int i=1,x,y;i