HDU - 1540 -- Tunnel Warfare【线段树区间求和 + 二分答案】

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条1到n的隧道,然后有三个操作。
Q 	X:查询X周围连通的隧道有多少个(包含自己)
D	X:摧毁X位置的隧道。
R	 :恢复最近被摧毁位置的通道。
题意
处理D,R:线段树的单点修改,分别是置0置1. R用操作用数组模拟栈操作。
处理Q:借助区间求和,sum是单调递增的,可以左边走一个二分答案,右边走一个二分答案即可。
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ls (rt << 1)
#define rs (rt << 1 | 1) 
using namespace std;
const int maxn = 5e4 + 5;
int sum[maxn<<2], arr[maxn];
int n, m;
void pushUp(int rt){
	sum[rt] = sum[ls] + sum[rs];
}
void build(int rt, int l, int r){
	if (l == r){
		sum[rt] = 1;
		return; 
	}
	int m = (l + r) >> 1;
	build(ls, l, m);
	build(rs, m + 1, r);
	pushUp(rt);
}
void update(int rt, int l, int r, int x, int C){
	if (l == r){
		sum[rt] = C;
		return;
	}
	int m = (l + r) >> 1;
	if (x <= m)	update(ls, l, m, x, C);
	else 		update(rs, m + 1, r, x, C);
	pushUp(rt);
}
int query(int rt, int l, int r, int L, int R){
	if (L <= l && r <= R){
		return sum[rt];
	}
	int m = (l + r) >> 1;
	int res = 0;
	if (L <= m)		res += query(ls, l, m, L, R);
	if (R > m)		res += query(rs, m + 1, r, L, R);
	return res;
}
bool check(int l , int r){
	int tot = query(1, 1, n, l, r);
	return r - l + 1 == tot;
}
void solve(){
	while(~scanf("%d%d", &n, &m)){
		build(1, 1, n);
		char handle[2];
		int x, cnt = 0;
		while (m--){
			scanf("%s", &handle);
			if (handle[0] == 'D'){
				scanf("%d", &x);
				update(1, 1, n, x, 0);
				arr[++cnt] = x;
			} else if (handle[0] == 'Q'){
				scanf("%d", &x);
				//最大值最小化,左开右闭 
				int l = 0, r = x;
				while (r - l > 1){
					int m = l + (r - l) / 2;
					if (check(m, x))	r = m;
					else	l = m;
				}
				int ans = x - r;
				//最小值最大化,左闭右开 
				l = x, r = n + 1;
				while (r - l > 1){
					int m = l + (r - l) / 2;
					if (check(x, m))	l = m;
					else	r = m;
				}
				ans += l - x;
				if (check(x, x))	ans++;
				printf("%d\n", ans);
			} else {
				int y = arr[cnt--];
				update(1, 1, n, y, 1);
			}
		}	
	}
}
int main(){
	solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值