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

hdu1540 Tunnel Warfare(線段樹)

編輯:關於C語言

hdu1540 Tunnel Warfare(線段樹)


Tunnel Warfare

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

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
題意:D表示破壞地道,R表示修復上一個被破壞的地道,Q表示查詢包括x的最長區間。 分析:用三個變量記錄左邊連續區間,右邊連續區間和最大連續區間;不知道怎麼說,看代碼吧。如果還看不懂就看這裡吧,寫的挺詳細的詳解 Ps:寫代碼一定要仔細啊,因為一個符號問題我查了近兩天,看的我眼睛那個花啊抓狂
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
const int MAXN = 50000+10;

struct node
{
    int l,r;
    int ls,rs,ms;//ls為左邊最大連續區間,rs為右,ms為區間內最大連續長度
}t[MAXN<<2];
int n,m;
int s[MAXN],top;//模擬棧

void build(int x, int y, int num)
{
    t[num].l = x;
    t[num].r = y;
    t[num].ls = t[num].rs = t[num].ms = y-x+1;
    if(x == y) return ;
    int mid = (x+y)>>1;
    build(x, mid, num<<1);
    build(mid+1, y, num<<1|1);
}

void update(int x, int num, int ok)
{
    if(t[num].l == t[num].r)
    {
        if(ok) //修復
            t[num].ls = t[num].rs = t[num].ms = 1;
        else //破壞
            t[num].ls = t[num].rs = t[num].ms = 0;
        return ;
    }
    int mid = (t[num].l+t[num].r)>>1;
    if(x <= mid)
        update(x, num<<1, ok);
    else
        update(x, num<<1|1, ok);
    t[num].ls = t[num<<1].ls;
    t[num].rs = t[num<<1|1].rs;
    t[num].ms = max(max(t[num<<1].ms, t[num<<1|1].ms), t[num<<1].rs+t[num<<1|1].ls);
    if(t[num<<1].ls == t[num<<1].r-t[num<<1].l+1)//如果左子樹滿了,父親左區間要加上右孩子的左區間
        t[num].ls += t[num<<1|1].ls;
    if(t[num<<1|1].rs == t[num<<1|1].r-t[num<<1|1].l+1)//同理
        t[num].rs += t[num<<1].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)>>1;
    if(x <= mid)
    {
        if(x >= t[num<<1].r-t[num<<1].rs+1)//因為x<=mid,看左子樹,t[num<<1].r-t[num<<1].rs+1代表左子樹右邊連續區間的左邊界值,如果t在左子樹的右區間內,則要看右子樹的左區間有多長並返回
            return query(x, num<<1)+query(mid+1, num<<1|1);
        else
            return query(x, num<<1);//如果不在左子樹的右邊界區間內,則只需要看左子樹
    }
    else
    {
        if(x <= t[num<<1|1].l+t[num<<1|1].ls-1)//同理
            return query(x, num<<1|1)+query(mid, num<<1);
        else
            return query(x, num<<1|1);
    }
}

int main()
{
    char ch[2];//其實這裡我也不懂為什麼用單個的字符就不行,會RE
    int x;
    while(scanf("%d%d",&n,&m)==2)
    {
        top = 0;
        build(1, n, 1);
        while(m--)
        {
            //getchar();
            scanf("%s",ch);
            if(ch[0] == 'D')
            {
                scanf("%d",&x);
                s[top++] = x;
                update(x, 1, 0);
            }
            else if(ch[0] == 'Q')
            {
                scanf("%d",&x);
                printf("%d\n",query(x, 1));
            }
            else
            {
                if(top > 0)
                {
                    x = s[--top];
                    update(x, 1, 1);
                }
            }
        }
    }
    return 0;
}


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