Apple Tree

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 39797 Accepted: 11814
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

知识点:dfs序建树+线段树(或者树状数组)

dfs序建树,人尽皆知sb题

题意:
给一棵n个节点的树,每个节点开始有一个苹果,m次操作
1.将某个结点的苹果数异或 1
2.查询一棵子树内的苹果数

对于第一个操作,就是线段树单点更新,我们每次异或1就行了。
对于第二个操作,就是线段树区间查询。

AC代码:

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e5+10;
int head[N],st[N],ed[N],tot,cnt,n,m,vis[N];
struct node{
	int l,r,data;
}t[N<<2];
struct edge{
	int next,to;
}e[N<<2];
void add(int u,int v){
	e[tot].to=v; e[tot].next=head[u]; head[u]=tot++;
}
void dfs(int u){
	st[u]=++cnt;	vis[u]=1;
	for(int i=head[u];~i;i=e[i].next)	if(!vis[e[i].to])	dfs(e[i].to);
	ed[u]=cnt;
}
void push_up(int p){
	t[p].data=t[p<<1].data+t[p<<1|1].data;
}
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	if(l==r){
		t[p].data=1;	return ;
	}	
	int mid=l+r>>1;
	build(p<<1,l,mid);	build(p<<1|1,mid+1,r);
	push_up(p);
}
void change(int p,int x){
	if(t[p].l==t[p].r){
		t[p].data^=1;	return ;
	}
	int mid=t[p].l+t[p].r>>1;
	if(x<=mid)	change(p<<1,x);
	else	change(p<<1|1,x);
	push_up(p);
}
int ask(int p,int l,int r){
	if(t[p].l==l&&t[p].r==r)	return t[p].data;
	int mid=t[p].l+t[p].r>>1;
	if(r<=mid)	return ask(p<<1,l,r);
	else if(l>mid)	return ask(p<<1|1,l,r);
	else	return ask(p<<1,l,mid)+ask(p<<1|1,mid+1,r);
}
int main(){
	memset(head,-1,sizeof head);
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;	scanf("%d %d",&u,&v);	add(u,v);	add(v,u);
	}
	dfs(1);
	build(1,1,n);
	scanf("%d",&m);
	while(m--){
		char op;	int x;	cin>>op>>x;
		if(op=='Q')	printf("%d\n",ask(1,st[x],ed[x]));
		else	change(1,st[x]);
	}
	return 0;
}