POJ 3321 Apple Tree(DFS序+線段樹單點修改區間查詢)

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25904   Accepted: 7682

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

題目連接:POJ 3321

很久以前就想做,但不知道如何轉換,跟另外一題差不多的意思,主要是進行單點修改的應該怎麼寫要知道據說樹狀數組更快,然而並不是是很懂樹狀數組的原理,只會模版,就直接用線段樹做了。

代碼:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct seg
{
	int l,mid,r;
	int sum;
};
seg T[N<<2];
struct edge
{
	int to;
	int pre;
}E[N];
int head[N],cnt,in[N],out[N],total;
void add(int s,int t)
{
	E[cnt].to=t;
	E[cnt].pre=head[s];
	head[s]=cnt++;
}
void init()
{
	MM(head,-1);
	cnt=0;
	MM(in,0);
	MM(out,0);
	total=0;
}
void dfs(int now,int fa)
{
	in[now]=++total;
	for (int i=head[now]; ~i; i=E[i].pre)
	{
		int v=E[i].to;
		if(v!=fa)
		{
			dfs(v,now);
		}
	}
	out[now]=total;
}
void pushup(int k)
{
	T[k].sum=T[LC(k)].sum+T[RC(k)].sum;
}
void build(int k,int l,int r)
{
	T[k].l=l;
	T[k].r=r;
	T[k].mid=MID(l,r);
	if(l==r)
		T[k].sum=1;
	else
	{
		build(LC(k),l,T[k].mid);
		build(RC(k),T[k].mid+1,r);
		pushup(k);
	}
}
void update(int k,int x)
{
	if(T[k].l==T[k].r&&T[k].l==x)
		T[k].sum^=1;
	else
	{
		if(x<=T[k].mid)
			update(LC(k),x);
		else
			update(RC(k),x);
		pushup(k);
	}
}
int query(int k,int l,int r)
{
	if(l<=T[k].l&&T[k].r<=r)
		return T[k].sum;
	else
	{
		if(r<=T[k].mid)
			return query(LC(k),l,r);
		else if(l>T[k].mid)
			return query(RC(k),l,r);
		else
			return query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r);			
	}
}
int main(void)
{
	int n,m,q,i,x,y,c;
	char ops[3];
	while (~scanf("%d",&n))
	{
		init();
		for (i=0; i<n-1; ++i)
		{
			scanf("%d%d",&x,&y);
			add(x,y);
		}
		dfs(1,-1);
		build(1,1,n);
		scanf("%d",&m);
		while (m--)
		{
			scanf("%s",ops);
			if(ops[0]=='C')
			{
				scanf("%d",&c);
				update(1,in[c]);
			}
			else if(ops[0]=='Q')
			{
				scanf("%d",&q);
				printf("%d\n",query(1,in[q],out[q]));
			}
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章