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!

InputThe 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.

OutputOutput 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
  5.  
  6. 表示初识线段树,对区间维护前缀后缀表示无力,不过最后还是卡过去了。注释应该比较清晰。。。
    清楚线段树的结构,和区间前缀后缀 左孩子右孩子的拼接情况。。。说不清楚。。。。
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <stack>
  14. using namespace std;
  15. #define FO freopen("in.txt","r",stdin);
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  18. #define pb push_back
  19. #define mp make_pair
  20. #define all(x) (x).begin(),(x).end()
  21. #define fi first
  22. #define se second
  23. #define SZ(x) ((int)(x).size())
  24. #define debug(x) cout << "&&" << x << "&&" << endl;
  25. #define lowbit(x) (x&-x)
  26. #define mem(a,b) memset(a, b, sizeof(a));
  27. typedef vector<int> VI;
  28. typedef long long ll;
  29. typedef pair<int,int> PII;
  30. const ll mod=;
  31. const int inf = 0x3f3f3f3f;
  32. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  33. ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
  34. //head
  35.  
  36. const int maxn=;
  37. int pre[maxn<<],suf[maxn<<],maxx[maxn<<],n,m,st[maxn];//维护区间前缀1,区间后缀1,st模拟栈
  38.  
  39. void pushup(int rt,int x) {
  40. pre[rt]=pre[rt<<];//rt的前缀1就是左孩子前缀1
  41. suf[rt]=suf[rt<<|];//rt后缀1就是右孩子后缀1
  42. maxx[rt]=max(maxx[rt<<],maxx[rt<<|]);//rt的最大两种情况
  43. maxx[rt]=max(maxx[rt],pre[rt<<|]+suf[rt<<]);
  44. if(suf[rt<<|]==(x>>)) suf[rt]+=suf[rt<<];// 如果右孩子的后缀全是1,可以拼接左孩子的后缀
  45. if(pre[rt<<]==(x-(x>>))) pre[rt]+=pre[rt<<|];//如果左孩子前缀全是1 可以拼接右孩子的前缀
  46. }
  47.  
  48. void build(int rt,int L,int R) {
  49. pre[rt]=suf[rt]=maxx[rt]=R-L+;//这里置为R-L+1 和 常规用pushup效果一样
  50. int mid=(L+R)>>;
  51. if(L!=R) {
  52. build(rt<<,L,mid);
  53. build(rt<<|,mid+,R);
  54. }
  55. }
  56.  
  57. void updata(int rt,int L,int R,int pos,int val) {
  58. if(L==R) {//单点修改
  59. pre[rt]=suf[rt]=maxx[rt]=val;
  60. return;
  61. }
  62. int mid=(L+R)>>;
  63. if(pos<=mid) updata(rt<<,L,mid,pos,val);
  64. else updata(rt<<|,mid+,R,pos,val);
  65. pushup(rt,R-L+);
  66. }
  67.  
  68. int query(int rt,int L,int R,int pos) {
  69. if(L==R||maxx[rt]==||maxx[rt]==(R-L+))//断点 || maxx为0 || maxx最大
  70. return maxx[rt];
  71. int mid=(L+R)>>;
  72. if(pos<=mid) {//如果在左子树
  73. if(pos>=mid-suf[rt<<]+) return pre[rt<<|]+suf[rt<<];//如果大于左孩子的后缀1 == 包含两部分
  74. else return query(rt<<,L,mid,pos); //否则搜左子树
  75. } else {//如果在右子树
  76. if(pos<=mid++pre[rt<<|]-) return pre[rt<<|]+suf[rt<<];//如果小于右孩子的前缀1 == 包含两部分
  77. else return query(rt<<|,mid+,R,pos);//否则右子树
  78. }
  79. }
  80.  
  81. int main() {
  82. while(~scanf("%d%d",&n,&m)) {
  83. int cur=;
  84. build(,,n);
  85. int pos;
  86. while(m--) {
  87. char s[];
  88. scanf("%s",s);
  89. if(s[]=='D') {
  90. scanf("%d",&pos);
  91. st[cur++]=pos;
  92. updata(,,n,pos,);
  93. } else if(s[]=='Q') {
  94. scanf("%d",&pos);
  95. printf("%d\n",query(,,n,pos));
  96. } else {
  97. pos=st[--cur];
  98. updata(,,n,pos,);
  99. }
  100. }
  101. }
  102. }
  1.  

kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)的更多相关文章

  1. kuangbin专题七 HDU1166 敌兵布阵 (线段树或树状数组)

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  2. I - Tunnel Warfare HDU - 1540 线段树最大连续区间

    题意  :一段区间  操作1 切断点 操作2 恢复最近切断的一个点 操作3 单点查询该点所在最大连续区间 思路:  主要是push_up :  设区间x 为母区间  x<<1 ,x< ...

  3. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  4. hdu1540 Tunnel Warfare

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

  5. HDU--1540 Tunnel Warfare(线段树区间更新)

    题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...

  6. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  7. hdu1540 Tunnel Warfare【线段树】

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  8. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

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

  9. HDU-1540 Tunnel Warfare(区间连续点长度)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

随机推荐

  1. docker 笔记 (7) 限制容器

    内存 -m 或 --memory:设置内存的使用限额,例如 100M, 2G.--memory-swap:设置 内存+swap 的使用限额.--vm 1:启动 1 个内存工作线程.--vm-bytes ...

  2. oracle 在xml中批量插入,批量修改及多组条件查询

    最近公司用ibatis开发项目,本来可以用存储过程处理批量插入,批量修改及多组条件查询:但由于使用模块相对较小,暂时就在xml中配置,以前没有在xml做过类似处理,有必要记录一下:好了,代码如下: & ...

  3. (修改)oracle11g监听多台主机配置,用pl/sql连接操作多个数据库详解

    很多朋友在开发项目中并不是每个人用一个数据库,而是有单独的一台主机作为开发的数据库服务器,这样,就需要我们的开发人员去连接它. 首先是进入oracle的 Net  Mananger:

  4. MySQL 删除字段数据某关键字后的所有数据

    ),'开发商') WHERE Compay LIKE '%开发商%'; sql附上

  5. android wifi框架

    ---恢复内容开始--- frameworks/base/services/java/com/android/server/wifi 中的ReadMe文件 WifiService: Implement ...

  6. 支撑矢量机SVM

    1.线性SVM 首先,回顾一下SVM问题的定义,如下: 线性约束很烦,不方便优化,是否有一种方法可以将线性约束放到优化问题本身,这样就可以无拘无束的优化,而不用考虑线性约束了.其对应的拉格朗日对偶形式 ...

  7. dos 下bat 常用符号

    1.@一般在它之后紧跟一条命令或一条语句,则此命令或语句本身在执行的时候不会显示在屏幕上.请把下面的代码保存为test.cmd文件,然后运行,比较一下两条echo语句在屏幕上的输出差异:    ech ...

  8. IFC—IfcProduct实体继承框架

  9. Flash of Unstyled Content (FOUC)

    在这次的产品发布中,客户发现了一个问题,就是在Firefox浏览器中,页面在加载的时候,出现没有样式的内容一闪而过的现象.其实,在测试过程中,我们也看到了类似的问题,但是并没有意识到这是一个问题,以为 ...

  10. ARC059F

    传送门 分析 见ptx大爷博客 代码 #include<iostream> #include<cstdio> #include<cstring> #include& ...