您的位置:首页 > 移动开发

POJ 3321 Apple Tree(树状数组)

2015-08-17 21:34 344 查看
Apple Tree

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 21635Accepted: 6573
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

题意:给定一棵树,某些节点上有苹果,多次询问各子树上的节点数,并且在

询问的中途随时可能新增和删除苹果。

思路:这个可以使用树状数组来做,不过和一般的树状数组不一样,因为这是

一棵树上进行分叉,所以区间不是按照1-n进行排列的,所以要找到一种方法通过两个点

之间的关系把1-n进行重新排列,存在begin和end中,这样就可以用树状数组了。

不可以用vector做,我用了直接超时,改成边表就AC了......Orz


点击打开链接

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<vector>

#define N 100005

using namespace std;

int n;
int cnt;
int m;
int head
,c
;
int begin
,end
;
bool v
,du
;

struct node{
    int xx;
    int next;
}q[N<<1];

void add(int x,int y){
    q[cnt].xx = y;
    q[cnt].next = head[x];
    head[x] = cnt++;
}

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

int getsum(int x){
    int s = 0;
    while(x>0){
        s += c[x];
        x -= lowbit(x);
    }
    return s;
}

void updata(int x,int t){
    while(x<=n){
        c[x] += t;
        x += lowbit(x);
    }
}

void DFS(int x){
    cnt++;
    begin[x] = cnt;
    v[x] = 1;
    for(int i=head[x];i!=-1;i=q[i].next){
        if(v[q[i].xx] == 0){
            DFS(q[i].xx);
        }
    }
    end[x] = cnt;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        memset(head,-1,sizeof(head));
        memset(v,0,sizeof(v));
        memset(du,1,sizeof(du));
        cnt = 0;
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        cnt = 0;
        DFS(1);
        for(int i=1;i<=n;i++){
            updata(i,1);
        }
        scanf("%d",&m);
        char str[10];
        while(m--){
            scanf("%s%d",str,&x);
            if(str[0] == 'C'){
                if(du[x]){
                    updata(begin[x],-1);
                    du[x] = 0;
                }else{
                    updata(begin[x],1);
                    du[x] = 1;
                }
            }else{
                printf("%d\n",getsum(end[x]) - getsum(begin[x]-1));
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: