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

题目的大概意思就是地道战。 D x 表示摧毁x点,R 表示修复最后一个被摧毁的点,Q x 表示查询包含x点的最大连续区间的长度是多少。
这题主要是维护状态比较麻烦。
对于每个节点,我们要除了储存它的左右边界,还要储存从区间左端点开始的最大连续区间长度(ls),从右端点开始的最大连续区间长度(rs),
以及当前区间内的最大的连续长度(ms)。例如0100111,ls=0,rs=3,ms=3.状态更新的细节写到代码注释里了。
代码如下:
  1 #include <bits/stdc++.h>
  2 #define lson rt<<1
  3 #define rson rt<<1|1
  4 using namespace std;
  5 #define M 50050
  6 struct segTree
  7 {
  8     int l,r;
  9     int ls,rs,ms;
 10 }tree[M<<2];
 11 void buildTree (int rt,int l,int r)
 12 {
 13     tree[rt].l=l,tree[rt].r=r;
 14     tree[rt].ls=tree[rt].rs=tree[rt].ms=r-l+1;
 15     if (l==r)
 16     return ;
 17     int mid=(l+r)>>1;
 18     buildTree(lson,l,mid);
 19     buildTree(rson,mid+1,r);
 20 }
 21 void upDate(int rt,int x,int val)
 22 {
 23     if (tree[rt].l==tree[rt].r)
 24     {
 25         if (val==1)
 26         tree[rt].ls=tree[rt].rs=tree[rt].ms=1;
 27         else
 28         tree[rt].ls=tree[rt].rs=tree[rt].ms=0;
 29         return ;
 30     }
 31     int mid=(tree[rt].l+tree[rt].r)>>1;
 32     if (x<=mid)
 33     upDate(lson,x,val);
 34     else
 35     upDate(rson,x,val);
 36 
 37     tree[rt].ls=tree[lson].ls;//父节点先继承左儿子的ls
 38     tree[rt].rs=tree[rson].rs;//父节点先继承右儿子的rs
 39 
 40     tree[rt].ms=max(tree[lson].ms,tree[rson].ms);
 41 tree[rt].ms=max(tree[rt].ms,tree[lson].rs+tree[rson].ls);
 42 /*
 43 父节点内当前的连续最大值是当前区间ls(1),rs(2),
 44 与左儿子的rs右儿子ls(两部分在中间拼起来,3)三者的最大值
 45 */
 46 if (tree[lson].ls==tree[lson].r-tree[lson].l+1)
 47 /*如果父节点的左儿子的ls等于左儿子的长度(满了),
 48 则父节点的ls在继承左儿子ls的基础上还要加上右儿子的ls
 49 例:1111 1101
 50     lson  rson
 51 父节点.ls=lson.ls(4)+rson.ls(2)=6;
 52 */
 53     tree[rt].ls+=tree[rson].ls;
 54 if (tree[rson].rs==tree[rson].r-tree[rson].l+1)
 55 //同理如果右儿子的rs满了父节点的rs还要加上左儿子的rs
 56     tree[rt].rs+=tree[lson].rs;
 57 }
 58 int query (int rt,int x)
 59 {
 60     if (tree[rt].l==tree[rt].r||tree[rt].ms==0||tree[rt].ms==tree[rt].r-tree[rt].l+1)
 61     {
 62         return tree[rt].ms;
 63         //当查询到叶子节点,或当前区间ms为0或区间是满的,return
 64     }
 65     int mid=(tree[rt].l+tree[rt].r)>>1;
 66     if (x<=mid)
 67     {
 68         if (x>=tree[lson].r-tree[lson].rs+1)
 69         /*   x如果在左儿子的右连续区间内
 70         lson 1111 1101 rson
 71                x
 72         答案等于左儿子内包含x的区间(4)+右儿子的ls长度(2)=6
 73          */
 74         return query(lson,x)+ tree[rson].ls;
 75         else//否则只需查询左儿子
 76         return query(lson,x);
 77     }
 78     else
 79     {
 80         if (x<=tree[rson].l+tree[rson].ls-1)//x在右儿子的左连续区间内
 81         return query(rson,x)+ tree[lson].rs;//答案等于右儿子内包含x的区间+左儿子rs
 82         else
 83         return query(rson,x);
 84     }
 85 }
 86 int s[M],top;//模拟一个栈,来处理最后一个被破坏的区间
 87 int main()
 88 {
 89     int m,n;
 90     char o[5];
 91     //freopen("de.txt","r",stdin);
 92     while (~scanf("%d%d",&n,&m))
 93     {
 94         top=0;
 95         buildTree(1,1,n);
 96         while (m--)
 97         {
 98             scanf("%s",o);
 99             int x;
100             if (o[0]=='D')
101             {
102                 scanf("%d",&x);
103                 s[top++]=x;
104                 upDate(1,x,0);
105             }
106             else if (o[0]=='Q')
107             {
108                 scanf("%d",&x);
109                 printf("%d\n",query(1,x));
110             }
111             else
112             {
113                 if (x>0)
114                 {
115                     x=s[--top];
116                     upDate(1,x,1);
117                 }
118             }
119         }
120     }
121     return 0;
122 }
123         

 

 
 
posted @ 2016-10-09 10:05  抓不住Jerry的Tom  阅读(269)  评论(0编辑  收藏  举报