HDU 1540 Tunnel Warfare (線段樹 / 求最大連續區間長度)

傳送門

題意: 抗日戰爭時期,華北平原廣大地區進行了廣泛的隧道戰爭。一般來說,通過隧道連接的村莊成一直線。除了兩端的兩個村外,每個村莊都與兩個相鄰的村直接相連。
入侵者經常對一些村莊發動襲擊,並摧毀其中的部分隧道。八路軍指揮官要求提供隧道和村莊的最新連接狀態。如果某些村莊被嚴重隔離,則必須立即恢復連接!

輸入:
輸入的第一行包含兩個正整數n和m(n,m≤50,000),表示村莊和事件的數量。接下來的m行中的每行都描述一個事件。
下面以不同的格式描述了三個不同的事件:

D x:第x個村莊被摧毀。
問:陸軍司令部要求第x個村莊包括其自身直接或間接聯繫的村莊數。
R:最後被摧毀的村莊被重建。

思路: 就是個常規線段樹題目,和acwing上的你能回答這些問題嗎做法原理相似。

代碼實現:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define null NULL
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int   mod = 1e9 + 7;
const int  N = 50005;

int n, m, x, tt;
char op[3];
stack<int> st;

struct node{
    int l, r;
    int lmax, rmax, tmax;
}tr[N << 2];

//區間合併
void pushdown(int u){
    tr[u].lmax = tr[u << 1].lmax;
    tr[u].rmax = tr[u << 1 | 1].rmax;

    if(tr[u << 1].lmax == tr[u << 1].r - tr[u << 1].l + 1) tr[u].lmax = tr[u << 1].lmax + tr[u << 1 | 1].lmax;
    if(tr[u << 1 | 1].rmax == tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) tr[u].rmax = tr[u << 1].rmax + tr[u << 1 | 1].lmax;

    tr[u].tmax = max(tr[u].rmax, tr[u].lmax);
    tr[u].tmax = max(tr[u].tmax, tr[u << 1].rmax + tr[u << 1 | 1].lmax);
}

void build(int u, int l, int r)
{
    int t = r - l + 1;
    tr[u] = {l, r, t, t, t};

    if(l == r) return;

    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
}

void modify(int u, int x, int v)
{
    if(tr[u].l == x && tr[u].r == x){
        tr[u].lmax = tr[u].rmax = tr[u].tmax = v;
        return;
    }

    int mid = tr[u].l + tr[u].r >> 1;
    if(x <= mid) modify(u << 1, x, v);
    else modify(u << 1 | 1, x, v);

    //主義在修改後將兩個區間合併
    pushdown(u);
}

int query(int u, int x)
{
    if(!tr[u].tmax) return 0;
    //在左端區間
    if(x < tr[u].l + tr[u].lmax)
        return tr[u].lmax;
    //在右端區間
    if(x > tr[u].r - tr[u].rmax)
        return tr[u].rmax;
    //在兩個區間的中間
    if(x > tr[u << 1].r - tr[u << 1].rmax && x < tr[u << 1 | 1].l + tr[u << 1 | 1].lmax)
        return tr[u << 1].rmax + tr[u << 1 | 1].lmax;

    int mid = tr[u].l + tr[u].r >> 1;
    if(x <= mid) return query(u << 1, x);
    else return query(u << 1 | 1, x);
}

int main()
{
    while(~scanf("%d%d", &n, &m)){
        while(!st.empty()) st.pop();
        build(1, 1, n);
        while(m --){
            scanf("%s", op);
            if(*op == 'D'){
                scanf("%d", &x);
                st.push(x);
                modify(1, x, 0);
            }
            else if(*op == 'Q'){
                scanf("%d", &x);
                printf("%d\n", query(1, x));
            }
            else if(!st.empty()){
                x = st.top(); st.pop();
                modify(1, x, 1);
            }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章