Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7749   Accepted: 3195

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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3 OOXOOOO

D 6 OOXOOXO

D 5 OOXOXXO

R OOXOOXO

R OOXOOOO 题意:三种操作
D x 摧毁城市x
R 修复最后被摧毁的城市
Q x 问 x 所在最长未被摧毁的区间长度是多少 题解:对于询问Q x 二分枚举左右边界,利用树状数组进行维护
满足条件的区间满足此式子 SUM(r) - SUM(l-1) = r-l+1(SUM[i]代表前i个城市未被摧毁的城市数量,SUM(r) - SUM(l-1)代表[l,r]内城市的存留数量) 二分的时候注意一下下边界,初始值要在最小的基础上减一。
/**
题意:三种操作
D x 摧毁城市x
R 修复最后被摧毁的城市
Q x 问 x 所在最长未被摧毁的区间长度是多少 题解:对于询问Q x 二分枚举左右边界,利用树状数组进行维护
满足条件的区间满足此式子 SUM(r) - SUM(l-1) = r-l+1(SUM[i]代表前i个城市未被摧毁的城市数量,SUM(r) - SUM(l-1)代表[l,r]内城市的存留数量)
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
const int N = ; int c[N];
bool destory[N];
stack<int> stk;
int n,q;
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=n;i+=lowbit(i)){
c[i]+=v;
}
}
int getsum(int idx){
int v=;
for(int i=idx;i>=;i-=lowbit(i)){
v+=c[i];
}
return v;
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF){
while(!stk.empty()) stk.pop();
memset(c,,sizeof(c));
memset(destory,false,sizeof(destory));
for(int i=;i<=n;i++){
update(i,);
}
while(q--){
char s[];
int x;
scanf("%s",s);
if(s[]=='D'){
scanf("%d",&x);
if(destory[x]) continue;
destory[x] = true;
update(x,-);
stk.push(x); }else if(s[]=='R'){
if(stk.empty()) continue;
x = stk.top();
stk.pop();
update(x,);
destory[x] = false;
}else{
scanf("%d",&x);
if(destory[x]){
printf("0\n");
continue;
}
int l=x,r=x;
int low = ,high = x;
while(low<=high){
int mid = (low+high)>>;
if(getsum(x)-getsum(mid)==x-mid){
l = mid+;
high = mid-;
}else low = mid+;
}
low = x-,high = n;
while(low<=high){
int mid = (low+high)>>;
if(getsum(mid)-getsum(x-)==mid-x+){
r = mid;
low = mid+;
}else high = mid-;
}
printf("%d\n",r-l+);
}
}
}
return ;
}

poj 2892(二分+树状数组)的更多相关文章

  1. Lost Cows POJ - 2182 二分 + 树状数组

    Code: #include<cstdio> #include<stack> #include<cstring> #include<algorithm> ...

  2. 【BZOJ-2527】Meteors 整体二分 + 树状数组

    2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Stat ...

  3. 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  4. BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组

    BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...

  5. bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3173 插入的数是以递增的顺序插入的 这说明如果倒过来考虑,那么从最后一个插入的开始删除,不会对以某 ...

  6. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  7. zoj-3963 Heap Partition(贪心+二分+树状数组)

    题目链接: Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence ...

  8. 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组

    题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...

  9. 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

    题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...

随机推荐

  1. 常用模块之 os,json,shelve,xml模块

    os 即操作系统 在 os 中提供了很多关于文件,文件夹,路径处理的函数 这是我们学习的重点 os.path 是os模块下专门用于处理路径相关的 python是一门跨平台语言,由于每个平台路径规则不同 ...

  2. [译]The Python Tutorial#2. Using the Python Interpreter

    [译]The Python Tutorial#Using the Python Interpreter 2.1 Invoking the Interpreter Python解释器通常安装在目标机器的 ...

  3. A1027 Colors in Mars (20)(20 分)

    A1027 Colors in Mars (20)(20 分) People in Mars represent the colors in their computers in a similar ...

  4. [Hdu3507]Print Article(斜率优化)

    Description 题意:给N个数,按顺序全部取走,每次取一段连续的区间,代价为\((S[i]-S[j])^2+M\) 其中M为一个给定的常数,\(S[i]\)为前缀和 \(N\leq 50000 ...

  5. 51NOD 1292 1277(KMP算法,字符串中的有限状态自动机)

    在前两天的CCPC网络赛中...被一发KMP题卡了住了...遂决定,哪里跌倒就在哪里爬起来...把个KMP恶补一发,连带着把AC自动机什么的也整上. 首先,介绍设定:KMP算法计划解决的基本问题是,两 ...

  6. RAID与LVM磁盘阵列技术

    RAID(Redundant Array of Independent Disks,独立冗余磁盘阵列) RAID概念: RAID技术通过把多个硬盘设备组合成一个容量更大.安全性更好的磁盘阵列,并把数据 ...

  7. TCP/IP网络编程之套接字与标准I/O

    标准I/O函数 标准标准I/O函数有两个优点: 标准I/O函数具有良好的移植性 标准I/O函数可以利用缓冲提高性能 关于移植性无需过多解释,不仅是I/O函数,所有标准函数都具有良好的移植性.因为,为了 ...

  8. Nodejs-内置核心模块&npm包管理工具

    1.核心模块的意义 如果只是在服务器运行JavaScript代码,其实意义不大(浏览器就可以解决)因为无法实现功能(读写文件,访问网络) Node的用处在于本身还提供了一系列的功能模块,用于与操作系统 ...

  9. js基础之javascript函数定义及种类-普通涵数-自执行函数-匿名函数

    普通函数 1.不带参数 function fucname(){ alert("hello"); } funcname() 2.带参数 function funcname(arg){ ...

  10. fetch 使用记录

    fetch api出来很多年了 ,由于兼容性问题之前一直没在项目中用到,最近做的项目只需兼容IE9+,把fetch引入了进来.目前用起来感觉挺好的,简洁. fetch 返回值是promise对象,对于 ...