题目链接

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

  1. 7 9
  2. D 3
  3. D 6
  4. D 5
  5. Q 4
  6. Q 5
  7. R
  8. Q 4
  9. R
  10. Q 4

Sample Output

  1. 1
  2. 0
  3. 2
  4. 4

Hint

An illustration of the sample input:

  1. OOOOOOO

  2. D 3 OOXOOOO

  3. D 6 OOXOOXO

  4. D 5 OOXOXXO

  5. R OOXOOXO

  6. R OOXOOOO

Source

 
题意:有n个村庄编号为1,2,3...n 它们按照序号一一相连,现在有m次操作,有以下几种操作:
        1、D  x  表示将x号村庄摧毁。
        2、Q  x  表示查询x村庄能到达的村庄数(包括x村庄)。
        3、R      表示修复最近一个被摧毁的村庄。
        每次查询输出一个值。
 
思路:线段树单点更新、区间合并,用栈存储被摧毁的村庄号。
 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <stack>
  6. using namespace std;
  7. const int maxn=;
  8. stack<int> s;
  9. struct Node{
  10. int l,r,m;
  11. }tr[*maxn];
  12.  
  13. void build(int i,int l,int r)
  14. {
  15. tr[i].l=tr[i].r=tr[i].m=r-l+;
  16. if(l==r) return;
  17. int mid=(l+r)/;
  18. build(*i,l,mid);
  19. build(*i+,mid+,r);
  20. }
  21.  
  22. void update(int i,int l,int r,int x,int y)
  23. {
  24. if(l==r)
  25. {
  26. tr[i].l=tr[i].r=tr[i].m=y;
  27. return;
  28. }
  29. int mid=(l+r)/;
  30. if(x<=mid) update(*i,l,mid,x,y);
  31. else update(*i+,mid+,r,x,y);
  32.  
  33. if(tr[*i].m==mid-l+) tr[i].l=tr[*i].m+tr[*i+].l;
  34. else tr[i].l=tr[*i].l;
  35. if(tr[*i+].m==r-mid) tr[i].r=tr[*i+].m+tr[*i].r;
  36. else tr[i].r=tr[*i+].r;
  37. tr[i].m=max(max(tr[*i].m,tr[*i+].m),tr[*i].r+tr[*i+].l);
  38. }
  39.  
  40. int query(int i,int l,int r,int x)
  41. {
  42. int sum=;
  43. if(l==r) return tr[i].m;
  44. if(r-l+==tr[i].m) return tr[i].m;
  45. int mid=(l+r)/;
  46. if(x<=mid){
  47. if(mid-tr[*i].r+<=x)
  48. return tr[*i].r+tr[*i+].l;
  49. else return query(*i,l,mid,x);
  50. }
  51. else {
  52. if(tr[*i+].l+mid>=x)
  53. return tr[*i].r+tr[*i+].l;
  54. else return query(*i+,mid+,r,x);
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. int n,m;
  61. while(scanf("%d",&n)!=EOF)
  62. {
  63. scanf("%d",&m);
  64. build(,,n);
  65. int x;
  66. char str[];
  67. while(!s.empty()) s.pop();
  68. while(m--)
  69. {
  70. scanf("%s",str);
  71. if(str[]=='D')
  72. {
  73. scanf("%d",&x);
  74. s.push(x);
  75. update(,,n,x,);
  76. }
  77. else if(str[]=='Q')
  78. {
  79. scanf("%d",&x);
  80. printf("%d\n",query(,,n,x));
  81. }
  82. else
  83. {
  84. update(,,n,s.top(),);
  85. s.pop();
  86. }
  87. }
  88. }
  89. return ;
  90. }

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. Word常用实用知识3

    纯手打,可能有错别字,使用的版本是office Word 2013 转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6322813.html,谢谢. 分页符分页 ...

  2. puppet来管理文件和软件包

    puppet来管理文件和软件包 1 exec的使用,可以运行shell命令 为配置文件添加配置,指示写了关键部分,其他配置省略没写 代码示例如下: [root@pup manifests]# cat ...

  3. 模仿qq界面实现(WTL)

    前面对于界面用哪一种我试过用duilib,但是老感觉和MFC差距有点多,终于发现WTL的库能够实现我的所有界面功能,几天的努力终于搞定界面的重写.还是见我的成果吧: 1登录界面: 2主界面: 3.主界 ...

  4. ubuntu linux 设置环境变量

    添加环境变量 1.添加临时变量 终端中输入: PATH="$PATH:yourpath" :yourpath是要添加的环境变量(即要添加目录的绝对路径,例:/home/myprog ...

  5. 在点击div中的p时,如何阻止事件冒泡?

    今天整理笔记,发现在学习javaScript的过程中,遇到过一个在当时看来很棘手的问题,现在特地总结一下,也希望能帮助到曾像我一样迷惘的初学者. 我还是以一个案例来说明问题,html代码如下: < ...

  6. 2017<java技术>预备作业计科冀浩然

    1.阅读邹欣老师的博客,谈谈你期望的师生关系是什么样的? 我期望的师生关系是相互融洽的,老师能够在上课的时候尽量多的教我们专业知识,可以尽量多和我们进行互动,课下能和我们如同朋友一般就可以了. 2.你 ...

  7. oracle数据库在mybatis中使用uuid

    <insert id="insert" parameterType="com.xxx.SystemDepartment">     <sele ...

  8. 【4N魔方阵】

    /* 4N魔方阵 */ #include<stdio.h> #include<stdlib.h> #define N 8 int main(void){ int i, j; ] ...

  9. Sqlserver 链接服务器和同义词

    在数据库的日常维护中,经常会遇到跨服务器的数据传输. 例如A服务器上的数据每天要从B服务器上去获取数据,然后插入到自己的服务器上.这种情况就要用到链接服务器了. 接下来,我就把我本机当作服务器A,17 ...

  10. node.js爬虫杭州房产销售及数据可视化

    现在年轻人到25岁+,总的要考虑买房结婚的问题,2016年的一波房价大涨,小伙伴们纷纷表示再也买不起上海的房产了,博主也得考虑考虑未来的发展了,思考了很久,决定去杭州工作.买房.定居.生活,之前去过很 ...