coddforces 343d

http://www.elijahqi.win/archives/1164
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
一眼看去树链剖分
但是滕老师还是坚持让我们想一想dfs+线段树是怎么搞的

确实能搞

树链剖分留坑待填吧。。

深搜序使得操作子树变成连续的一段区间进行操作

但是 针对操作2 我们怎么办呢

暴力修改要是链的话复杂无法接受啊

于是不妨我们给要修改的那个点打标记 要是我下一次询问答案的时候发现我的子树中有被打过标记的,那我一定是0 跑不了

不妨我们把这个标记和灌水的标记合二为一吧

我每次灌水的时候都要先检查下我子树中是否包含0 要是包含0的话就把我的父亲进行改0的操作

在修改0的时候不能忘记把我此前改1 的懒标记进行下放,因为我始终只会改子树的其中之一

顺便update一下,使我当前标记变成0表示我这一路径底下存在0 也就是查询我的时候返回值应该是0 在查询的时候同时也要先下放才可以

在染色1操作时,我可能存在左子树为0右子树为1 我为0的情况 这时把左子树染成1 同时也需要把我自己更新为1才行

#include<cstdio>
#include<algorithm>
#define N 550000
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct node{
    int l,r,left,right;bool flag;
}tree[N<<2];
struct node1{
    int y,next;
}data[N<<1];
int n,m,h[N],in[N],out[N],fa[N],root,num;
void dfs(int x){
    in[x]=++num;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (fa[x]==y) continue;fa[y]=x;
        dfs(y);
    }out[x]=num;
}
void build(int &x,int l,int r){
    x=++num;tree[x].l=l;tree[x].r=r;
    if (l==r) return;
    int mid=l+r>>1;
    build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
}
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].flag=tree[l].flag&tree[r].flag;
}
inline void insert1(int x,int l,int r){
    if (l<=tree[x].l&&r>=tree[x].r){tree[x].flag=1;return;}
    int mid=tree[x].l+tree[x].r>>1;
    if (l<=mid) insert1(tree[x].left,l,r);
    if (r>mid) insert1(tree[x].right,l,r);
    update(x);  
}
inline void pushdown(int x){
    if (!tree[x].flag) return;
    int l=tree[x].left,r=tree[x].right;
    tree[l].flag=true;tree[r].flag=true;
}
void insert2(int x,int l){
    if (tree[x].l==tree[x].r){tree[x].flag=0;return;}
    int mid=tree[x].l+tree[x].r>>1;pushdown(x);
    if (l<=mid) insert2(tree[x].left,l);else insert2(tree[x].right,l);
    update(x);
}
int query(int x,int l,int r){
    if (l<=tree[x].l&&r>=tree[x].r) return tree[x].flag;
    int mid=tree[x].l+tree[x].r>>1;int tmp=1;pushdown(x);
    if (l<=mid) tmp&=query(tree[x].left,l,r);
    if (r>mid) tmp&=query(tree[x].right,l,r);
    return tmp;
}
int main(){
//  freopen("cf.in","r",stdin);
    n=read();
    for (int i=1;i<n;++i){
        int x=read(),y=read();
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }num=0;
    dfs(1);m=read();num=0;build(root,1,n);
    for (int i=1;i<=m;++i){
        int op=read(),x=read();
        if (op==1){
            int tmp=query(root,in[x],out[x]);insert1(root,in[x],out[x]);
            if (!tmp&&x!=1) insert2(root,in[fa[x]]);
        }
        if (op==2)insert2(root,in[x]);
        if (op==3) printf("%d\n",query(root,in[x],out[x]));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值