(I) hdu1540 (combined section) (segment tree) (h) poj3667 (merging section)

hdu 1540  Tunnel Warfare

As part you do not understand when reading this article, please ask in the comments section, or jump  tree line general introduction

 

Recommended read (segment tree) (h) poj3667 (range consolidation)  to acquire some knowledge of the range of the combined segment tree 

 

The title and the title (poj3667) is substantially the same, but it is to be noted that part of the requirements interrogation interval length comprises 0 to x

 

query (look at the code, and is pretty intuitive, easy to understand)

int Query ( int RT, int L, int R & lt, int X) {
     IF return MaxiLen [RT] (R & lt || MaxiLen L == [RT] || MaxiLen [RT]. 1 + L-R & lt ==!);
     / / find the interval containing x x || 0 || are all with full range of x is 1 
  // see for yourself what are the longest will return ... 0 maintenance interval within MaxiLen
int MID = L + r >> 1 ; IF (X <= mID) { IF (X> = mID-Last [the LS] + 1'd) return Last [the LS] + pre [the RS]; // interposed intermediate the else return Query (the LS, L, mID, X) ; // the LS } the else { IF (X <= mID pre + [the RS]) return Last [the LS] + pre [the RS]; // interposed intermediate the else return Query (the RS, mID +1,r,x); //RS } }

 

About very common to find x, where emphasis is sandwiched, the precursor to subsequent partial section view of the left and right; the special situation of the entire interval determining if (l == r || MaxiLen [rt] || MaxiLen [rt! ] == r-l + 1) can be combined to write three

 

Code

/*i.hdu1540*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int N=2e6+3;
int STK[N],HEAD=0;int n,m;
int pre[N<<2],last[N<<2],MaxiLen[N<<2];
#define LS (rt<<1)
#define RS (LS|1)

void build(int rt,int l,int r){
    pre[rt]=last[rt]=MaxiLen[rt]=r-l+1;
    if(l==r)return;
    int mid=l+r>>1;
    build(LS,l,mid);
    build(RS,mid+1,r);
}
void pushup(int rt,int l,int r){
    MaxiLen[rt]=max(max(MaxiLen[LS],MaxiLen[RS]),pre[RS]+last[LS]);
    int mid=l+r>>1;
    if(pre[LS]==mid-l+1)pre[rt]=mid-l+1+pre[RS];
    else pre[rt]=pre[LS];
    if(last[RS]==r-mid)last[rt]=r-mid+last[LS];
    else last[rt]=last[RS];
    return;
}
void update(int rt,int l,int r,int x,int p){
    if(l==r){pre[rt]=last[rt]=MaxiLen[rt]=p;return;}
    int mid=l+r>>1;
    if(x<=mid)update(LS,l,mid,x,p);
    else update(RS,mid+. 1 , R & lt, X, P); 
    a pushup (RT, L, R & lt); 
} 
int Query ( int RT, int L, int R & lt, int X) {
     IF (L || R & lt MaxiLen == [RT] ||! MaxiLen [RT] + L-R & lt == . 1 ) return MaxiLen [RT];
     // find the interval containing x x || 0 || containing the full range of all x. 1 
    int MID = L + R & lt >> . 1 ;
     IF (X <= mID) {
         IF (X> = mID-Last [the LS] + . 1 ) return Last [the LS] + pre [the RS]; // interposed intermediate 
        the else  return Query (the LS, L, mID, X);                  / / the LS 
    }else{
        if(x<=mid+pre[RS])return last[LS]+pre[RS];     //夹中间
        else return query(RS,mid+1,r,x);               //RS
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        build(1,1,n);
        char c=getchar();int x;
        while(m--){
            c=getchar();
            if(c=='D'){
                scanf("%d",&x);
                update(1,1,n,x,0);
                STK[++HEAD]=x;
            }
            else if(c=='Q'){
                scanf("%d",&x);
                printf("%d\n",query(1,1,n,x));
            }
            else update(1,1,n,STK[HEAD--],1);
            c=getchar();
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lsy263/p/11229113.html