CodeForces 343D water tree(树链剖分)

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

 

题意:给出一棵树,定义三种操作

1 将u的子树修改为1

2 将u及其所有祖先修改为0

3 查询树上标号为u的点的值

 

题解:非常裸的树链剖分,连push_up都不用,嗯,就是这样。

代码如下:

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
using namespace std;

struct node
{
    int l,r,sum,lazy;
}tr[2000020];
vector<int> g[500050];
int deep[500050],size[500050],fa[500050],son[500050],id[500050],top[500050],cnt;

void push_down(int root)
{
    int mid=(tr[root].l+tr[root].r)>>1;
    tr[lson].sum=tr[root].lazy*(mid-tr[root].l+1);
    tr[lson].lazy=tr[root].lazy;
    tr[rson].sum=tr[root].lazy*(tr[root].r-mid);
    tr[rson].lazy=tr[root].lazy;
    tr[root].lazy=-1;
}

void build(int root,int l,int r)
{
    if(l==r)
    {
        tr[root].l=l;
        tr[root].r=r;
        tr[root].lazy=-1;
        tr[root].sum=0;
        return ;
    }
    tr[root].l=l;
    tr[root].r=r;
    tr[root].lazy=-1;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
}

void update(int root,int l,int r,int val)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        tr[root].sum=val;
        tr[root].lazy=val;
        return ;
    }
    if(~tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        update(rson,l,r,val);
    }
    else
    {
        if(mid>=r)
        {
            update(lson,l,r,val);
        }
        else
        {
            update(lson,l,mid,val);
            update(rson,mid+1,r,val);
        }
    }
}

int query(int root,int l,int r)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        return tr[root].sum;
    }
    if(~tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        return query(rson,l,r);
    }
    else
    {
        if(mid>=r)
        {
            return query(lson,l,r);
        }
        else
        {
            return query(lson,l,mid)+query(rson,mid+1,r); 
        }
    }
}

void dfs1(int now,int f,int dep)
{
    fa[now]=f;
    size[now]=1;
    deep[now]=dep;
    int maxson=-1;
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f)
        {
            continue;
        }
        dfs1(g[now][i],now,dep+1);
        size[now]+=size[g[now][i]];
        if(size[g[now][i]]>maxson)
        {
            maxson=size[g[now][i]];
            son[now]=g[now][i];
        }
    }
}

void dfs2(int now,int topf)
{
    id[now]=++cnt;
    top[now]=topf;
    if(!son[now])
    {
        return ;
    }
    dfs2(son[now],topf);
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==son[now]||g[now][i]==fa[now])
        {
            continue;
        }
        dfs2(g[now][i],g[now][i]);
    }
}

void sub_update(int x)
{
    update(1,id[x],id[x]+size[x]-1,1);
}

void path_update(int u,int v)
{
    while(top[u]!=top[v])
    {
        if(deep[top[u]]<deep[top[v]])
        {
            swap(u,v);
        }
        update(1,id[top[u]],id[u],0);
        u=fa[top[u]];
    }
    if(deep[u]>deep[v])
    {
        swap(u,v);
    }
    update(1,id[u],id[v],0);
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int from,to;
        scanf("%d%d",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,n);
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int kd,u;
        scanf("%d%d",&kd,&u);
        if(kd==1)
        {
            sub_update(u);
        }
        else
        {
            if(kd==2)
            {
                path_update(1,u);
            }
            else
            {
                printf("%d\n",query(1,id[u],id[u]));
            }
        }
    }
}

 

posted @ 2018-05-07 14:05  Styx-ferryman  阅读(386)  评论(0编辑  收藏  举报