HDU 1540 线段树区间合并

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



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
题意:给出n个村庄,m次操作,D x代表毁坏村庄x,R代表修复上一个被毁坏的村庄,Q x询问包含此村庄最长没有被毁坏村庄的区间长度。
题解:区间合并,看我操作就行了~
ps:这题题面没说多case,但是是多case,x还可以大于y,坑的一笔。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
stack<int>sp;
struct node{
	int lef,rig,mas,yes;//mas此区间最长没被毁坏的长度,lef从左边开始没被毁坏的长度,rig从右边开始没被毁坏的长度
}tree[200005];
void build(int t,int l,int r){
	if(l==r){
		tree[t].lef=tree[t].rig=tree[t].mas=tree[t].yes=1;
		return ;
	}
	int mid=l+r>>1;
	build(t<<1,l,mid);
	build(t<<1|1,mid+1,r);
	if(tree[t<<1].lef==mid-l+1)tree[t].lef=tree[t<<1].lef+tree[t<<1|1].lef;//如果整个区间没被毁坏,那么lef就是整区间+右边的lef
	else tree[t].lef=tree[t<<1].lef;
	if(tree[t<<1|1].rig==r-mid)tree[t].rig=tree[t<<1|1].rig+tree[t<<1].rig;//同理
	else tree[t].rig=tree[t<<1|1].rig;
	tree[t].mas=max(max(tree[t<<1].mas,tree[t<<1|1].mas),tree[t<<1].rig+tree[t<<1|1].lef);
}
void update(int t,int d,int iss,int l,int r){
	if(l==r){
		tree[t].yes=tree[t].mas=tree[t].lef=tree[t].rig=iss;
		return ;
	}
	int mid=l+r>>1;
	if(d<=mid)update(t<<1,d,iss,l,mid);
	else update(t<<1|1,d,iss,mid+1,r);
	if(tree[t<<1].lef==mid-l+1)tree[t].lef=tree[t<<1].lef+tree[t<<1|1].lef;
	else tree[t].lef=tree[t<<1].lef;
	if(tree[t<<1|1].rig==r-mid)tree[t].rig=tree[t<<1|1].rig+tree[t<<1].rig;
	else tree[t].rig=tree[t<<1|1].rig;
	tree[t].mas=max(max(tree[t<<1].mas,tree[t<<1|1].mas),tree[t<<1].rig+tree[t<<1|1].lef);
}
int query(int t,int d,int l,int r){
	if(l==r||tree[t].mas==0||tree[t].mas==r-l+1){//如果到了叶节点或者这整段区间被毁坏或者全没被毁坏,返回
		return tree[t].mas;
	}
	int mid=l+r>>1;
	if(d<=mid){//如果包含的这个点在左子树
		if(d>=mid-tree[t*2].rig+1){//如果在左子树的rig包含的区间
			return query(t<<1,d,l,mid)+query(t<<1|1,mid+1,mid+1,r);
		}
		else return query(t<<1,d,l,mid);
	}
	else{//右子树
		if(d<=tree[t<<1|1].lef+mid){//右子树的lef
			return query(t<<1|1,d,mid+1,r)+query(t<<1,mid,l,mid);
		}
		else return query(t<<1|1,d,mid+1,r);
	}
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		build(1,1,n);
		int i;
		while(!sp.empty())sp.pop();
		for(i=1;i<=m;i++){
			char c[3];
			scanf("%s",c);
			int d;
			if(c[0]=='D'){
				scanf("%d",&d);
				update(1,d,0,1,n);
				sp.push(d);
			}
			else if(c[0]=='R'){
				if(sp.empty())continue;
				int last=sp.top();
				sp.pop();
				update(1,last,1,1,n);
			}
			else{
				scanf("%d",&d);
				printf("%d\n",query(1,d,1,n));
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值