codeforce-343D---Water Tree (树链剖分)

D. Water Tree
time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard 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:

  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 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.

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


题意:给出n-1条边把n个点连成一棵树,三种操作,1:把点v和他的子数注水;2:把点1~v这条链上所有点的水清空;3:查询节点v的情况;

思路:很明显的树链剖分,对于操作1,dfs后会发现每一个节点和他子树的所有节点在线段树中的位置是一块连续的区域,即更新id[v]到id[v]+sz[v]-1;操作二就是更新1~id[v];

(数组含义看代码);

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 500010
using namespace std;
int n,tot,num,head[maxn];
int son[maxn],dep[maxn],fa[maxn],sz[maxn],id[maxn],top[maxn];
//节点的重儿子,深度,父亲节点,子树的大小,在线段树中的编号,所在链的根节点
struct E
{
    int to;int next;
}edge[maxn*2];
struct node
{
    int l;int r;int tag;//tag为1表示注了水,为-1表示没有水
}segtree[maxn*4];
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void dfs1(int u,int f,int deep)
{
    son[u]=0;
    sz[u]=1;
    dep[u]=deep;
    fa[u]=f;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==f) continue;
        dfs1(ff,u,deep+1);
        sz[u]+=sz[ff];
        if(sz[son[u]]<sz[ff])
            son[u]=ff;
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=++num;
    if(son[u])dfs2(son[u],tp);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==fa[u]||ff==son[u])continue;
        dfs2(ff,ff);
    }
}
void build(int root,int l,int r)
{
    segtree[root].l=l;
    segtree[root].r=r;
    segtree[root].tag=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}
void pushdown(int root)
{
    if(segtree[root].tag!=0)
    {
        segtree[root<<1].tag=segtree[root<<1|1].tag=segtree[root].tag;
        segtree[root].tag=0;
    }
}
void update(int root,int l,int r,int tag)
{
    int ll=segtree[root].l;
    int rr=segtree[root].r;
    if(l<=ll&&rr<=r)
    {
        segtree[root].tag=tag;
        return;
    }
    pushdown(root);
    int mid=(ll+rr)>>1;
    if(l<=mid) update(root<<1,l,r,tag);
    if(r>mid)  update(root<<1|1,l,r,tag);
}
int query(int root,int p)
{
    int ll=segtree[root].l;
    int rr=segtree[root].r;
    if(ll==rr) return segtree[root].tag;
    pushdown(root);
    int mid=(ll+rr)>>1;
    if(p<=mid) query(root<<1,p);
    else query(root<<1|1,p);
}
void Yougth(int u)
{
    int tp=top[u];
    while(tp!=1)
    {
        update(1,id[tp],id[u],-1);
        u=fa[tp];
        tp=top[u];
    }
    update(1,1,id[u],-1);
    return;
}
void init()
{
    tot=num=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        int u,v;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        build(1,1,n);
        int q,x,y;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&x,&y);
            if(x==1)
                update(1,id[y],id[y]+sz[y]-1,1);
            else if(x==2)
                Yougth(y);
            else
                printf("%d\n",query(1,id[y])+1>>1);//因为query可能得到-1,输出时稍微处理一下
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值