POJ 3321 Apple Tree (树状数组)


Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21191   Accepted: 6436

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.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

Source

POJ Monthly--2007.08.05, Huang, Jinsong

题目链接:http://poj.org/problem?id=3321

题目大意:有棵苹果树。開始每一个结点都有一个苹果,C x表示该点有苹果则吃掉,没有则长出。Q x表示询问x结点上包含x结点的苹果数量

题目分析:将苹果树倒过来形成一个树形结构,由于其后序遍历的结果是一段连续的区间,而且以x为根的子树根为区间的右端点,因此能够通过树状数组来求解此问题,用DFS预处理出每一个结点子树的左右区间。



#include <cstdio>
#include <cstring>
int const MAX = 1e5 + 5;
int head[MAX], c[MAX], a[MAX];
int n, m, cnt, num;

struct EDGE
{
    int to, next;
}e[MAX];

struct NODE
{
    int l, r;
}nd[MAX];

void Add(int x, int y)
{
    e[cnt].to = y;
    e[cnt].next = head[x];
    head[x] = cnt++;
}

void DFS(int now)
{
    nd[now].l = num;
    for(int i = head[now]; i != -1; i = e[i].next)
        DFS(e[i].to);
    nd[now].r = num ++;
}

int lowbit(int x)
{
    return x & (-x);
}

void change(int x)
{
    if(a[x])
    {
        for(int i = x; i < num; i += lowbit(i))
            c[i] ++;
    }
    else
    {
        for(int i = x; i < num; i += lowbit(i))
            c[i] --;
    }
}

int cal(int x)
{
    int res = 0; 
    for(int i = x; i > 0; i -= lowbit(i))
        res += c[i];
    return res;
}

int main()
{
    scanf("%d", &n);
    memset(head, -1, sizeof(head));
    memset(c, 0, sizeof(c));
    cnt = 0;
    for(int i = 0; i < n - 1; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        Add(x, y);
    }
    num = 1;
    DFS(1);
    for(int i = 1; i <= n; i++)
    {
        a[i] = 1;
        change(i);
    }
    scanf("%d", &m);
    while(m --)
    {
        int x;
        char s[2];
        scanf("%s %d", s, &x);
        if(s[0] == 'Q')
            printf("%d
", cal(nd[x].r) - cal(nd[x].l - 1));
        else
        {
            a[nd[x].r] = (a[nd[x].r] + 1) % 2;
            change(nd[x].r);
        }
    }
}


原文地址:https://www.cnblogs.com/zsychanpin/p/6801324.html