1. 程式人生 > >Tunnel Warfare 線段樹 區間合並|最大最小值

Tunnel Warfare 線段樹 區間合並|最大最小值

some nbsp except ren 包含 cst edi mman 簡單

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!

InputThe 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.
OutputOutput 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


#include <cstdio>//法一:最大值最小值法,這個就是每一個點,如果這個點沒有被摧毀,那就找到這個點最左邊的和最右邊的
#include <cstdlib>//最大-最小+1.這個就是這個最大連續長度。 #include <queue>//建樹,很簡單,主要就是query和update。 #include <algorithm>//這個地方的怎麽去找一個包含一個數的一個區間的最大最小值呢? #include <vector>//這個就是從上面往下面查詢的過程中,就去找,如果是找最大值就去max,最小值就取min #include <cstring>//這個要註意建樹,這個區間的最大值的意思是,小於等於這個數的最大的被炸了的村莊,這個就說明,開始最大值為0,因為沒有任何一個村莊被炸 #include <string>//區間的最小值,意思是大於等於這個數,被炸了的村莊的最小值,開始為n+1.因為沒有村莊被炸。 #include <iostream> #include <stack> #define inf 0x3f3f3f3f using namespace std; const int maxn = 1e5 + 100; struct node { int l, r; int mx, mn; }tree[maxn*4]; int n; void build(int id,int l,int r) { tree[id].l = l; tree[id].r = r; if(l==r) { tree[id].mn = n+1; tree[id].mx = 0; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); tree[id].mx = max(tree[id << 1].mx, tree[id << 1 | 1].mx); tree[id].mn = min(tree[id << 1].mn, tree[id << 1 | 1].mn); } void update_max(int id,int x,int z) { if(tree[id].l==tree[id].r) { tree[id].mx = z; return; } int mid = (tree[id].l + tree[id].r) >> 1; if(x<=mid) update_max(id << 1, x, z); if (x > mid) update_max(id << 1 | 1, x, z); tree[id].mx = max(tree[id << 1].mx, tree[id << 1 | 1].mx); } void update_min(int id,int x,int z) { if(tree[id].l==tree[id].r) { tree[id].mn = z; return; } int mid = (tree[id].l + tree[id].r) >> 1; if (x <= mid) update_min(id << 1, x, z); if (x > mid) update_min(id << 1 | 1, x, z); tree[id].mn = min(tree[id << 1].mn, tree[id << 1 | 1].mn); } int query_max(int id,int x,int y) { int ans = 0; if(x<=tree[id].l&&y>=tree[id].r) { return tree[id].mx; } int mid = (tree[id].l + tree[id].r) >> 1; if (x <= mid) ans = max(ans, query_max(id << 1, x, y)); if (y > mid) ans = max(ans, query_max(id << 1 | 1, x, y)); return ans; } int query_min(int id,int x,int y) { int ans = inf; if(x<=tree[id].l&&y>=tree[id].r) { return tree[id].mn; } int mid = (tree[id].l + tree[id].r) >> 1; if (x <= mid) ans = min(ans, query_min(id << 1, x, y)); if (y > mid) ans = min(ans, query_min(id << 1 | 1, x, y)); return ans; } int main() { int m, x; while(cin >> n >> m) { stack<int>sta; build(1, 1, n); while(m--) { char s[10]; scanf("%s", s); if(s[0]==D) { cin >> x; update_max(1, x, x); update_min(1, x, x); sta.push(x); } if(s[0]==R) { int y = sta.top(); sta.pop(); update_max(1, y, 0); update_min(1, y, n + 1); } if(s[0]==Q) { cin >> x; int L = query_min(1, x, n + 1); int R = query_max(1, 0, x); //printf("%d %d\n", L, R); if (L == R) printf("0\n"); else printf("%d\n", L - R - 1); } } } return 0; }

//法二:區間合並,這個應該更好懂一點,就是維護一下一個區間的前綴後綴長度
//這個更新應該比較簡單,
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 100;
struct node
{
    int l, r, len;
    int max_pre, max_last;
}tree[maxn*4];

void push_up(int id)
{
    tree[id].max_pre = tree[id << 1].max_pre;
    tree[id].max_last = tree[id << 1 | 1].max_last;
    if (tree[id << 1].max_pre == tree[id << 1].len)
    {
        tree[id].max_pre += tree[id << 1 | 1].max_pre;
    }
    if(tree[id<<1|1].max_last==tree[id<<1|1].len)
    {
        tree[id].max_last += tree[id << 1].max_last;
    }
}

void build(int id,int l,int r)
{
    tree[id].l = l;
    tree[id].r = r;
    tree[id].len = r - l + 1;
    if(l==r)
    {
        tree[id].max_pre = tree[id].max_last = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    push_up(id);
}

void update(int id,int x,int z)
{
    if(tree[id].l==tree[id].r)
    {
        tree[id].max_pre = z;
        tree[id].max_last = z;
        return;
    }
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (x <= mid) update(id << 1, x, z);
    else update(id << 1 | 1, x, z);
    push_up(id);
}

int query_pre(int id,int x,int y)
{
    int ans = 0, res = 0;
    if(x<=tree[id].l&&y>=tree[id].r)
    {
        //printf("tree[%d].max_pre=%d\n", id, tree[id].max_pre);
        return tree[id].max_pre;
    }
    //printf("id=%d x=%d y=%d\n", id, x, y);
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (x <= mid) ans=query_pre(id << 1, x, y);
    if (y > mid) res=query_pre(id << 1 | 1, x, y);
    //printf("id=%d ans=%d res=%d mid=%d\n",id, ans, res,mid);
    if (ans >= mid - x + 1)
    {
        //printf("tree[%d].max_pre=%d mid=%d x=%d\n",id, tree[id].max_pre, mid, x);
        ans += res;
    }
    return ans;
}

int query_last(int id,int x,int y)
{
    int ans = 0, res = 0;
    if (x <= tree[id].l&&y >= tree[id].r)
    {
        //printf("tree[%d].last=%d\n", id, tree[id].max_last);
        return tree[id].max_last;
    }
    //printf("id=%d x=%d y=%d\n", id, x, y);
    int mid = (tree[id].l + tree[id].r) >> 1;
    if (x <= mid) ans = query_last(id << 1, x, y);
    if (y > mid) res = query_last(id << 1 | 1, x, y);
    //printf("id=%d mid=%d ans=%d res=%d\n", id, mid,ans, res);
    if (res >= y-mid)
    {
        //printf("tree[%d].max_last=%d  mid=%d x=%d\n",id,tree[id].max_last, mid, x);
        res += ans;
    }
    return res;
}

int main()
{
    int n, m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        stack<int>sta;
        build(1, 1, n);
        while(m--)
        {
            char s[10];
            scanf("%s", s);
            if(s[0]==D)
            {
                int x;
                cin >> x;
                update(1, x, 0);
                sta.push(x);
            }
            if(s[0]==R)
            {
                int y = sta.top(); sta.pop();
                update(1, y, 1);
            }
            if(s[0]==Q)
            {
                int x;
                cin >> x;
                int ans = query_pre(1, x, n);
                ans += query_last(1, 1, x);
                if(ans) printf("%d\n", ans-1);
                else printf("0\n");
            }
        }
    }
    return 0;
}
/*
7 10
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
Q 3
 */








Tunnel Warfare 線段樹 區間合並|最大最小值