POJ 3321-Apple Tree(樹狀陣列)

kewlgrl發表於2016-04-09

Apple Tree

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23729   Accepted: 7132

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 toN 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 forku and forkv 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 forkx, 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

題目意思:
是有一棵果樹,書上有n個分叉點,從1~n給各個分叉點編號。蘋果長在分叉點的地方,一個分叉點最多隻能長一個蘋果。求這棵樹上一共有多少個蘋果。
最開始的時候樹上長滿蘋果,即每個分叉點都有一個蘋果。

先輸入n,表示分叉點個數;再輸入u,v兩個分叉點表示這兩個點由樹枝連在一起;
C x,表示x點得蘋果狀態發生變化:若以前有,則吃掉變無;若以前沒有,則長出一個蘋果。
Q x,表示查詢x點這棵子樹上的蘋果數目。

用樹狀陣列,方便統計子樹的權值和。

這個是下面程式中sum函式所求的值。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=100000+2;
struct node1
{
    int next,tail;//第i條邊相連的節點是edge[i].tail
    //連線的下一條邊的序號是edge[i].next
} edge[maxn];//邊表
struct node2
{//以節點i為根的子樹在後根序列中的區間為(apple[i].l,apple[i].r)
    int r,l;
} apple[maxn];//蘋果樹
int s[maxn];//s[i]是節點i相連的第i條邊
int c[maxn];//c樹狀陣列
int a[maxn];//a[i]是後根遍歷中第i個節點的權值
int cnt;//後根遍歷序號
void DFS(int u)//初始u=1
{//從節點u出發,計算每個節點為根的子樹區間(apple[].l,apple[].r)
    int i;
    apple[u].l=cnt;
    for(i=s[u]; i!=-1; i=edge[i].next)
        DFS(edge[i].tail);
    apple[u].r=cnt++;
}
int lowbit(int x)//計算二進位制數x右方的第一位1對應的權
{
    return x&(-x);
}
void change(int x)//從a[x]開始調整一棵子樹
{
    int i;
    if(a[x])//有蘋果,都加一
        for(i=x; i<cnt; i+=lowbit(i))
            c[i]++;
    else//蘋果被吃掉了
        for(i=x; i<cnt; i+=lowbit(i))
            c[i]--;
}
int sum(int x)//計算節點x上的蘋果總數
{
    int i,res=0;
    for(i=x; i>0; i-=lowbit(i))
        res+=c[i];
    return res;
}
int main()
{
    int i,m,n,t1,t2,t;
    char str[3];
    scanf("%d",&n);//樹的節點數
    memset(s,-1,sizeof(s[0])*(n+1));
    memset(c,0,sizeof(c[0])*(n+1));
    memset(apple,0,sizeof(apple[0])*(n+1));
    for(i=0; i<n-1; ++i)
    {
        scanf("%d%d",&t1,&t2);//讀第i條邊(t1,t2)
        edge[i].tail=t2;//第i條邊連線t2
        edge[i].next=s[t1];//其後繼指標指向t1連線的上一條邊
        s[t1]=i;//節點t1相連的邊序號i
    }
    cnt=1;
    DFS(1);//節點1開始DFS,計算每個節點的在後根遍歷的順序值,節點權值設為1
    for(i=1; i<=n; ++i)
    {
        a[i]=1;//長滿蘋果
        change(i);//構造樹狀陣列c
    }
    scanf("%d",&m);
    while(m--)
    {
        scanf("%s%d",str,&t);//命令標誌、節點序號
        if(str[0]=='Q')//輸出節點上的蘋果樹
            printf("%d\n",sum(apple[t].r)-sum(apple[t].l-1));
        else
        {//計算節點上的變化情況
            a[apple[t].r]=(a[apple[t].r]+1)%2;//有無蘋果的轉換
            change(apple[t].r);//更新這棵子樹
        }
    }
    return 0;
}


相關文章