POJ-3321 Apple Tree (line segment tree + dfs order)

Description
There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won’t grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
“C x” which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
“Q x” which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output
For every inquiry, output the correspond answer per line.

We directly number the entire tree in dfs sequence, and then build a line segment tree for maintenance.

When querying, the question is xxThe subtree of x is based on itself as the root node and then search downsize [x] size[x]s i z e [ x ] nodes, which is the intervalxxx ~ x + s i z e [ x ] − 1 x + size[x] - 1 x+size[x]1 , pay attention to− 1 -11 because he has to subtract himself. (Because we set up the number in dfs order, the characteristic of dfs order is that each subtree can be divided into continuous intervals. As for the size, we maintain a size
array to indicate the size of the subtree)

When modifying, 0 needs to become 1, and 1 needs to become 0, so we can directly use XOR ^.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;
const int N = 200007, M = 500007, INF = 0x3f3f3f3f;

int n, m;
struct Tree{
    
    
    int l, r;
    int sum;
}tr[N << 2];
int d[N], size[N];
int t;
int ver[M], nex[M], head[N], tot;

void add(int x, int y){
    
    
    ver[tot] = y;
    nex[tot] = head[x];
    head[x] = tot ++ ;
}

int num;

void dfs(int x,  int fa){
    
    
    d[x] = ++ num;
    size[x] = 1;
    for(int i = head[x]; ~i; i = nex[i]){
    
    
        int y = ver[i];
        if(y != fa){
    
    
            dfs(y, x);
            size[x] += size[y];
        }
    }
}

void pushup(int p)
{
    
    
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
}

void build(int p, int l, int r)
{
    
    
    tr[p] = {
    
    l, r};
    if(l == r){
    
    
        tr[p].sum = 1;
        return ;
    }
    int mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

void modify(int p, int x)
{
    
    
    if(tr[p].l == tr[p].r){
    
    
        tr[p].sum ^= 1;
        return ;
    }
    int mid = tr[p].l + tr[p].r >> 1;
    if(x <= mid)modify(p << 1, x);
    else modify(p << 1 | 1, x);
    pushup(p);
}

int query(int p, int l, int r)
{
    
    
    if(tr[p].l >= l && tr[p].r <= r){
    
    
        return tr[p].sum;
    }
    int mid = tr[p].l + tr[p].r >> 1;
    int res = 0;
    if(l <= mid)res += query(p << 1, l, r);
    if(r > mid)res += query(p << 1 | 1, l, r);
    return res;
}

int main()
{
    
    
    while(~scanf("%d", &n)){
    
    
        tot = 0, num = 0;
        memset(head, -1, sizeof head);
        memset(tr, 0, sizeof tr);
        for(int i = 1; i <= n - 1; ++ i){
    
    
            int x, y;
            scanf("%d%d", &x, &y);
            add(x, y);
            add(y, x);
        }
        dfs(1, 0);
        build(1, 1, n);

        scanf("%d", &m);
        for(int i = 1; i <= m; ++ i){
    
    
            char ch[5];
            int x;
            scanf("%s", ch);
            if(ch[0] == 'Q'){
    
    
                scanf("%d", &x);
                printf("%d\n", query(1, d[x], d[x] + size[x] - 1));
            }
            else {
    
    
                scanf("%d", &x);
                modify(1, d[x]);
            }
        }
    }
    return 0;
}


Guess you like

Origin blog.csdn.net/weixin_45697774/article/details/108567678