程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 3321 Apple Tree (樹狀數組)

POJ 3321 Apple Tree (樹狀數組)

編輯:關於C++

 

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 
#include 
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);
        }
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved