题干:

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
"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
"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

题目大意:

给出一个苹果树,每个节点一开始都有苹果

C X,如果X点有苹果,则拿掉,如果没有,则新长出一个(即:可以用异或处理)

Q X,查询X点与它的所有后代分支一共有几个苹果

解题报告:

     dfs序,然后用线段树单点更新+区间查询一下就可以了。(dfs序是专门用来处理一棵树的子树查询的,需要用线段树或树状数组维护查询)

AC代码:(用输入外挂,用时少了200ms左右,不知道rank1的63ms是怎么做到的)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 100000 + 5;
int n,m;
int cnt,id;
int head[MAX*10];
int q[MAX*4],in[MAX*4],out[MAX*4];
struct Edge {
	int fr;
	int to;
	int ne;
} e[MAX * 4];//因为可能双向边? 
struct TREE {
	int l,r;
	int val;
} tree[MAX * 8];
void add(int u,int v) {
	e[++cnt].to = v;
	e[cnt].fr = u;
	e[cnt].ne = head[u];
	head[u] = cnt;
}
void pushup(int cur) {
	tree[cur].val = tree[cur*2].val + tree[cur*2+1].val;
}
void build(int l,int r,int cur) {
	tree[cur].l = l;
	tree[cur].r = r;
	if(l == r) {
		tree[cur].val = 1;
		return ;//又忘写return了。。。 
	}
	int m = (l+r)/2;
	//刚开始没写build递归。。。 
	build(l,m,cur*2);
	build(m+1,r,cur*2+1);
	pushup(cur);
}
void dfs(int cur,int root) //cur为当前点,root为根节点 
{
	q[++id] =cur;
	in[cur] = id;
	for(int i = head[cur]; i!=-1;i = e[i].ne) {
		dfs(e[i].to,cur);
	}
	out[cur] = id;
}
void update(int tar,int cur) {
	if(tree[cur].l == tree[cur].r) {
		tree[cur].val ^= 1;
		return ; 
	}
	if(tar <= tree[cur*2].r) update(tar,cur*2);//刚开始写成tree[cur*2].l了
	else update(tar,cur*2+1);
	pushup(cur);
}
int query(int pl,int pr,int cur) {
	if(pl <= tree[cur].l && pr >= tree[cur].r) {
		return tree[cur].val;
	}
	int res = 0;
	if(pl <= tree[cur*2].r) res += query(pl,pr,cur*2);
	if(pr >= tree[cur*2+1].l) res += query(pl,pr,cur*2+1);
	return res; 
}
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
	cin>>n;
	char op[10];
	int x;
	cnt = id = 0;
	memset(head,-1,sizeof head);
	int u,v;
	for(int i = 1; i<n; i++) {
		//scanf("%d%d",&u,&v);
		u = read();
		v = read();
		add(u,v);
	}
	dfs(1,0);//这个0 是没用的 

	build(1,n,1);
//	printf("**********\n");
//	for(int i = 1; i<=5; i++) {
//		printf("%d|  ",tree[i].val);
//	}
//	printf("\n****************\n");
	scanf("%d",&m);
	while(m--) {
		scanf("%s",op);
		if(op[0] == 'C') {
			//scanf("%d",&x);
			x = read();
			update(in[x],1);
		}
		else {
			//scanf("%d",&x);
			x = read();
			int ans = query(in[x],out[x],1);
			printf("%d\n",ans);
		}
	} 
	return 0 ;
 } 

总结:

   小错误还是很多啊。。。