Apple Tree

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

题目大意

一个有n个节点的树,树的每个节点可能有一个苹果或没有,有两种操作: C x 将节点x的权值改变,即如果有一个苹果删掉,否则增加一个苹果。 Q x 询问以节点x为根的子树中有多少个苹果。

数据范围:1 <= n <= 100000, 询问个数m <= 100000

题目分析

树的Dfs序 对于棵树进行dfs遍历,并记录每一个点的dfs序号(st[i]),在一个节点的所有儿子都被遍历过后,记录当前c[i]为当前最大dfs序号的节点的dfs序号

将问题转化为了区间操作C x 将某一点的权值改变Q x 询问区间[b[x], c[x]]的和树状数组可以轻松实现这些操作

AC代码

#include<iostream>
#include<cstdio>
using namespace std;
long long n,m,o,x,y,tot,sum,b[100005],c[100005],bj[100005],s[100005],head[100005];
char ch;
struct node//结构体
{
   
	long long to,next;
}a[100005];
void add(long long x,long long y)//邻接表
{
   
	a[++tot]=(node){
   y,head[x]};
	head[x]=tot;
} 
void dfs(long long x)//dfs(求dfs序)
{
   
	o++;
	b[x]=o;
	for(long long i=head[x];i;i=a[i].next)dfs(a[i].to);
	c[x]=o;
}
long long lowbit(long long x){
   return x&(-x);}
void gg(long long x,long long y)//更改
{
   
	for(;x<=n;x+=lowbit(x))s[x]+=y;
}
long long find(long long x)//查询
{
   
	sum=0;
	for(;x;x-=lowbit(x))sum+=s[x];
	return sum;
}
int main()
{
   
	scanf("%lld",&n);//用scanf和printf,免得超时
	for(long long i=1;i<=n-1;i++)
	{
   
		scanf("%lld%lld",&x,&y);
		add(x,y);//建表
	}
	dfs(1);//dfs
	for(long long i=1;i<=n;i++)
	{
   
		bj[i]=1;//标记
		gg(b[i],1);//更改
	}
	scanf("%lld",&m);
	for(long long i=1;i<=m;i++)
	{
   
		cin>>ch;
		if(ch=='Q')
		{
   
			scanf("%lld",&x);
			printf("%lld\n",find(c[x])-find(b[x]-1));//输出答案
		}
		else
		{
   
			scanf("%lld",&x);
			if(bj[x]==0)
			{
   
				gg(b[x],1);//更改
				bj[x]=1;//标记
			}
			else
			{
   
				gg(b[x],-1);//更改
				bj[x]=0;//标记
			}
		}
	} 
}

谢谢