poj 2892 Tunnel Warfare(树状数组+二分)
来源:互联网 发布:科比0506赛季数据 编辑:程序博客网 时间:2024/04/30 18:59
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Hint
An illustration of the sample input:
OOOOOOOD 3 OOXOOOOD 6 OOXOOXOD 5 OOXOXXOR OOXOOXOR OOXOOOO
三个操作:
D pos 将pos位置摧毁,让它和周围不相连。
Q pos 问和pos 相连的有多少个村庄。
R 修复最近摧毁的村庄。
思路:D和R用树状数组很好维护,关键在于Q如何查找?
我们可以做两次二分,一次从1~pos,一次从pos到n。 求得最远的满足sum(r)-sum(l)==r-l+1的点,然后两段距离加起来减一即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 50050int n,m;int a[N],c[N];int lowbit(int x){ return x&-x;}void add(int x,int v){ for(int i=x;i<=n;i+=lowbit(i)) c[i]+=v;}int sum(int x){ int ans=0; for(int i=x;i;i-=lowbit(i)) ans+=c[i]; return ans;}int main(){ char op[5]; while(~scanf("%d %d",&n,&m)) { memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) add(i,1); int cnt=0,t; while(m--) { scanf("%s",op); if(op[0]=='D') { scanf("%d",&t); add(t,-1); a[cnt++]=t; } else if(op[0]=='R') add(a[--cnt],1); else { scanf("%d",&t); if(sum(t)==sum(t-1)) { printf("0\n"); continue; } int l=1,r=t,ans=0; while(l<=r) { int mid=(l+r)>>1; if(sum(t)-sum(mid-1)==t-mid+1) { ans=t-mid+1; r=mid-1; } else l=mid+1; } l=t,r=n; int ans1=0; while(l<=r) { int mid=(l+r)>>1; if(sum(mid)-sum(t-1)==mid-t+1) { ans1=mid-t+1; l=mid+1; } else r=mid-1; } printf("%d\n",ans1+ans-1); } } } return 0;}
- POJ 2892 Tunnel Warfare (树状数组+二分)
- 【poj 2892】Tunnel Warfare 二分+树状数组
- poj 2892 Tunnel Warfare(树状数组+二分)
- 【POJ 2892】 Tunnel Warfare(树状数组+二分)
- poj 2892 Tunnel Warfare(树状数组+二分)
- POJ-2892 Tunnel Warfare 树状数组
- POJ 2892 Tunnel Warfare [树状数组]
- 【HDU】Tunnel Warfare 树状数组+二分
- poj 2892 Tunnel Warfare(线段树#5/树状数组)
- poj 2892 Tunnel Warfare
- poj 2892 Tunnel Warfare
- poj 2892 Tunnel Warfare
- poj 2892 Tunnel Warfare
- POJ 2892 Tunnel Warfare
- poj 2892 Tunnel Warfare
- POJ 2892 Tunnel Warfare
- POJ 2892 Tunnel Warfare
- poj 2892Tunnel Warfare
- jquery遍历数组的问题
- 【JavaWeb开发】Eclipse或MyEclipse配置Tomcat
- 慢启动以及传输原理
- jquery onready的问题
- [Array]Two Sum
- poj 2892 Tunnel Warfare(树状数组+二分)
- JAVA设计模式之单例模式
- jquery mobile.js文件与jquery.js的插入顺序
- cocos2dx 基础知识
- c++关闭io同步流
- 博客自荐(十一期 2016年07月)
- 手把手教你用python写脚本看caffe训练好的网络测试单张图片的输出
- java中哈希表,Map,HashCode,Equals等介绍
- CODEVS 1134 noip2011 铺地毯