题目链接

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 (nm ≤ 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

Source

 
题意:有n个村庄编号为1,2,3...n 它们按照序号一一相连,现在有m次操作,有以下几种操作:
        1、D  x  表示将x号村庄摧毁。
        2、Q  x  表示查询x村庄能到达的村庄数(包括x村庄)。
        3、R      表示修复最近一个被摧毁的村庄。
        每次查询输出一个值。
 
思路:线段树单点更新、区间合并,用栈存储被摧毁的村庄号。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn=;
stack<int> s;
struct Node{
int l,r,m;
}tr[*maxn]; void build(int i,int l,int r)
{
tr[i].l=tr[i].r=tr[i].m=r-l+;
if(l==r) return;
int mid=(l+r)/;
build(*i,l,mid);
build(*i+,mid+,r);
} void update(int i,int l,int r,int x,int y)
{
if(l==r)
{
tr[i].l=tr[i].r=tr[i].m=y;
return;
}
int mid=(l+r)/;
if(x<=mid) update(*i,l,mid,x,y);
else update(*i+,mid+,r,x,y); if(tr[*i].m==mid-l+) tr[i].l=tr[*i].m+tr[*i+].l;
else tr[i].l=tr[*i].l;
if(tr[*i+].m==r-mid) tr[i].r=tr[*i+].m+tr[*i].r;
else tr[i].r=tr[*i+].r;
tr[i].m=max(max(tr[*i].m,tr[*i+].m),tr[*i].r+tr[*i+].l);
} int query(int i,int l,int r,int x)
{
int sum=;
if(l==r) return tr[i].m;
if(r-l+==tr[i].m) return tr[i].m;
int mid=(l+r)/;
if(x<=mid){
if(mid-tr[*i].r+<=x)
return tr[*i].r+tr[*i+].l;
else return query(*i,l,mid,x);
}
else {
if(tr[*i+].l+mid>=x)
return tr[*i].r+tr[*i+].l;
else return query(*i+,mid+,r,x);
}
} int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&m);
build(,,n);
int x;
char str[];
while(!s.empty()) s.pop();
while(m--)
{
scanf("%s",str);
if(str[]=='D')
{
scanf("%d",&x);
s.push(x);
update(,,n,x,);
}
else if(str[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(,,n,x));
}
else
{
update(,,n,s.top(),);
s.pop();
}
}
}
return ;
}

poj 2892---Tunnel Warfare(线段树单点更新、区间合并)的更多相关文章

  1. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  2. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. hdu1540之线段树单点更新+区间合并

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  5. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  6. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  9. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

随机推荐

  1. 关于多字节字符入库失败处理(所谓的Emji),该处理是舍弃特殊字符

    具体处理方法及样例如下: /** * 屏蔽超过三个字节以上的字符 * @param strByte * @return */ public static String filterUtf8(byte[ ...

  2. KB奇遇记(2):缘起

    最早听到这家公司的名字,大概还是在好几年前. 正是2012年,之前的在一起灿坤待过的同事LY在这家公司当高层,正好公司规模大了,要上ERP项目.苦于公司没有这方面的人才,而内部IT又太差劲支撑不起来. ...

  3. C++ 头文件系列(map)

    简介 该头文件包含两个概念相似的容器----map.multimap. 而这两个容器反映的概念就是 映射. 这两个容器 相同 的属性有: 关联性 映射 动态增长 键(Key)唯一性 这两个不相同的属性 ...

  4. ubuntu 笔记一

    注:ubuntu14.04 64位 1.刚安装的ubuntu无法在终端使用su 原因:root没有默认密码,需要手动设定. 解决方法:以具有sudo权限的用户登录 给root用户设置密码:打开一个te ...

  5. [2017.02.07] Lua入门学习记录

    #!/home/auss/Projects/Qt/annotated/lua -- 这是第一次系统学习Lua语言 --[[ 参考资料: 1. [Lua简明教程](http://coolshell.cn ...

  6. iOS 即时通讯 + 仿微信聊天框架 + 源码

    这些你造吗? 即时通讯(IM),在IOS这片江湖里面已经算是一个老者了,我这小旋风也是在很早以前巡山的时候,就知道有即时通讯这个妖怪,以前也多多少少接触过一些,在造APP的时候用过,哎呀,说着说着就感 ...

  7. DB2 表空间监控

    默认DB2 缓冲池信息监控是OFF, 需要开启(DB2表空间是由缓冲池分配的) CollBufferpool : ============ The CollBufferpool collector c ...

  8. Arduino入门学习

    一直听到许多做物联网.智能家居的控制器使用的是Arduino,从师兄那里拿到了一块Arduino开发板,进行了一下午的学习,感觉这个适合小孩子们玩:) 废话少说,总结一下,便于以后可能会用得到.我主要 ...

  9. JVM垃圾收集(GC)算法

    判断对象是否已死 1. 引用计数算法 给对象中添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失败时,计数器值就减1:任何时刻计数器为0的对象就是不能再被使用的. 主流的Java虚拟机 ...

  10. [Spark] - HashPartitioner & RangePartitioner 区别

    Spark RDD的宽依赖中存在Shuffle过程,Spark的Shuffle过程同MapReduce,也依赖于Partitioner数据分区器,Partitioner类的代码依赖结构主要如下所示: ...