Apple Tree 有时间戳的树状数组

Problem 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?

Apple Tree          有时间戳的树状数组
 
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
**********************************************************************************************************************************
最重要的是记录树状数组的时间戳
**********************************************************************************************************************************
  1 /*
  2  有时间戳的树状数组,
  3 */
  4 #include<iostream>
  5 #include<string>
  6 #include<cstring>
  7 #include<cmath>
  8 #include<queue>
  9 #include<cstdio>
 10 #include<map>
 11 #include<algorithm>
 12 using namespace std;
 13 #define  maxn  220000
 14 typedef vector<int> VCT_INT;
 15 vector<VCT_INT>G(maxn/2);
 16 int nCount,n,m;
 17 int Start[maxn],End[maxn];
 18 int C[maxn],lowbit[maxn];
 19 int HasApple[maxn/2];
 20  int query_sum(int p)//求一个区间内的和
 21  {
 22     int sum=0;
 23     while(p>0)
 24     {
 25        sum+=C[p];
 26        p-=lowbit[p];
 27     }
 28     return sum;
 29  }
 30 
 31  void modify(int p,int val)//插入更新
 32  {
 33      while(p<=nCount)
 34      {
 35         C[p]+=val;
 36         p+=lowbit[p];
 37      }
 38  }
 39 
 40 void dfs(int s)//插入时间戳
 41 {
 42    Start[s]=++nCount;
 43    for(int it=0;it<G[s].size();it++)
 44     {
 45        dfs(G[s][it]);
 46     }
 47     End[s]=++nCount;
 48 }
 49 
 50 int main()
 51 {
 52    int i,j,k;
 53    while(scanf("%d",&n)!=EOF)
 54    {
 55       int a,b,c;
 56      for(i=1;i<n;i++)
 57      {
 58         scanf("%d %d",&a,&b);
 59         G[a].push_back(b);
 60      }
 61      nCount=0;
 62      dfs(1);
 63      for(i=1;i<=nCount;i++)
 64       lowbit[i]=i&(i^(i-1));
 65      for(i=1;i<=nCount;i++)//求C[]数组
 66       C[i]=i-(i-lowbit[i]+1)+1;
 67      for(i=1;i<=n;i++)
 68      {
 69        HasApple[i]=1;
 70      }
 71       scanf("%d",&m);
 72     for(i=0;i<m;i++)
 73     {
 74         char cmd[10];
 75         int a;
 76         scanf("%s%d",cmd,&a);
 77         if(cmd[0]=='C')
 78         {
 79             if(HasApple[a])
 80             {
 81                 modify(Start[a],-1);
 82                 modify(End[a],-1);
 83                 HasApple[a]=0;
 84             }
 85             else
 86             {
 87                 modify(Start[a],1);
 88                 modify(End[a],1);
 89                 HasApple[a]=1;
 90             }
 91         }
 92         else
 93         {
 94             int t1=query_sum(End[a]);
 95             int t2=query_sum(Start[a]);
 96             printf("%d
",(t1-t2)/2+HasApple[a]);
 97         }
 98     }
 99    }
100 }
View Code