Tunnel Warfare HDU - 1540(线段树做法 (修改查询区间最大值最小值)or stl做法)

题目

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到n,有三种操作 1:将x标记
2:查询x左右最长连续未被标记的长度,包括x
3:恢复最后一个被标记的位置

线段树做法

借助线段树区间最大值最小值的修改来实现,线段树记录被标记的节点后的区间最大值最小值。
首先构造树,所有最小值初始化为n+1,最大值初始化为 0.
操作1对应的是 x位置标记 等效于将x所在节点的最大值最小值都修改为x。
操作2 等于 在线段树上 【1,x】查询被标记的最大值l,在【x,n】上查询被标记的最小值r。如果r == l,即r == l == x,则直接输出0,else,则操作2的结果长度就是r-l-1。
操作3恢复最后一个标记位置,只需要在之前将标记的点存入栈,现在就pop出来一个,然后借助update_max修改最大值为0,不对区间查询的结果产生影响,同理将update_min修改最小值为n+1。

#include <cstdio>
#include <cmath>
#include <stack>
int const maxn =  1e5;
using namespace std;
struct node{
	int max;
	int min;
}num[maxn*4];
int n;
void bulid(int l, int r, int ret){
	int m = (l+r)>>1;
	if(l == r){
		num[ret].min = n+1;
		num[ret].max = 0;
		return ;
	}
	bulid(l, m, ret<<1);
	bulid(m+1, r, ret<<1|1);
	num[ret].max = max(num[ret<<1].max,num[ret<<1|1].max);
	num[ret].min = min(num[ret<<1].min, num[ret<<1|1].min); 
}
void update(int l, int r, int x, int y,int ret){
	if(l == r){
		num[ret].max = max(num[ret].max, y);
		num[ret].min = min(num[ret].min, y);
		return ;
	}
	int m = (l+r)>>1;
	if(m < x)
		update(m+1, r, x, y, ret<<1|1);
	else
		update(l, m, x, y, ret<<1);
	num[ret].max = max(num[ret<<1].max,num[ret<<1|1].max);
	num[ret].min = min(num[ret<<1].min, num[ret<<1|1].min);
}
void update_max(int l, int r, int x, int y,int ret){
	if(l == r){
		num[ret].max =  y;

		return ;
	}
	int m = (l+r)>>1;
	if(m < x)
		update_max(m+1, r, x, y, ret<<1|1);
	else
		update_max(l, m, x, y, ret<<1);
	num[ret].max = max(num[ret<<1].max,num[ret<<1|1].max);
}
void update_min(int l, int r, int x, int y,int ret){
	if(l == r){
		num[ret].min =  y;
		return ;
	}
	int m = (l+r)>>1;
	if(m < x)
		update_min(m+1, r, x, y, ret<<1|1);
	else
		update_min(l, m, x, y, ret<<1);
	num[ret].min = min(num[ret<<1].min, num[ret<<1|1].min);
}
int query_max(int L, int R, int l, int r, int ret){
	int ans = 0, m;
	if(L <= l && r <= R){
		return num[ret].max;
	}
	m = (l+r)>>1;
	if(m >= L)
		ans = max(ans,query_max(L, R, l, m, ret<<1));
	if(m < R)
		ans = max(ans, query_max(L,R, m+1, r, ret<<1|1));
	return ans; 
}
int query_min(int L, int R, int l, int r, int ret){
	int ans = n+1, m; //ans = 0x3f3f3f3f; 
	if(L <= l && r <= R){
		return num[ret].min;
	}
	m = (l+r)>>1;
	if(m >= L)
		ans = min(ans,query_min(L, R, l, m, ret<<1));
	if(m < R)
		ans = min(ans, query_min(L,R, m+1, r, ret<<1|1));
	return ans; 
}
int main(){
	int m;
	char ch[2];
	int temp; 
	int l, r;
	while(~scanf("%d %d", &n, &m)){
		stack<int> st;
		bulid(1, n, 1);
		for (int i = 0; i < m; i++){
			scanf("%s", &ch);
			if(ch[0] == 'D'){
				scanf("%d", &temp);
				update(1, n, temp, temp, 1);
				st.push(temp);
			}
			else if(ch[0] == 'Q'){
				scanf("%d", &temp);
				r = query_min(temp, n, 1, n, 1);
				l = query_max(1, temp, 1, n, 1);
				if(l == r){
					printf("0\n");
				}
				else{
					printf("%d\n",r-l-1);
				}
			}
			else{
				int tt = st.top();
				st.pop();
				update_max(1, n, tt, 0 ,1);
				update_min(1, n, tt, n+1, 1);
				
			}
		} 
	}
	
	return 0;
}

stl做法

借助stack和set实现,set自动排序,操作1就insert,操作2就upper_bound分别查找大于等于x和小于x等于的在set中的第一个数。然后处理方式参照方法1。操作3就是弹出栈的元素在set中用erease删除。

#include <cstdio>
#include <set>
#include <stack>
using namespace std;

int main(){
	int n, m;
	char ch[2];
	int temp;
	int tt;
	int l, r;
	while(~scanf("%d %d", &n, &m)){
		stack<int> st;
	set<int> se;
	se.insert(0);
	se.insert(n+1); 
	for (int i = 0; i < m; i++){
	
		scanf("%s", &ch);
		if(ch[0] == 'D'){
			scanf("%d", &temp);
			se.insert(temp);
			st.push(temp);
		}
		else if(ch[0] == 'Q'){
			scanf("%d", &temp);
			if(se.count(temp)){
				printf("0\n");
			}
			else{
				l = *(--se.upper_bound(temp));
				r = *(se.upper_bound(temp));
				printf("%d\n", r-l-1);
			}
		
		}
		else{
			tt = st.top();
			st.pop();
			se.erase(se.find(tt));
		}
	} 	
	}
	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值