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. K-th Number POJ - 2104

    K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...

  2. JVM垃圾回收--年轻代、年老点和持久代(转)

      关键字约定 Young generation –>新生代 Tenured / Old Generation –>老年代 Perm Area –>永久代 年轻代: 所有新生成的对象 ...

  3. CSU-2007 Football Training Camp

    Football Training Camp 在一次足球联合训练中一共有n支队伍相互进行了若干场比赛. 对于每场比赛,赢了的队伍得3分,输了的队伍不得分,如果为平局则两支队伍各得1分. Input 输 ...

  4. 5.2 pandas 常用函数清单

    文件读取 df = pd.read_csv(path='file.csv') 参数:header=None 用默认列名,0,1,2,3... names=['A', 'B', 'C'...] 自定义列 ...

  5. MySQL之体系结构与存储实例

    定义数据库和实例 在数据库领域中有两个词很容易混淆,这就是“数据库”(database)和“实例”(instance).作为常见的数据库术语,这两个词的定义如下: 数据库:物理操作系统文件或其他形式文 ...

  6. Android输入法弹出时覆盖输入框问题

    本文来自网易云社区 作者:孙有军 当一个activity中含有输入框时,我们点击输入框,会弹出输入法界面,整个界面的变化效果与manifest中对应设置的android:windowSoftInput ...

  7. 整理的一些Android开发类免费视频课程

    1.Android实战淘宝网项目视频:http://edu.ibeifeng.com/view-index-id-248.html 2.Android滚动视差实现课程:http://edu.ibeif ...

  8. Mybatis使用-Error attempting to get column 'type' from result set. / '255' in column '4' is outside valid range for the datatype TINYINT.

    一.遇到的问题是这样的: [RemoteTestNG] detected TestNG version 6.9.10log4j: Parsing for [root] with value=[DEBU ...

  9. 设置CMD默认代码页为65001或936

    之前不知道怎么改的,CMD的代码页被默认设置成了65001   但我右击CMD标题,选择‘默认值’,显示默认却是936,但为何每次打开都是65001呢   上网找到设置默认值的方法 1 win键+R打 ...

  10. Leetcode 617.合并二叉树

    合并二叉树 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新 ...