Codeforces 343D Water Tree(线段树+DFS序)*

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:

  1. Fill vertex v with water. Then v and all its children are filled with water.
  2. Empty vertex v. Then v and all its ancestors are emptied.
  3. 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<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)

#define root l,r,rt
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int  maxn =6e5+5;
/*
题目大意:给定一颗树要求在树上维护两种操作,
一种是删除,删除的同时应该将其连同祖先节点一起删除,
一种是灌水,灌水的时候其所有子节点均分有水。
然后是查询,看子节点是否有水。

这道题很明显要用线段树维护DFS序来处理,
其中灌水操作用区间赋值来实现,而删除操作是单点更新,
因为删除的和其子节点没有关系。
上推是用与操作来传递,有一个为零其区间都是零,
下推只推送赋值1的操作。

当然,,这道题不知道为啥哪里有小错误,,
现在还没过,,以后有时间回去再找找。


*/

int n,a,b;
int f[maxn];

///链式前向星
struct node{int u,nxt;};
node edge[maxn];
int head[maxn],tot=0,ti;
void init(){memset(head,-1,sizeof(head));tot=ti=0;}
void add(int x,int y){edge[tot]=node{y,head[x]};head[x]=tot++;}
///DFS序区间建立
int pl[maxn],pr[maxn];
void dfs(int u,int pre)
{
    f[u]=pre;///建立父节点的关系
    pl[u]=++ti;
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].u;
        if(v==pre) continue;
        dfs(v,u);
    }
    pr[u]=ti;
}

int tree[maxn<<2],lazy[maxn<<2];
void pushup(lrt)///往上面推的过程
{
    tree[rt]=(tree[rt<<1]&&tree[rt<<1|1]);
}
void pushdown(lrt)
{
    if(l==r) return ;
    int mid=l+r>>1;
    if(lazy[rt]>0)
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        tree[rt<<1]=tree[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}
void build(lrt)
{
    lazy[rt]=tree[rt]=0;
    if(l==r)return ;
    int mid=l+r>>1;
    build(lson),build(rson),pushup(l,r,rt);
}

void update(lrt,int L,int R,int v)
{
    if(L<=l&&r<=R)
    {
        tree[rt]=lazy[rt]=v;
        return ;
    }
    pushdown(root);
    int mid=l+r>>1;
    if(L<=mid) update(lson,L,R,v);
    if(mid<R) update(rson,L,R,v);
    pushup(root);
}

void Delete(lrt,int pos)
{
    if(l==r) {tree[rt]=0;return ;}
    pushdown(root);
    int mid=l+r>>1;
    if(pos<=mid) Delete(lson,pos);
    if(mid<pos) Delete(rson,pos);
    pushup(root);
}

int query(lrt,int L,int R)
{
    if(L<=l&&r<=R) return tree[rt];
    pushdown(root);
    int mid=l+r>>1,ans=1;
    if(L<=mid) ans=min(ans,query(lson,L,R));///
    if(mid<R) ans=min(ans,query(rson,L,R));
    pushup(root);
    return ans;
}

int main()
{
    init();
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b),add(b,a);
    }

    dfs(1,-1);
    build(1,n,1);
    ///for(int i=1;i<=n;i++) update(pl[1],pr[1],1,pl[i],pr[i],0);

    int q,op,x;
    scanf("%d",&q);
    for(int i=0;i<q;i++)
    {
        scanf("%d%d",&op,&x);
        if(op==1)
        {
            if(!query(1,n,1,pl[x],pr[x]) && f[x]!=-1) Delete(1,n,1,pl[x]);
            update(1,n,1,pl[x],pr[x],1);
        }
        else if(op==2)  Delete(1,n,1,pl[x]);///区别是不用下推标记,下推标记在题目的逻辑中会出错
        else if(op==3) printf("%d\n",query(1,n,1,pl[x],pr[x]));
        ///判断一个节点是否为满则用其查询到的长度是否为区间长度判定
       /// for(int i=1;i<=n;i++) query(1,n,1,pl[i],pr[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81742420