POJ 2892-Tunnel Warfare(线段树单点更新-炸毁修复城市隧道)

向右看齐 2022-06-17 04:18 232阅读 0赞

Tunnel Warfare














Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8361   Accepted: 3453

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

  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

POJ Monthly—2006.07.30, updog

题目意思:

N个城市相互连通,D X表示炸毁城市X与其他城市的连接,R X表示恢复上一个最后炸毁的城市的连接,Q X表示查询城市X与邻近 有多少个连续的其他城市。

解题思路:

线段树,单点更新。

节点保存llen和rlen表示该节点左右两侧连续节点的个数。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<cstring>
  7. #include<map>
  8. #include<algorithm>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define INF 0xfffffff
  13. #define MAXN 500005
  14. int sta[MAXN];//修复栈
  15. struct Node //树
  16. {
  17. int l,r;//左右节点
  18. int llen,rlen;//区间内的城市情况,连续个1的个数
  19. };
  20. Node tree[MAXN<<2];
  21. void PushUP(int i)//向上更新父节点的值
  22. {
  23. tree[i].llen=tree[2*i].llen ;
  24. tree[i].rlen=tree[2*i+1].rlen ;
  25. if(tree[2*i].llen==tree[2*i].r-tree[2*i].l+1)//区间连续
  26. tree[i].llen+=tree[2*i+1].llen;
  27. if(tree[2*i+1].rlen==tree[2*i+1].r-tree[2*i+1].l+1)
  28. tree[i].rlen+=tree[2*i].rlen;
  29. }
  30. void BuildTree(int i, int l, int r)//建树
  31. {
  32. tree[i].l=l;
  33. tree[i].r=r;
  34. if(l==r)
  35. {
  36. tree[i].llen=tree[i].rlen=1;//初始状态都是连通的
  37. return ;
  38. }
  39. BuildTree(2*i,l,(l+r)/2);
  40. BuildTree(2*i+1,(l+r)/2+1,r);
  41. PushUP(i);//继续更新
  42. }
  43. void Update(int i,int l,int r,int t,int op)
  44. {
  45. if(tree[i].l==tree[i].r)
  46. {
  47. tree[i].llen=tree[i].rlen=op;
  48. return ;
  49. }
  50. int mid=(tree[i].l+tree[i].r)/2;
  51. if(t<=mid)
  52. Update(2*i,l,r,t,op);
  53. else
  54. Update(2*i+1,l,r,t,op);
  55. PushUP(i);//修改完后,仍然要向上修正父节点的值
  56. }
  57. int Query(int i,int l,int r,int t)
  58. {
  59. if(tree[i].l==tree[i].r)
  60. return tree[i].llen;
  61. int mid=(tree[i].l+tree[i].r)/2;
  62. if(t<=mid)
  63. {
  64. if(tree[2*i].r-tree[2*i].rlen+1<=t)//t被包含在左子树连续区间内
  65. return tree[2*i].rlen+tree[2*i+1].llen;
  66. Query(2*i,l,r,t);
  67. }
  68. else
  69. {
  70. if(tree[2*i+1].llen+tree[2*i+1].l-1>=t)//t被包含在右子树连续区间内
  71. return tree[2*i].rlen+tree[2*i+1].llen;
  72. Query(2*i+1,l,r,t);
  73. }
  74. }
  75. int main()
  76. {
  77. #ifdef ONLINE_JUDGE
  78. #else
  79. freopen("G:/cbx/read.txt","r",stdin);
  80. //freopen("G:/cbx/out.txt","w",stdout);
  81. #endif
  82. int n,m;
  83. while(~scanf("%d%d\n",&n,&m))
  84. {
  85. BuildTree(1,1,n);//建树
  86. int cnt=0;
  87. for(int j=0; j<m; ++j)
  88. {
  89. char ch;
  90. cin>>ch;
  91. if(ch=='D')//摧毁
  92. {
  93. int t;
  94. scanf("%d\n",&t);
  95. sta[cnt++]=t;
  96. Update(1,1,n,t,0);
  97. }
  98. else if(ch=='Q')//查询
  99. {
  100. int t;
  101. scanf("%d\n",&t);
  102. printf("%d\n",Query(1,1,n,t));
  103. }
  104. else if(ch=='R')//修复
  105. {
  106. int t=sta[--cnt];//从栈中取出最后炸毁的序列号
  107. Update(1,1,n,t,1);
  108. }
  109. }
  110. }
  111. return 0;
  112. }

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读