线段树的dfs序总结+例题

dfs序记录的是一棵树从根节点开始的访问顺序。
把一棵树变成一行数字,这样就可以在区间中访问,in[x]与out[x]之间是x的子树。

例题:

1.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
题意:有一棵树,上面有n个节点(根节点为1),初始时每一个节点上有一个苹果,

有n-1条树枝,每个树枝连接两个节点,有两种操作类型:

C x 表示,如果x节点有苹果的话Kaka会把它摘下来,如果没有苹果的话,会长出来一个。

Q x 表示,要输出x以及其子树上的苹果数量。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m,t,num;
struct node{
	int to,ne;
}e[maxn*2];
int fir[maxn*2],in[maxn*2],out[maxn*2];
void add(int u,int v){
	e[++t].to=v;
	e[t].ne=fir[u];
	fir[u]=t;
}
void dfs(int x,int fa){
	in[x]=++num;
	for(int i=fir[x];i!=-1;i=e[i].ne){
		int v=e[i].to;
		if(v==fa)continue;
		dfs(v,x);
	}
	out[x]=num;
}
struct node1{
	int l,r,sum;
}tree[maxn*4];
void build(int l,int r,int n){
	tree[n].l=l;tree[n].r=r;
	if(l==r){
		tree[n].sum=1;
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,n<<1);
	build(mid+1,r,n<<1|1);
	tree[n].sum=tree[n<<1].sum+tree[n<<1|1].sum;
} 
void update(int x,int n){
	if(tree[n].l==tree[n].r){
		tree[n].sum=(tree[n].sum+1)%2;
		return;
	}
	int mid=(tree[n].l+tree[n].r)>>1;
	if(x<=mid)update(x,n<<1);
	else update(x,n<<1|1);
	tree[n].sum=tree[n<<1].sum+tree[n<<1|1].sum;
}
int query(int n,int l,int r){
	if(tree[n].l==l&&tree[n].r==r){
		return tree[n].sum;
	}
	int mid=(tree[n].l+tree[n].r)>>1;
	if(r<=mid)return query(n<<1,l,r);
	else if(l>mid)return query(n<<1|1,l,r);
	else return query(n<<1,l,mid)+query(n<<1|1,mid+1,r);
}
int main(){
	while(~scanf("%d",&n)){
	memset(e,0,sizeof(e));
	memset(fir,-1,sizeof(fir));
	int v,u;
	t=0;
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add(u,v);
		add(v,u);
	}
	num=0;
	dfs(1,0);
	build(1,num,1);
	char op[3];
	int m;
	scanf("%d",&m);
	while(m--){
		scanf("%s%d",op,&u);
		if(op[0]=='C'){
			update(in[u],1);
		}
		else{
			printf("%d\n",query(1,in[u],out[u]));
		}
	}
}
	return 0;
}

2.Assign the task HDU - 3974
There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody’s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
Input
The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.

For each test case:

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).

The next line contains an integer M (M ≤ 50,000).

The following M lines each contain a message which is either

“C x” which means an inquiry for the current task of employee x

or

"T x y"which means the company assign task y to employee x.

(1<=x<=N,0<=y<=10^9)
Output
For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.
Sample Input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
Sample Output
Case #1:
-1
1
2
题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属。都停下手上的工作,开始做a。
操作 T x y 分配x任务y,C x询问x的当前任务;

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=50005;
int n,t,rudu[maxn],q[maxn*4],id,m,in[maxn*4],out[maxn*4],head[maxn*2];
struct node{
	int to;
	int ne;
}e[maxn*2];
struct node1{
	int l,r;
	int val;
	int laz;
}tree[maxn*4];
void dfs(int x,int root){
	q[++id]=x;
	in[x]=id;
	for(int i=head[x];i!=-1;i=e[i].ne){
		dfs(e[i].to,x);
	}
	out[x]=id;
}
void add(int u,int v){
	e[++t].to=v;
	e[t].ne=head[u];
	head[u]=t;
}
void pushdown(int cur){
	if(tree[cur].laz==0)return;
	tree[cur<<1].val=tree[cur<<1|1].val=tree[cur<<1].laz=tree[cur<<1|1].laz=tree[cur].val;
	tree[cur].laz=0;
}
void update(int pl,int pr,int val,int cur){
	if(pl<=tree[cur].l&&pr>=tree[cur].r){
		tree[cur].val=val;
		tree[cur].laz=val;
		return;
	}
	pushdown(cur);
	if(pl<=tree[cur<<1].r)update(pl,pr,val,cur<<1);
	if(pr>=tree[cur<<1|1].l)update(pl,pr,val,cur<<1|1);
}
void build(int l,int r,int cur){
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].val=-1;
	tree[cur].laz=0;
	if(l==r)return;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1); 
}
int query(int tar,int cur){
	if(tree[cur].l==tree[cur].r){
		return tree[cur].val;
	}
	pushdown(cur);
	if(tar<=tree[cur<<1].r)return query(tar,cur<<1);
	else return query(tar,cur<<1|1);
}
int main(){
	int T;
	scanf("%d",&T);
	int cas=1;
	while(T--){
		memset(head,-1,sizeof(head));
		memset(rudu,0,sizeof(rudu));
		memset(e,0,sizeof(e));
		t=id=0;
		scanf("%d",&n);
		int u,v;
		for(int i=1;i<n;i++){
			scanf("%d%d",&u,&v);
			add(v,u);
			rudu[u]++;
		}
		int st=1;
		for(int i=1;i<=n;i++){
			if(rudu[i]==0){
				st=i;
				break;
			}
		}
		id=0;
		dfs(st,0);
		build(1,n,1);
		scanf("%d",&m);
		char op[2];
		printf("Case #%d:\n",cas++);
		while(m--){
			scanf("%s",op);
			if(op[0]=='C'){
				scanf("%d",&u);
				printf("%d\n",query(in[u],1));
			}
			else{
				scanf("%d%d",&u,&v);
				update(in[u],out[u],v,1);
			} 
		}
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值