程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> hdu 1540 Tunnel Warfare (線段樹區間合並)

hdu 1540 Tunnel Warfare (線段樹區間合並)

編輯:關於C

題目鏈接: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 
#include 
#include 

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved