Tunnel Warfare(线段树+查找最大值最小值+模板)

Tunnel Warfare

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 27   Accepted Submission(s) : 7
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
 

Source
POJ Monthly
 

Statistic |  Submit |  Back

参考:http://blog.csdn.net/chudongfang2015/article/details/52133243

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少

思路:

首先,在某点时,用线段树求出这点之前的被破坏的村庄的最大值,这点之后的被破坏村庄的最小值,注意特殊情况(如最大值最小值均是本身),所以这就需要在建立树的时候,使得没有被破坏的村庄用0和n+1代替然后存入线段树的节点中去,然后最大值最小值套模板即可

注意在写更新操作是单点更新用了两个数,因为这样是为了以后的恢复,一个数代表村庄,一个数代表被恢复后得数(0或n+1)

代码:’

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define INF 0x3f3f3f3f
#define maxn 50010
struct Tree{
	int maxx;
	int minn;
}tree[maxn<<2];//线段树结构体,用来求最大值最小值

int n,m; 

int ds[maxn<<2];//保存被破坏的村庄,便于恢复
//建树
void build(int l,int r,int i){
	if(l==r){
		tree[i].maxx=0;
		tree[i].minn=n+1;
		return;
	}
	else{
	int mid=(l+r)>>1;
	build(l,mid,L(i));
	build(mid+1,r,R(i));
	tree[i].maxx=max(tree[L(i)].maxx,tree[R(i)].maxx);
	tree[i].minn=min(tree[L(i)].minn,tree[R(i)].minn);
	
	}
	
}
void update1(int s,int e,int l,int r,int i){  //更新求最大
	if(l==r){
		tree[i].maxx=e;
		return;
		}
	else{
	int mid=(l+r)>>1;
	if(s<=mid)
	update1(s,e,l,mid,L(i));
	else
	update1(s,e,mid+1,r,R(i));
	tree[i].maxx=max(tree[L(i)].maxx,tree[R(i)].maxx);

	}
}

void update2(int s,int e,int l,int r,int i)//更新求最小
{
	if(l==r){
		tree[i].minn=e;
		return;
		}
	else{
	int mid=(l+r)>>1;
	if(s<=mid)
	update2(s,e,l,mid,L(i));
	else
	update2(s,e,mid+1,r,R(i));
	tree[i].minn=min(tree[L(i)].minn,tree[R(i)].minn);
	}
}

int query_max(int s,int e,int l,int r,int i){//查找区间最大值
	if(s<=l&&r<=e){
		return tree[i].maxx;
	}
	
	int mid=(l+r)>>1;
	int res=0;
	
	if(s<=mid)
		res=max(res,query_max(s,e,l,mid,L(i)));
	if(e>mid)
		res=max(res,query_max(s,e,mid+1,r,R(i)));
	return res;
}

int query_min(int s,int e,int l,int r,int i){//查找区间最小子
	if(s<=l&&r<=e){
		return tree[i].minn;
	}
	
	int mid=(l+r)>>1;
	int res=INF;
	
	if(s<=mid)
		res=min(res,query_min(s,e,l,mid,L(i)));
	if(e>mid)
		res=min(res,query_min(s,e,mid+1,r,R(i)));
	return res;
}


int main(){
	int i,ans,x;
	char ch[15];
	while(scanf("%d%d",&n,&m)!=EOF){
		ans=0;
		memset(ds,0,sizeof(ds));
		memset(tree,0,sizeof(tree));
		build(1,n,1);
		while(m--){
			scanf("%s",&ch);
			if(ch[0]=='D'){
				scanf("%d",&x);
				update1(x,x,1,n,1);
				update2(x,x,1,n,1);
				ds[++ans]=x;
				
			}
			else if(ch[0]=='Q'){
				int max1,min1;
				scanf("%d",&x);
				max1=query_max(1,x,1,n,1);
				min1=query_min(x,n,1,n,1);//据线段树查询
				if(max1==min1)
				printf("0\n");
				else
				printf("%d\n",(min1-max1-1));
			}
			else{
				int t=ds[ans--];
				update1(t,0,1,n,1);//恢复村庄,注意时0或n+1
				update2(t,n+1,1,n,1);
				
			}
		}
	}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值