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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6065    Accepted Submission(s): 2344


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

Source
POJ Monthly
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1542  1255  1698  1828  2871 

题目大意:输入n,m:n表示有n个城市相连,m表示下面有m行指令。D X表示破坏X这个城市,R表示修复最近一次破坏的城市,Q X表示查询以X点为中心的整体连续中最大的连续个数并输出。
根据题意画出下图,方便理解:

解题思路:首先是建树,在结构体里定义ls记录该区间左端点的最大连续个数,rs记录区间右端点开始的最大连续个数,ms表示该区间最大的连续个数。
代码中的注释解释的比较清楚。

详见代码。
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

struct node
{
    int l,r;
    int ls,rs,ms;//分别表示左边最大连续,右边最大连续,以及整个区间内的最大连续长度
} s[50050*3];

int n,m;
int op[50010];

void InitTree(int l,int r,int k)
{
    s[k].l=l;
    s[k].r=r;
    s[k].ls=s[k].rs=s[k].ms=r-l+1;//最开始的时候全都是连着的。所以长度为r-l+1
    if (l==r)
        return ;
    int mid=(l+r)/2;
    InitTree(l,mid,k*2);
    InitTree(mid+1,r,k*2+1);
}

void UpdataTree(int x,int flag,int k)//x表示修复或者破坏的数字,flag用来标记是破坏还是修复
{
    if (s[k].l==s[k].r)
    {
        if (flag==1)
            s[k].ls=s[k].rs=s[k].ms=1;//修复
        else
            s[k].ls=s[k].rs=s[k].ms=0;//破坏
        return ;
    }
    int mid=(s[k].l+s[k].r)/2;
    if (x<=mid)
        UpdataTree(x,flag,2*k);
    else
        UpdataTree(x,flag,2*k+1);
    if(s[2*k].ls == s[2*k].r-s[2*k].l+1)//左区间的左连续=左子树的长度,就说名左区间的数全部连续,(左子树区间满了),整个区间的左区间就应该加上有区间的左部分。
        s[k].ls =s[2*k].ls+s[2*k+1].ls;
    else
        s[k].ls=s[2*k].ls;
    if(s[2*k+1].rs==s[2*k+1].r-s[2*k+1].l+1)//同理
        s[k].rs=s[2*k+1].rs+s[2*k].rs;
    else
        s[k].rs=s[2*k+1].rs;
    s[k].ms=max(max(s[2*k].ms,s[2*k+1].ms),s[2*k].rs+s[2*k+1].ls);//整个区间内的最大连续应为:左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中取最大
}

int SearchTree(int x,int k)
{
    if(s[k].l==s[k].r||s[k].ms==0||s[k].ms==s[k].r-s[k].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
        return s[k].ms;
    int mid=(s[k].l+s[k].r)/2;
    if (x<=mid)
    {
        if (x>=s[2*k].r-s[2*k].rs+1)//判断当前这个数是否在左区间的右连续中,其中s[2*k].r-s[2*k].rs+1代表左子树右边连续区间的左边界值,即有连续区间的起点
            return s[2*k].rs+s[2*k+1].ls;//也可以SearchTree(x,2*k)+SearchTree(mid+1,2*k+1);
        else
            return SearchTree(x,2*k);
    }
    else
    {
        if (x<=s[2*k+1].l+s[2*k+1].ls-1)//判断当前这个数是否在左区间的右连续中,其中s[2*k].r-s[2*k].rs+1代表左子树右边连续区间的左边界值,即有连续区间的起点
            return s[2*k].rs+s[2*k+1].ls;//这种方法SearchTree(x,2*k+1)+SearchTree(mid,2*k);也是可以的,但是比较浪费时间
        else
            return SearchTree(x,2*k+1);
    }
}

int main()
{
    int x;
    char ch[2];
    while (~scanf ("%d%d",&n,&m))
    {
        int top=0;
        InitTree(1,n,1);
        while (m--)
        {
            scanf("%s",ch);
            if (ch[0]=='D')
            {
                scanf("%d",&x);
                op[top++]=x;
                UpdataTree(x,0,1);
            }
            else if (ch[0]=='Q')
            {
                scanf("%d",&x);
                printf ("%d\n",SearchTree(x,1));
            }
            else
            {
                if (x>0)
                {
                    x=op[--top];
                    UpdataTree(x,1,1);
                }
            }
        }
    }
    return 0;
}



 

你可能感兴趣的:(算法,C语言,合并)