Tunnel Warfare HDU - 1540 (segment tree, interval merger)

Meaning of the questions: n a house there, at first are intact, there are m operations, can destroy i numbers houses, to rebuild houses destroyed in the last, i can query the number of houses and a few houses directly or indirectly connected.

Solution: 2 Hypothesis 1 3 4 5 6 7 8 3 and 7 have been destroyed, and now query 5, then that is 1--5 most right is the destruction of 3,5 - 8 leftmost destroyed is 7, the answer is 7--3--1 = 3, of course, if just a query that has destroyed so do not --1, so the need to maintain each node closest to destroy it about two points

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<stack>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>

using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long LL;
const int MAXN = 5e4 + 5;
const int mod = 998244353;

int stl[MAXN << 2],str[MAXN << 2];
int n;

void pushup(int o) {
    stl[o] = max(stl[o << 1], stl[o << 1 | 1]);
    str[o] = min(str[o << 1], str[o << 1 | 1]);
}
void build(int o,int l,int r) {
    if (l == r) {
        stl[o] = 0;
        str[o] = n + . 1 ; 
    } the else {
         int MID = (L + R & lt) >> . 1 ; 
        Build (O << . 1 , L, MID); 
        Build (O << . 1 | . 1 , MID + . 1 , R & lt); 
        a pushup (O); 
    } 
} 
void Update ( int O, int L, int R & lt, int POS, int ID) { // ID is used to determine damage or repair 
    IF (L == R & lt) {
         IF (ID) 
        { 
            STL [O]= max(stl[o],id);
            str[o] = min(str[o],id);
        }
        else {
            stl[o] = 0;
            str[o] = n + 1;
        }
    } else {
        int mid = (l + r) >> 1;
        if (pos <= mid) update(o << 1, l, mid, pos, id);
        else update(o << 1 | 1, mid + 1, r, pos, id);
        pushup(o);
    }
}
int query_r(int o,int l,int r,int ql,int qr) {
    if(ql <= l && r <= qr) return str[o];
    int mid = (l + r) >> 1;
    int ans = n + 1;
    if(ql <= mid) ans = min(ans,query_r(o << 1,l,mid,ql,qr));
    if(qr > mid) ans = min(ans,query_r(o << 1 | 1,mid + 1,r,ql,qr));
    return ans;
}
int query_l(int o,int l,int r,int ql,int qr) {
    if(ql <= l && r <= qr) return stl[o];
    int mid = (l + r) >> 1;
    int ans = 0;
    if(ql <= mid) ans = max(ans,query_l(o << 1,l,mid,ql,qr));
    if(qr > mid) ans = max(ans,query_l(o << 1 | 1,mid + 1,r,ql,qr));
    return ans;
}

int main() {
    int m;
    while (~scanf("%d %d", &n, &m)) {
        build(1, 1, n);
        char s[5];
        int c;
        stack<int> sta;
        while (m--) {
            scanf("%s", s);
            if (s[0] == 'D') {
                scanf("%d", &c);
                update(1, 1, n, c, c);
                sta.push(c);
            }
            else if(s[0] == 'R') {
                c = sta.top();
                sta.pop();
                update(1,1,n,c,0);
            }
            else {
                scanf("%d",&c);
                int ans = query_r(1,1,n,c,n) - query_l(1,1,n,1,c) - 1;
                //printf("%d %d", query_r(1,1,n,c,n) , query_l(1,1,n,1,c));

                printf("%d\n",max(0,ans));
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/smallhester/p/11330158.html