HDU 1540 Tunnel Warfare(线段树区间合并)

来源:互联网 发布:mac手游模拟器 编辑:程序博客网 时间:2024/06/08 09:11

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
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Hint

An illustration of the sample input:

      OOOOOOOD 3   OOXOOOOD 6   OOXOOXOD 5   OOXOXXOR     OOXOOXOR     OOXOOOO

题解:

比赛的时候没写出来。。。这还是区间合并比较基础的题目,我还是太弱了qaq,搜了篇博客学了下

代码:

#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<deque>using namespace std;stack<int>s;//由于要恢复上一个被破坏的所以要用栈struct node{    int l,r;    int ls,rs,ms;//ls表示区间从左起数的连续数,rs同理,ms是区间最大的连续数};node t[200005];void Build(int l,int r,int num){    t[num].l=l;    t[num].r=r;    t[num].ls=t[num].rs=t[num].ms=r-l+1;//初始化为满长度    if(l==r)        return;    int mid=(l+r)/2;    Build(l,mid,num*2);    Build(mid+1,r,num*2+1);}void update(int x,int num,int d){    if(t[num].l==t[num].r)    {        t[num].ls=t[num].rs=t[num].ms=d;        return;    }    int mid=(t[num].l+t[num].r)/2;    if(x<=mid)        update(x,num*2,d);    else        update(x,num*2+1,d);    t[num].ms=max(max(t[num*2].ms,t[num*2+1].ms),t[num*2].rs+t[num*2+1].ls);//区间最大值为左区间最大,右区间最大,和左区间右连续+右区间左连续之中的最大值    t[num].ls=t[num*2].ls;    t[num].rs=t[num*2+1].rs;    if(t[num].ls==t[num*2].r-t[num*2].l+1)//如果左子区间是满的,那么加上右子区间的左连续        t[num].ls+=t[num*2+1].ls;    if(t[num].rs==t[num*2+1].r-t[num*2+1].l+1)//如果右子区间是满的,那么加上左子区间的右连续        t[num].rs+=t[num*2].rs;}int query(int x,int num){    if(t[num].l==t[num].r||t[num].ms==0||t[num].ms==t[num].r-t[num].l+1)//如果访问到了该点或者区间全满或者全空都直接退出访问        return t[num].ms;    int mid=(t[num].l+t[num].r)/2;    if(x<=mid)    {        if(x>=t[num*2].r-t[num*2].rs+1)//重点理解!!右边式子代表左子树右边连续区间的左边界值,如果x在左子树的右区间内,则要看右子树的左区间有多长并返回            return query(x,num*2)+query(mid+1,num*2+1);        else            return query(x,num*2);    }    else    {        if(x<=t[num*2+1].l+t[num*2+1].ls-1)//同理            return query(x,num*2+1)+query(mid,num*2);        else            return query(x,num*2+1);    }}int main(){    int i,j,n,m,d,x;    char c;    scanf("%d%d",&n,&m);    Build(1,n,1);    for(i=0;i<m;i++)    {        getchar();        scanf("%c",&c);        if(c=='D')        {            scanf("%d",&x);            s.push(x);            update(x,1,0);        }        else if(c=='R')        {            x=s.top();            s.pop();            update(x,1,1);        }        else        {            scanf("%d",&x);            printf("%d\n",query(x,1));        }    }return 0;}


原创粉丝点击