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?



poj 3321  Apple Tree   树状数组_#include


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


#include<stdio.h> 
 
 #include<string.h> 
 
 #include<stdlib.h> 
 
 #include<iostream> 
 
 using namespace std; 
 
 #define Size 110011 
 


 struct ele 
 
 { 
 
     int data; 
 
     ele *next; 
 
 }p[Size]; 
 
 int lnc; 
 
 int Begin[Size]; 
 
 int End[Size]; 
 
 int c[Size]; 
 


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


 int sum(int n) 
 
 { 
 
     int num=0; 
 
     while(n>0) 
 
     { 
 
         num+=c[n]; 
 
         n-=lowbit(n); 
 
     } 
 
     return num; 
 
 } 
 


 void Add(int i,int x) 
 
 { 
 
     while(i<Size) 
 
     { 
 
         c[i]+=x; 
 
         i+=lowbit(i); 
 
     } 
 
 } 
 


void dfs(int pos)
 {
     Begin[pos]=++lnc;
     ele *point=p[pos].next;
     while(point)
     {
         if(Begin[point->data]==0)
         {
               dfs(point->data);
         }
         point=point->next;
     }
     End[pos]=lnc;
 } 
 


 int main() 
 
 { 
 
     int n; 
 
     int i; 
 
     int x,y,z; 
 
     int k,t; 
 
     char str; 
 


     while(scanf("%d",&n)!=EOF) 
 
     { 
 
        // printf("******\n"); 
 
         lnc=0; 
 
         memset(Begin,0,sizeof(Begin)); 
 
         memset(End,0,sizeof(End)); 
 
         for(i=1;i<=n-1;i++) 
 
         { 
 
             scanf("%d%d",&x,&y); 
 
             ele *point=(ele*)malloc(sizeof(ele)); 
 
             point->data=y; 
 
             point->next=p[x].next; 
 
             p[x].next=point; 
 


             ele *it=(ele*)malloc(sizeof(ele)); 
 
             it->data=x; 
 
             it->next=p[y].next; 
 
             p[y].next=it; 
 
         } 
 
         dfs(1); 
 
         for(i=1;i<=n;i++) 
 
             Add(i,1); 
 
         scanf("%d",&z); 
 
       //  printf("///\n"); 
 
         while(z--) 
 
         { 
 
             cin>>str; 
 
             scanf("%d",&x); 
 
             if(str=='C') 
 
             { 
 
               
   k=sum(Begin[x]);
                 t=sum(Begin[x]-1);
                 if(k==t)
                     Add(Begin[x],1);
                 else
                     Add(Begin[x],-1); 
 
             } 
 
             else 
 
                 printf("%d\n",sum(End[x])-sum(Begin[x]-1)); 
 
         } 
 
     } 
 


     return 0; 
 
 }