hdu--1540 Tunnel Warfare(线段树+区间合并)

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:

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.

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题意:在一条地道上有n个村庄,进行m次操作,D操作是摧毁村庄X,Q操作是询问村庄X能与多少村庄是连续的,R操作是重建上一个被摧毁的村庄。打印每次询问结果思路:首先建树,a[k].lazy为1说明[ a[k].l,a[k].r ]区间内村庄全部完好,a[k].lazy为0说明[ a[k].l,a[k].r ]区间内存在村庄被摧毁。用lm,rm和m分别表示从左端点往右数最大连续村庄数,从右端点往左数最大连续村庄数和区间内的最大连续村庄数。然后进行单点更新,有pushup对父节点进行更新。最后进行询问,用到的是二分的思想,分别对左子树和右子树进行统计。AC代码:

  1 #include <iostream>
  2 #include<cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 using namespace std;
  6 const int maxn=50000+10;
  7 int num[maxn];
  8 struct note
  9 {
 10     int l,r,lm,rm,m,lazy;
 11 }a[maxn<<2];
 12 int max(int a,int b)
 13 {
 14     if(a>b)
 15         return a;
 16     else
 17         return b;
 18 }
 19 int build(int l,int r,int k)
 20 {
 21     a[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m=(r-l+1);
 22     a[k].lazy=1;
 23     if(l==r)
 24         return 0;
 25         int mid=(l+r)/2;
 26     build(l,mid,k*2);
 27     build(mid+1,r,k*2+1);
 28     return 0;
 29 }
 30 int pushup(int k)
 31 {
 32     if(a[k*2].rm<(a[k*2].r-a[k*2].l+1))
 33         a[k].lm=a[k*2].lm;
 34     else
 35         a[k].lm=a[k*2].rm+a[k*2+1].lm;
 36     if(a[k*2+1].lm<a[k*2+1].r-a[k*2+1].l+1)
 37         a[k].rm=a[k*2+1].rm;
 38     else
 39         a[k].rm=a[k*2+1].lm+a[k*2].rm;
 40     a[k].m=max(max(a[k].lm,a[k].rm),a[k*2+1].lm+a[k*2].rm);
 41     if(a[k].m==a[k].r-a[k].l+1)
 42         a[k].lazy=1;
 43     else
 44         a[k].lazy=0;
 45     return 0;
 46 }
 47 int ins(int d,int n,int k)
 48 {
 49     if(d==a[k].l&&a[k].r==a[k].l)
 50     {
 51         a[k].rm=a[k].lm=a[k].m=n;
 52         a[k].lazy=n;
 53         return 0;
 54     }
 55     //pushdown(k)
 56     if(d<=a[k*2].r)
 57         ins(d,n,k*2);
 58     if(d>a[k*2].r)
 59         ins(d,n,k*2+1);
 60     pushup(k);
 61     return 0;
 62 }
 63 int sea(int n,int k)
 64 {
 65     if(a[k].lazy==1||a[k].r==a[k].l)
 66     {
 67         return a[k].m;
 68     }
 69     if(n<=a[k*2].r)
 70     {
 71         if(n>=a[k*2].r-a[k*2].rm+1)
 72             return sea(n,k*2)+sea(a[k*2+1].l,k*2+1);
 73         else
 74             return sea(n,k*2);
 75     }
 76     else
 77     {
 78         if(n<=a[k*2+1].lm+a[k*2+1].l-1)
 79             return sea(n,k*2+1)+sea(a[k*2].r,k*2);
 80         else
 81             return sea(n,k*2+1);
 82     }
 83     return 0;
 84 }
 85 int main()
 86 {
 87     int n,m,k,b;
 88     while(~scanf("%d%d",&n,&m))
 89     {
 90         build(1,n,1);
 91         k=0;
 92         while(m--)
 93         {
 94             char s[5];
 95             scanf("%s",s);
 96             if(s[0]==‘D‘)
 97             {
 98                 scanf("%d",&num[k]);
 99                 ins(num[k],0,1);
100                 k++;
101             }
102             else if(s[0]==‘Q‘)
103             {
104                 scanf("%d",&b);
105                 printf("%d\n",sea(b,1));
106             }
107             else
108             {
109                 k--;
110                 ins(num[k],1,1);
111             }
112         }
113     }
114
115     return 0;
116 }

时间: 2024-08-04 02:43:24

hdu--1540 Tunnel Warfare(线段树+区间合并)的相关文章

HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定 所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间. 注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间.反之同理. 查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询. 查询

HDU 1540 Tunnel Warfare(线段树 区间合并 最大连续区间)

题意  有n个连在一起的地道  接下来有m个操作  D x 炸掉x号地道  炸掉后x所在的区间就不连续了  Q x 查询输出包括x的最大连续区间长度   R修复最后一个被炸的地道  注意输入R时可能并没有需要修复的地道 线段树的区间合并问题  线段树要维护3个信息 len  对应区间的最大连续长度 ll  对应区间最左端的一段连续长度 lr  对应区间最右端的一段连续长度 那么双亲节点的这些信息和孩子节点有什么关系呢  容易发现 双亲的len 是 左孩子的len 右孩子的len  左孩子的右端长

hdu 1540 Tunnel Warfare 线段树 区间合并

题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2.如果在坐儿子的右区间或右儿子的左区间,返回这两个区间长度和 3.继续递归 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std;

HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)

Problem 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 vill

hdu 1540 Tunnel Warfare(线段树)

题目链接:hdu 1540 Tunnel Warfare 题目大意:有连续的N个城镇,三种操作: D x:第x城镇被破坏 Q x:插叙第x城镇所在联通块有多少个城镇没有被破坏 R:修复最后一个被破坏的城镇 解题思路:线段树区间合并,每个城镇看成一个叶子节点,用一个vector记录破坏顺序.对于查询来说,每次只要判断是否在mid?R[lson(u)],mid+L[rson(u)]之间即可,否则即递归查询左右子树. #include <cstdio> #include <cstring>

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

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene

HDU 1540 Tunnel Warfare 线段树:单点更新,区间合并

Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast

HDU 1540 POJ 2892 线段树区间合并

给出N个点,M次操作,N个点开始在一条线上链式相连 D操作  把某点删除掉 Q操作  询问某点一共可以连接多少个点 R操作  把上一次删除的点还原 线段树处理区间合并 分别记录每个区间的左端连续最长和右端连续最长 #include "stdio.h" #include "string.h" struct node { int l,r,lx,rx,x; }data[200010]; int mark[50100]; int Max(int a,int b) { if

Tunnel Warfare 线段树 区间合并|最大最小值

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 con

HDU 1540 Tunnel Warfare (线段树或set水过)

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段树的话,就维护三个值分别是左端点连续右端点连续,全连续的最长的区别,然后用线段树维护就好. 代码如下: set过: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #includ