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.
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
算是树链剖分的模板题,在线段树查询和更新的时候,可以选择优化一下,不知道不优化能不能过。第一次自己一次过这种题,吼吼吼(菜鸡的悲哀)
代码如下:
#include
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxx=5e5+100;
int head[maxx<<1],pre[maxx],id[maxx];
int size[maxx],son[maxx],fa[maxx];
int dep[maxx],top[maxx];
struct edge{
int to;
int next;
}e[maxx<<1];
struct node{
int l;
int r;
int lazy;
int sum;
}p[maxx*4];
int n,m,tot,sign;
/*------------准备阶段--------------*/
void init()
{
memset(head,-1,sizeof(head));
tot=sign=0;
}
void add(int u,int v)
{
e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
/*--------------dfs-----------------*/
void dfs1(int u,int f)
{
dep[u]=dep[f]+1;
fa[u]=f;
size[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(to==f) continue;
dfs1(to,u);
size[u]+=size[to];
if(size[to]>size[son[u]]) son[u]=to;
}
}
void dfs2(int u,int Top)
{
id[u]=++sign;
pre[sign]=u;
top[u]=Top;
if(son[u]) dfs2(son[u],Top);
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(to==son[u]||to==fa[u]) continue;
dfs2(to,to);
}
}
/*--------------线段树--------------*/
void pushup(int cur){p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;}
void pushdown(int cur)
{
if(p[cur].lazy>0)
{
p[cur<<1].sum=p[cur<<1].r-p[cur<<1].l+1;
p[cur<<1|1].sum=p[cur<<1|1].r-p[cur<<1|1].l+1;
p[cur<<1].lazy=1;
p[cur<<1|1].lazy=1;
p[cur].lazy=0;
}
else if(p[cur].lazy<0)
{
p[cur<<1].sum=p[cur<<1|1].sum=0;
p[cur<<1].lazy=p[cur<<1|1].lazy=-1;
p[cur].lazy=0;
}
}
void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur].sum=0;
if(l==r) return ;
int mid=(l+r)/2;
build(l,mid,2*cur);
build(mid+1,r,2*cur+1);
pushup(cur);
}
void update(int l,int r,int cur,int add)
{
if(p[cur].sum==p[cur].r-p[cur].l+1&&add==1) return ;
if(p[cur].sum==0&&add==-1) return ;//这两句算是一个优化,如果子树已经是满的或者是空的,就不需要更新了
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r)
{
if(add>0)
{
p[cur].sum=(R-L+1);
p[cur].lazy=1;
}
else
{
p[cur].sum=0;
p[cur].lazy=-1;
}
return ;
}
pushdown(cur);
int mid=(L+R)/2;
if(r<=mid) update(l,r,2*cur,add);
else if(l>mid) update(l,r,2*cur+1,add);
else
{
update(l,mid,2*cur,add);
update(mid+1,r,2*cur+1,add);
}
pushup(cur);
}
int query(int x,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(p[cur].sum==R-L+1) return 1;//小优化
if(L==R)
{
if(p[cur].sum) return 1;
else return 0;
}
pushdown(cur);
int mid=(L+R)/2;
if(x<=mid) return query(x,2*cur);
else return query(x,2*cur+1);
}
/*------------树链剖分-------------*/
void solve(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]id[y]) swap(x,y);
update(id[x],id[y],1,-1);
}
int main()
{
init();
scanf("%d",&n);
int x,y;
for(int i=1;i
努力加油a啊,(o)/~