A good summary of the Chairman of the tree

Code at: https: //blog.csdn.net/weixin_44584560/article/details/87115412

#include <bits / STDC ++ H.>
 the using  namespace STD;
 const  int N = 100000 + . 5 ; 

int A [N], B [N], RT [N * 20 is ], LS [N * 20 is ], RS [N * 20 is ], SUM [N * 20 is ];
 int    n-, K, TOT, SZ, QL, QR, X, Q, T; 

void the Build ( int & O, int L, int R & lt) {       // I = 0 time prefix status: 0 tree 
    O ++ = TOT; 
    SUM [O] = 0 ;
     IF (R & lt == L) return ;
     intm = (L + R & lt) >> . 1 ; 
    the Build (LS [O], L, m); 
    the Build (RS [O], m + . 1 , R & lt); 
} 

void Update ( int & O, int L, int R & lt, int Last, int P) { 
    o = TOT ++;              // accompanied rt o update references, rt updated   
    LS [o] = LS [Last]; 
    RS [o] = RS [Last]; 
    SUM [o ] = SUM [Last] + . 1 ;
     IF (R & lt == L) return ;
     int m = (L + R & lt) >> . 1 ;
    if( p <= m) update( ls[o] ,l ,m ,ls[last] ,p);
    if( p > m ) update( rs[o] ,m+1,r ,rs[last] ,p);
}

int query( int ss ,int tt ,int l ,int r ,int k ){
     if( l == r) return l;
     int m = ( l + r )>>1;
     int cnt = sum[ ls[tt] ] - sum[ ls[ss] ];
     if( k <= cnt ) return query( ls[ss] ,ls[tt] ,l ,m ,k);
     else return query( rs[ss] ,rs[tt] ,m+1 ,r ,k - cnt);
}

void work(){
    scanf("%d%d%d" ,&ql ,&qr ,&x );
    int ans = query( rt[ ql-1],rt[qr] ,1 ,sz ,x);
    printf( "%d\n" ,b[ans] );
}

int main( ){
    scanf("%d" ,&T);
    while( T-- ){
        scanf( "%d%d" ,&n ,&q );
        for( int i=1 ;i<=n ;i++) 
        Scanf ( " % D " , & A [I]), B [I] = A [I]; 
        
        Sort (B + . 1 , B + n-+ . 1 ); 
        SZ = UNIQUE (B + . 1 , B + n-+ . 1 ) - ( B + . 1 ); 
        TOT = 0 ; 
        the Build (RT [ 0 ], . 1 , SZ);
         // RT [i], the time when the inserted i the nodes corresponding numbered, is inserted into the first sequence 
        for ( int i = . 1 ; i <= n-; I ++) A [I] = lower_bound (B + . 1 , B + . 1 + SZ, A [I]) - B;
         for( int i=1 ;i<=n ;i++ )update( rt[i] ,1 ,sz ,rt[ i - 1] ,a[i]);
        while( q-- )work();
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/-ifrush/p/11254986.html