Apple Tree 有时间戳的树状数组

博客围绕kaka家的苹果树展开,树有N个分叉,苹果长在分叉上。给出树的连接关系,有M条操作信息,包括改变某分叉苹果状态(C x)和查询某分叉子树苹果数量(Q x),初始树满是苹果。
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?

 

 

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\n",(t1-t2)/2+HasApple[a]);
 97         }
 98     }
 99    }
100 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3484874.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值