CodeForces343D.Water Tree(树链剖分)

版权声明:蒟蒻的博文,dalao转载标明出处就好吖 https://blog.csdn.net/jokingcoder/article/details/81702541

D. Water Tree

time limit per test 4 seconds
memory limit per test 256 megabytes

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

Solution

要维护某棵子树和某结点到root的链,就是典型的线段树维护树剖。
在dfs序上跑线段树,期望时间复杂度 O ( q l o g n )

Code

#include <cstdio>
#define N 500010

using namespace std;

struct Node{
    int to, nxt;
}e[N << 1];
int cnt, tot, anc[N], len[N], dad[N], size[N], son[N], lst[N], df[N], si[N << 2];

inline void add(int u, int v) {
    e[++cnt].to = v;
    e[cnt].nxt = lst[u];
    lst[u] = cnt;
}

void dfs1(int x, int fa) {
    size[x] = 1;
    dad[x] = fa;
    for (int i = lst[x]; i; i = e[i].nxt) {
        if (e[i].to == fa) continue;
        dfs1(e[i].to, x);
        size[x] += size[e[i].to];
        if (size[e[i].to] > size[son[x]])
            son[x] = e[i].to;
    }
}

void dfs2(int x, int fa) {
    df[x] = ++tot; 
    if (son[x]) {
        anc[son[x]] = anc[x];
        len[son[x]] = len[x] + 1;
        dfs2(son[x], x);
        for (int i = lst[x]; i; i = e[i].nxt) {
            if (e[i].to == fa || e[i].to == son[x]) continue;
            anc[e[i].to] = e[i].to;
            len[e[i].to] = 1;
            dfs2(e[i].to, x);
        }
    }
}

void pushdown(int rt) {
    int ls = rt << 1, rs = rt << 1 | 1;
    si[ls] = si[rt];
    si[rs] = si[rt];
    si[rt] = -1;
}

void update(int ll, int rr, int l, int r, int id, int va) {
    if (ll <= l && r <= rr) {
        si[id] = va;
        return;
    }
    int mid = (l + r) >> 1, ls = id << 1, rs = id << 1 | 1;
    if (si[id] != -1) pushdown(id);
    if (mid >= ll) update(ll, rr, l, mid, ls, va);
    if (mid + 1 <= rr) update(ll, rr, mid + 1, r, rs, va);
    if (si[ls] == si[rs] && si[ls] != -1) si[id] = si[ls];
    else si[id] = -1;
}

int query(int po, int l, int r, int id) {
    if (si[id] != -1) return si[id];
    int mid = (l + r) >> 1;
    if (si[id] != -1) pushdown(id);
    if (l <= po && po <= mid) return query(po, l, mid, id << 1);
    else return query(po, mid + 1, r, id << 1 | 1);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        add(x, y);
        add(y, x);
    }
    dfs1(1, 0);
    anc[1] = 1;
    len[1] = 1;
    dfs2(1, 0);
    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int c, v;
        scanf("%d%d", &c, &v);
        if (c == 1) {
            update(df[v], df[v] + size[v] - 1, 1, n, 1, 1);
        }
        if (c == 2) {
            for (int now = v; now; now = dad[anc[now]]) {
                update(df[anc[now]], df[anc[now]] + len[now] - 1, 1, n, 1, 0);
            }
        }
        if (c == 3) {
            printf("%d\n", query(df[v], 1, n, 1));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jokingcoder/article/details/81702541