HDU 1540 线段树区间合并抽象学习

Problem Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below:
D x: The x-th village was destroyed.
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.

Output

Output the answer to each of the Army commanders’ request in order on a separate line.

Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

思路

一道关于线段树的好题,需要一定的抽象,我都是琢磨别人的代码才搞明白的,惭愧惭愧。
对于这道题,需要动态维护两个区间,一个是左连续区间,一个是右连续区间。左连续可以理解为当前区间开始的那个村庄能够联系的村庄数量。右连续可以理解为当前区间结束的那个村庄能联系的村庄的数量。
1.对于build函数,初始化时左连续和右连续都是当前区间的长度。
2.对于update函数,更新有两个状态,一个是摧毁,一个是重建,摧毁为-1,重建为1,当更新到叶子节点时,如果是摧毁直接变为0,重建则直接变为1。其余都是正常的单点更新模板。重点在于它的pushup函数。
3.pushup函数:对于节点的右连续,有两种可能,一种是它左节点的左连续的长度,一种是除了左节点的长度以外,还在右节点有一些连续的长度。同理右节点也是如此。所以需要加以判断如果当前连续节点的长度达到了最大,那么当前的长度就可以加上另一边节点的长度了。
4.query函数,这个函数是这一个算法的核心。首先对于已经达到底层,直接返回叶子节点的状态。如果当前节点的左连续或右连续能够到达x那么ans就等于这个值,但是,还要考虑相邻子树的情况,因为可能在旁边的子树中还有一部分的值在其中。对于当前节点,如果为左子树,那么它的值只可能存在一部分在其的右边相邻子树中(不可能有值在其左相邻子树中,否则它的父节点就已经将其匹配了)同理右子树也是如此,所以只需要单独判断即可。

AC代码

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=50005;
int Lsum[maxn<<2];//维护当前区间从左到右的连续的村庄数 
int Rsum[maxn<<2];//维护当前区间从右到左的连续的村庄数 
int des[maxn];//用于保存最近一个被摧毁的村庄 
int n,m;
void pushup(int id,int l,int r){
	int mid=(l+r)>>1;
	Lsum[id]=Lsum[id<<1]; //默认时,当前区间的从左到右的村庄数是左结点的相同的值 
	Rsum[id]=Rsum[id<<1|1];//当前区间的从右到左的村庄数是右节点的相同的值 
	if(Lsum[id<<1]==(mid-l+1))Lsum[id]+=Lsum[id<<1|1];//如果左节点的左区间值达到了最大,就可以说明右节点可能还有区间的能够连接 
	if(Rsum[id<<1|1]==(r-mid))Rsum[id]+=Rsum[id<<1];//同理 
}
void build(int id,int l,int r){
	Lsum[id]=Rsum[id]=(r-l)+1;//初始时所有村庄都能连通 
	if(l==r){
		return;
	}
	int mid=(l+r)>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid+1,r);
} 
void update(int id,int l,int r,int x,int c){
	if(l==r){//用1表示回复-1表示摧毁,到最底层时如果为-1就置为0,1就置为1 
		if(c==1){
			Lsum[id]=Rsum[id]=1;
		}
		else{
			Lsum[id]=Rsum[id]=0;
		}
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid)update(id<<1,l,mid,x,c);//普通的单点更新操作 
	else update(id<<1|1,mid+1,r,x,c);
	pushup(id,l,r);
}
int query(int id,int l,int r,int x){
	if(l==r){ //对于到达底部的值  直接返回即可 
		return Lsum[id]; 
	}
	int mid=(l+r)>>1;
	int ans=0;
	if(l<=x&&x<=r){//如果x在这个区间之中,就判断左连续或者右连续是否能到达这个点
		if(Lsum[id]>=x-l+1)ans=max(ans,Lsum[id]);
		else if(Rsum[id]>=r-x+1)ans=max(ans,Rsum[id]);
	}
	if(id%2==0){
	//如果当前节点为左节点,就判断它的右连续是否是包含了这个点的,如果是的话那么有可能右节点的左连续也能与其连接,直接加上 
		if(Rsum[id]>=r-x+1)ans=max(ans,Rsum[id]+Lsum[id+1]);
	}
	else{
	//同理右节点,判断是否有左节点的右连续可以加上 
		if(Lsum[id]>=x-l+1)ans=max(ans,Lsum[id]+Rsum[id-1]);
	}
	if(x<=mid)ans=max(ans,query(id<<1,l,mid,x));//单点查询的模板 
	if(x>mid)ans=max(ans,query(id<<1|1,mid+1,r,x));
	return ans;
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		int posd=0;
		memset(des,0,sizeof(des));
		build(1,1,n);
		char a[10];
		int x;
		for(int i=1;i<=m;i++){
			scanf("%s",a);
			if(a[0]=='D'){
				scanf("%d",&x);
				des[posd++]=x;
				update(1,1,n,x,-1);
			}
			else if(a[0]=='Q'){
				scanf("%d",&x);
				cout << query(1,1,n,x) << endl;
			} 
			else{
				if(posd==0)continue; 
				x=des[--posd];
				update(1,1,n,x,1);
			}
		}
	}
	return 0;
} 

总结

思维不够活跃,抽象深度不够。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值