【HDU】Tunnel Warfare 树状数组+二分

5 篇文章 0 订阅
2 篇文章 0 订阅


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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


 

题意: 有n个村庄, 从1到顺次连接, 有m个操作。  D代表毁坏村庄i, Q代表询问与村庄相连最长未被毁坏的个数, R表示修复上一个会坏的村庄。


解题思路: 这道题是在kuangbin带你飞线段树练习里看到的,  但是想了一会并不会用线段树维护往左最长和往右最长, 反而想到了用树状数组和二分的方法(用两个树状数组维护)

我可以用树状数组维护一个区间和, 对于每一次询问pos, 我分别二分他的左右两边, check一个位置的区间和是否等于mid - pos。 这样做的复杂度为O(n * log * log) 但是好像是可以做到nlog, 之前有一题学长讲了说01树状数组+二分可以降到log, 跟lowbit有关, 没记太清,  这题N*log*log也能过。  

这道题有几个坑点: 首先它是多组, 然后每个点可以被毁坏多次, 我因为这个原因wa好多发。

恩。。。 这应该是线段树很经典的区间合并问题。 有时间学一下再写一次。  能A题的办法就是好办法!


//2017-7-29 11:47
//2017-7-29 12:37
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int MaxN = 5e4;
int presum[MaxN + 5], sufsum[MaxN + 5];
int n, k;
int q[MaxN + 5];
bool ok[MaxN + 5];
int lowbit(int x){ return x & (-x); }
int rve(int p) {return n - p + 1;}

int getsum(int a[], int p){
	int ans = 0;
	while(p > 0){
		ans += a[p];
		p -= lowbit(p);
	}
	return ans;
}

void add(int a[], int p, int x){
	while(p <= n){
		a[p] += x;
		p += lowbit(p);
	}
}

bool check(int a[], int r, int l){
	if(getsum(a, r) - getsum(a, l) == r - l) return true;
	else return false;
}

void solve(int p){
	if(ok[p]){//当前点已经被破坏
		printf("0\n");
		return;
	}
	int l = p, r = n, ans1, ans2;
	while(l <= r){ //右最长距离
		int mid = (l + r) >> 1;
		if(check(presum, mid, p)) ans1 = mid, l = mid + 1;
		else r = mid - 1;
	}
	l = 1, r = p;
	while(l <= r){//左最长距离
		int mid = (l + r) >> 1;
		if(check(sufsum, rve(mid), rve(p))) ans2 = mid, r = mid - 1;
		else l = mid + 1;
	}
	printf("%d\n", ans1 - ans2 + 1);
}

int main(){
	while(~scanf("%d %d", &n, &k)){
		for(int i = 1; i <= n; i++) add(presum, i, 1), add(sufsum, i, 1); 
		while(k--){
			char c; int p;
			scanf(" %c", &c);
			if(c == 'D'){
				scanf("%d", &p);
				if(ok[p] == 0){ //重复破坏的不管
					ok[p] = 1;
					add(presum, p, -1);
					add(sufsum, rve(p), -1);
				}
				q[++q[0]] = p;
			}
			else if(c == 'R'){
				p = q[q[0]--];
				if(ok[p] == 1){ //重复修复也不管
					add(presum, p, 1);
					add(sufsum, rve(p), 1);
				}
				ok[p] = 0;
			}
			else if(c == 'Q'){
				scanf("%d", &p);
				solve(p);
			}
		}
		memset(presum, 0, sizeof(presum));
		memset(sufsum, 0, sizeof(sufsum));
		memset(ok, 0, sizeof(ok));
		q[0] = 0;
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值