天天看點

hdu1540 Tunnel Warfare(線段樹)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:寫代碼一定要仔細啊,因為一個符号問題我查了近兩天,看的我眼睛那個花啊

hdu1540 Tunnel Warfare(線段樹)Tunnel Warfare
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
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;
}