Apple Tree POJ - 3321 dfs+线段树

Apple Tree POJ - 3321

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

题意:
有一颗苹果树,上面有若干个苹果,2种操作,一个是摘苹果,一个是查询某个节点上的苹果数量。

分析:
N最大10W,M查询次数最大10W。很快就想到线段树或者树状数组来维护,但是我们发现,这是一颗树,都是一个一个的点,线段树都是一个一个的区间来表示,那么我们能不能把点变成区间呢。其实我们发现,一棵树的dfs序的编号就是改节点的区间,下图解释:

在这里插入图片描述

蓝色框的数字时dfs进入时的编号,红色的是dfs出去的编号,我们可以神奇的发现,正好就是线段树的区间,然后我们就可以构造线段树了

下面展示代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <functional>
#include <vector>
#define INF (1<<30)
#define il inline
using namespace std;
int const N=100000+100;
int head[N<<1],cnt,n,m,u,v,x;
char op[2];
struct Edge
{
	int e;
	int to;
}edge[N<<1];
il void add_edge(int u,int v)
{
	edge[cnt].e=v;
	edge[cnt].to=head[u];
	head[u]=cnt++;
}
int l[N<<1],r[N<<1],cnt2;
il void dfs(int u,int f)
{
	l[u]=++cnt2;
	for(int i=head[u];i!=-1;i=edge[i].to)
	{
		int v=edge[i].e;
		if(v==f)continue;
		dfs(v,u);
	}
	r[u]=cnt2;
}
struct Segment_tree
{
	int l,r;
	int sum;
}T[N<<2];
il void pushup(int p)
{
	T[p].sum=T[p<<1].sum+T[p<<1|1].sum;
}
il void build(int l,int r,int p)
{
	T[p].l=l,T[p].r=r;
	if(l==r)
	{
		T[p].sum=1;
		return ;
	}
	int m=(l+r)>>1;
	build(l,m,p<<1),build(m+1,r,p<<1|1);
	pushup(p);
}
il void update(int pos,int p)
{
	if(T[p].l==T[p].r)
	{
		T[p].sum=(T[p].sum+1)%2;//如果已经存在,那么+1对2取模就是0,表示采摘苹果;如果本身不存在,0+1取模就是1,表示长出一个苹果
		return ;
	}
	int m=(T[p].l+T[p].r)>>1;
	if(pos<=m)update(pos,p<<1);
	else update(pos,p<<1|1); 
	pushup(p);
}
il int query(int l,int r,int p)
{
	if(l<=T[p].l&&T[p].r<=r)
	{
		return T[p].sum;
	}
	int m=(T[p].l+T[p].r)>>1;
	int ans=0;
	if(l<=m)ans+=query(l,r,p<<1);
	if(r>m)ans+=query(l,r,p<<1|1);
	return ans;
}
int main()
{
	memset(head,-1,sizeof(head));
	cnt=1;
	cnt2=0;
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&u,&v);
		add_edge(u,v);
		add_edge(v,u);
	}
	dfs(1,-1);
//	for(int i=1;i<=n;i++)
//	{
//		printf("%d %d\n",l[i],r[i]);
//	}
	build(1,n,1);
	scanf("%d",&m);
	while(m--)
	{
		scanf("%s%d",op,&x);
		if(op[0]=='Q')
		{
			printf("%d\n",query(l[x],r[x],1));
		}
		else 
		{
			update(l[x],1); 
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值