【poj 2892】Tunnel Warfare 中文题意&题解&代码
来源:互联网 发布:图片动态制作软件 编辑:程序博客网 时间:2024/06/03 21:56
Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7577 Accepted: 3128
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个(最多32000个),给出3种操作
1,D x 表示x村庄被摧毁
2,R 表示修复上一个被摧毁的村庄
3,Q x 表示询问x以左和x以右的连续的没有被摧毁村庄 的个数,包括他自己
题解:线段数或者树状数组上二分,记录每个点是否有村庄,由于一开始要初始化为1,耗时,所以用0表示有村庄,-1表示没有,运算时自己计算,但这就造就了二分是很大的麻烦,还是不要作死哈。建议初始化成1,不会TLE
代码:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;bool vis[60000];int c[60000];int pre[60000];int fail;int a,b,n,m;int lowbit(int x){return (x&(-x));}int add(int x,int v){ for (;x<=n;x+=lowbit(x)) c[x]+=v;}int que(int x){ int ans=0; for (;x>0;x-=lowbit(x)) ans+=c[x]; return ans;}int erfen(int p){ int ll,rr; int l,r,mid; //cout<<" a is "<<p<<endl; l=0;r=p-1; int ss; while(l<=r) { mid=(l+r)>>1; ss=(n+que(p-1))-(n+que(p-1-mid)); if (ss==0) {l=mid+1;ll=mid;} else {r=mid-1;} } // cout<<" ll is "<<ll<<endl; l=0;r=n-p; //cout<<" start r is "<<r<<endl; while(l<=r) { mid=(l+r)>>1;//cout<<" l &r is "<<p+mid<<" "<<p<<endl; ss=(n+que(p+mid))-(n+que(p)); // cout<<"que(6& 4 ) is "<<que(6)<<" "<<que(4)<<endl; if (ss==0) {l=mid+1;rr=mid;} else {r=mid-1;} } // cout<<" rr is "<<rr<<endl; printf("%d\n",ll+rr+1);}int main(){ cin>>n>>m; char cc[7]; for (int i=1;i<=m;i++) { scanf("%s",&cc); if (cc[0]=='D') { scanf("%d",&a); add(a,-1); pre[++fail]=a; vis[a]=1; } else if (cc[0]=='Q') { scanf("%d",&a); if (vis[a]==1) {printf("%d\n",0);continue;} erfen(a); } else if (cc[0]=='R') { a=pre[fail--]; add(a,1); // cout<<" R IS "<<a<<endl; vis[a]=0; } }}
- 【poj 2892】Tunnel Warfare 中文题意&题解&代码
- 【poj 2892】Tunnel Warfare 题意&题解&代码(C++)
- 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
- 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 1451】T9 中文题意&题解&代码(c++)
- swift表格(UITableView)介绍与基础实现
- python 获取网络时间及修改本地时间
- set
- 阿里云CentOS使用vsftpd搭建FTP服务器
- Angularjs + Webpack 框架里,引入Bootstrap-datepicker插件时,datepicker is not a function错误的解决方法
- 【poj 2892】Tunnel Warfare 中文题意&题解&代码
- ubuntu root默认密码(初始密码)的修改
- @Autowired与@Resource的区别
- 类型萃取
- 牛腩回顾历程
- vector
- Android触摸事件(二)-MoveAndScaleTouchHelper,触摸辅助工具类
- Excel 打开执行某方法,定时关闭
- vector学习小节(一)