POJ 3321 Apple Tree(树状数组)

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21635 Accepted: 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
"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
"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[N],c[N];
int begin[N],end[N];
bool v[N],du[N];

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值