POJ - 2528 Mayor's posters (tree line, discrete process)

Meaning of the questions: given a large range (1 <= ri <= 10000000.) means m asking given modification will appear in this range, ask to see the complete number of the last poster

Idea: to see such a large range we will think discrete data, said on discrete tall is actually compressed data space given. Because the data is not given full (1 to 1E7) so put pressure into a blank space, the other continuous space reserved

Note that, to increase the space as a blank space when not in continuous space compression

 

Complete code:

#include <CString> 
#include <the iostream> 
#include <cstdio> 
#include <algorithm>
 the using  namespace STD;
 const  int MAXN = 1E5;
 const  int INF = 0x3f3f3f3f ;
 int SEG [MAXN << 2 ];
 int the lazy [MAXN < < 2 ];
 int ARR [MAXN], ANS [MAXN];
 int Last;
 int L [MAXN], R & lt [MAXN]; // store interrogation range of about
 int TOT;
 int DSEG [MAXN]; // segment after compression Tree 

void push_up ( int P) {
    if(seg[p<<1] == seg[p<<1|1])
        seg[p] = seg[p<<1];
    else seg[p] = inf;
}
void push_down(int p,int l,int r){
    if(lazy[p]!= inf){
        lazy[p<<1] = lazy[p];
        lazy[p<<1|1] = lazy[p];
        seg[p<<1] = lazy[p];
        seg[p<<1|1] = lazy[p];
        lazy[p] = inf;
    }
}
void build(int p,int l,int r){
    if(l==r){
        seg[p] = arr[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    push_up(p);
}
void update(int p,int l,int r,int ql,int qr,int v){
    if(ql<=l&&r<=qr){
        seg[p] = v;
        lazy[p] = v;
        return ;
    }
    push_down(p,l,r);
    int mid = (l+r)>>1;
    if(ql <=mid) update(p<<1,l,mid,ql,qr,v);
    if(qr > mid) update(p<<1|1,mid+1,r,ql,qr,v);
    push_up(p);
}
void query(int p,int l,int r,int ql,int qr){
    if(seg[p]!=inf){
        if(seg[p]!= last) ans[seg[p]]++;
        last = seg[p];
        return ;
    }
    if(l==r) {last = inf; return ;}
    int mid = (l+r)>>1;
    push_down(p,l,r);
    query(p<<1,l,mid,ql,qr);
    query(p<<1|1,mid+1,r,ql,qr);
}
int main () {
     int n-, T; 
    CIN >> T;
     the while (T-- ) { 
        TOT = 0 ; 
        CIN >> n-;
         IF (! n-) COUT << 0 << endl;
         // because each time there interval coverage problem, so here is equivalent to the update data recorded 
        the else {
             for ( int K = 0 ; K <n-; K ++ ) { 
                CIN >> L [K] >> R & lt [K]; 
                DSEG [TOT ++] = L [K]; 
                DSEG [TOT ++] =  R & lt [K];
            }
            // discrete processing 
            Sort (DSEG, DSEG + TOT);
             int len = UNIQUE (DSEG, DSEG + TOT) - DSEG;
             int size = len;
             for ( int I = . 1 ; I <size; I ++ ) {
                 // about interval does not continuous section blank is increased (taking a large space than before. 1) 
                IF (DSEG [I] - DSEG [I- . 1 ]> . 1 ) DSEG [len ++] = DSEG [I- . 1 ] + . 1 ; 
            } 
            Sort (DSEG, DSEG + len);
             for ( int I = 0; I <n-; I ++ ) {
                 // then reentered into discrete good L, R the array 
                L [I] = lower_bound (DSEG, DSEG + len, L [I]) - + DSEG . 1 ; 
                R & lt [I] = lower_bound (DSEG, DSEG + len, R & lt [I]) - DSEG + . 1 ; 
            } 
            Memset (the lazy, INF, the sizeof (the lazy)); 
            Memset (ARR, INF, the sizeof (ARR)); 
            Memset (ANS, 0 , the sizeof (ANS)); 

            Build ( . 1 , . 1 , len);
             for ( int I = 0 ; I <n-; I ++ ) { 
                Update ( . 1, . 1 , len, L [I], R & lt [I], + I . 1 ); 
            } 
            Last = INF; 
             // count the number of posters (color can also be said statistics) 
            Query ( . 1 , . 1 , len, . 1 , len) ;
             int CNT = 0 ;
             for ( int I = . 1 ; I <= len; I ++ ) {
                 IF (ANS [I]) CNT ++ ; 
            } 
            COUT << CNT << endl; 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11297520.html