Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11282 Accepted: 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;

}


文章來源:http://www.cnblogs.com/kuangbin/archive/2011/08/16/2141607.html