Codeforces 343D. Water Tree (树链剖分 + 区间染色)

Description
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 a i, b i (1 ≤ a i, b i ≤ n, a i ≠ b i) — 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 c i (1 ≤ c i ≤ 3), v i (1 ≤ v i ≤ n), where c i is the operation type (according to the numbering given in the statement), and v i 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

Solution
树链剖分 + 线段树区间染色

Code

#include <bits/stdc++.h>
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
typedef long long ll;
const int MX = 5e5 + 7;
int n;
int ecnt,head[MX];
struct Edge{
    int v,next;
}e[MX << 1];

void add(int u,int v){
    e[++ecnt].v = v;e[ecnt].next = head[u];head[u] = ecnt;
    e[++ecnt].v = u;e[ecnt].next = head[v];head[v] = ecnt;
}

struct Segtree{
    int l,r,cov,c,lazy;
}t[MX << 3];


void push_up(int rt){
    if(t[ls].cov && t[rs].cov){
        if(t[ls].c == t[rs].c) t[rt].c = t[ls].c, t[rt].cov = 1;
        else t[rt].cov = 0; 
    } else t[rt].cov = 0;
}

void push_down(int rt){
    if(t[rt].lazy != -1){
        int col = t[rt].lazy;
        t[ls].c = col, t[ls].lazy = col, t[ls].cov = 1;
        t[rs].c = col, t[rs].lazy = col, t[rs].cov = 1;
        t[rt].lazy = -1;
    }
}

void build(int rt,int l,int r){
    t[rt].l = l, t[rt].r = r;
    t[rt].c =  0;
    t[rt].lazy = -1;
    t[rt].cov = 0;
    if(l == r) {t[rt].cov = 1;return ;}
    int mid = (l + r) >> 1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    push_up(rt);
}

void update(int rt,int L,int R,int k){
    int l = t[rt].l, r = t[rt].r;
    if(L <= l && r <= R){
        // printf("L = %d R = %d l = %d r = %d\n", L,R,l,r);
        t[rt].c = k;
        t[rt].lazy = k;
        t[rt].cov = 1;
        return ;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(ls,L,R,k);
    if(R  > mid) update(rs,L,R,k);
    push_up(rt);
}

int qres;
void query(int rt,int pos){
    int l = t[rt].l, r = t[rt].r;
    if(l <= pos && pos <= r && t[rt].cov){
        qres = t[rt].c;return ;
    }
    if(l == r) {qres = t[rt].c;return ;}
    push_down(rt);
    int mid = (l + r) >> 1;
    if(pos <= mid) query(ls,pos);
    if(pos > mid) query(rs,pos);
    push_up(rt);
}

int siz[MX], son[MX], top[MX], dep[MX], fa[MX],id[MX],cnt;

void dfs1(int u,int f,int deep){
    siz[u] = 1, dep[u] = deep, fa[u] = f;
    int maxson = -1;
    for(int i = head[u];i;i = e[i].next){
        int v = e[i].v;
        if(v == f) continue;
        dfs1(v,u,deep+1);
        siz[u] += siz[v];
        if(siz[v] > maxson) maxson = siz[v], son[u] = v;
    }
}

void dfs2(int u,int topf){
    top[u] = topf;id[u] = ++cnt;
    if(!son[u]) return ;
    dfs2(son[u],topf);
    for(int i = head[u];i;i = e[i].next){
        int v = e[i].v;
        if(v == fa[u] || v == son[u]) continue;
        dfs2(v,v);
    }
}

int qnode(int u){
    qres = -1;
    query(1,id[u]);
    return qres;
}
void updSon(int u){
    update(1,id[u],id[u] + siz[u] - 1,1);
}


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

int main(){
    scanf("%d",&n);
    for(int i = 1;i < n;++i){
        int u,v;scanf("%d%d",&u,&v);
        add(u,v);
    }
    int q;scanf("%d",&q);
    build(1,1,n);
    dfs1(1,1,1);dfs2(1,1);
    while(q--){
        int op,u;scanf("%d%d",&op,&u);
        if(op == 1){
            updSon(u);
        } else if(op == 2){
            updRange(u);
        } else if(op == 3){
            printf("%d\n", qnode(u));
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值