ACM POJ 3321 Apple Tree(树状数组)

Apple Tree

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 11282Accepted: 3214

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    

/**************************************************
POJ 3321 Apple Tree
一棵树上长了苹果,每一个树枝节点上有长苹果和不长苹果两种状态,
两种操作,一种操作能够改变树枝上苹果的状态,
另一种操作询问某一树枝节点一下的所有的苹果有多少。具体做法
是做一次dfs,记下每个节点的开始时间Start[i]和结束时间End[i],
那么对于i节点的所有子孙的开始时间和结束时间都应位于Start[i]
和End[i]之间,另外用一个数组C[i]记录附加在节点i上的苹果的个数,
然后用树状数组统计Start[i]到End[i]之间的附加苹果总数。这里
用树状数组统计区间可以用Sum(Start[i])-Sum(End[i]-1)来计算。

**************************************************
*/
#include
<iostream>
#include
<vector>
#include
<stdio.h>
using namespace std;
#define MAXN 220000
int C[MAXN];
typedef vector
<int> VCT_INT;
vector
<VCT_INT>G(MAXN/2);
int Lowbit[MAXN];
int Start[MAXN];//dfs的开始时间
int End[MAXN];//dfs的结束时间
bool HasApple[MAXN/2];
int nCount;
void Dfs(int v)
{
Start[v]
=++nCount;
for(int i=0;i<G[v].size();i++)
Dfs(G[v][i]);
End[v]
=++nCount;
}
int QuerySum(int p)
{
int nSum=0;
while(p>0)
{
nSum
+=C[p];
p
-=Lowbit[p];
}
return nSum;
}
void Modify(int p,int val)
{
while(p<=nCount)
{
C[p]
+=val;
p
+=Lowbit[p];
}
}
int main()
{
int n;
scanf(
"%d",&n);
int x,y;
int i,j,k;
//建图
for(i=0;i<n-1;i++)
{
int a,b;
scanf(
"%d%d",&a,&b);
G[a].push_back(b);
}
nCount
=0;
Dfs(
1);
//树状数组要处理的原始数组下标范围1--nCount
for(i=1;i<=nCount;i++)
{
Lowbit[i]
=i&(i^(i-1));
}
for(i=1;i<=n;i++)
HasApple[i]
=1;
int m;
//求C数组
for(i=1;i<=nCount;i++)
C[i]
=i-(i-Lowbit[i]+1)+1;
scanf(
"%d",&m);
for(i=0;i<m;i++)
{
char cmd[10];
int a;
scanf(
"%s%d",cmd,&a);
if(cmd[0]=='C')
{
if(HasApple[a])
{
Modify(Start[a],
-1);
Modify(End[a],
-1);
HasApple[a]
=0;
}
else
{
Modify(Start[a],
1);
Modify(End[a],
1);
HasApple[a]
=1;
}
}
else
{
int t1=QuerySum(End[a]);
int t2=QuerySum(Start[a]);
printf(
"%d\n",(t1-t2)/2+HasApple[a]);
}
}
return 0;

}

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2011/08/16/2141607.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞