Chairman of the tree dynamically updated

Chairman of the tree is still seeking an interval of k large, dynamic query while the Chairman of the tree will need a large range of k, while still a single point update.

Chairman of the tree of knowledge of dynamic learning need to know: Static Chairman tree, an array of arrays.

Chairman of the tree structure and dynamics: Fenwick tree cover tree + static Chairman Chairman tree.

Title: Luo Gu P2617

https://www.luogu.org/problem/P2617

Code:

#include <the iostream> 
#include <cstdio> 
#include <Vector> 
#include <algorithm> the using namespace STD; const int MAXN = + 5E5 . 7 ; 
Vector < int > V;   // array of the discrete memory struct Query {
     int L , R & lt, K;
     BOOL OP; 
} Q [MAXN];    // store instructions ahead of the need to use, after discretization facilitates int the root [MAXN * 40 ];     // store static Chairman root of the tree int T [MAXN * 40 ];        // store dynamic Chairman of the root of the tree int

 

 






ARR [MAXN];           // N input values stored 
int LSON [MAXN * 40 ], rson [MAXN * 40 ];    // node left subtree stored 
int LS [MAXN * 40 ], RS [MAXN * 40 ];    // value used when storing the update 
int SUM [MAXN * 40 ];      // number of values stored in a section 
int CNT;    
 int size;    // length scale interval 
int n-, m; 

int lowbit ( int X) {
     return X & (- X); 
} 

int Find ( int X) {
    return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}

void build(int l, int r, int &cur) {
    cur = ++cnt;
    sum[cur] = 0;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, lson[cur]);
    build(mid + 1, r, rson[cur]);
}

void update(int l, int r, int pre, int &cur, int pos, int val) {
    cur = ++cnt;
    lson[cur] = lson[pre];
    rson[cur] = rson[pre];
    sum[cur] = sum[pre] + val;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) {
        update(l, mid, lson[pre], lson[cur], pos, val);
    } else {
        update(mid + 1, r, rson[pre], rson[cur], pos, val);
    }
}

void change(int x, int pos, int val) {
    while(x <= n) {
        update(1, size, T[x], T[x], pos, val);
        x += lowbit(x);
    }
}

int query(int l, int r, int pre, int cur, int k) {
    if(l == r) {
        return l;
    }
    int mid = (l + r) >> 1;
     Int RES = SUM [LSON [CUR]] - SUM [LSON [pre]];   // value interval [left, right] Static President is herein tree ask
     @ A dynamic time for the following two cycles Chairman of the tree as required interval [left, right] the value 
    for ( int I = . 1 ; I <= LS [ 0 ]; I ++ ) { 
        RES - = SUM [LSON [LS [I]]]; 
    } 
    for ( int = I . 1 ; I <= RS [ 0 ]; I ++ ) { 
        RES + = SUM [LSON [RS [I]]]; 
    } 

    // explained: President of the static value of the original tree is the number of N, the dynamic Chairman of the tree after the value after the update, you need to obtain two add up to the total value of 

    IF (RES> = k) {
         //The following are two cycles for each node to have left subtree update to an array 
        for ( int I = . 1 ; I <= LS [ 0 ]; I ++ ) { 
            LS [I] = LSON [LS [I]] ; 
        } 
        for ( int I = . 1 ; I <= RS [ 0 ]; I ++ ) { 
            RS [I] = LSON [RS [I]]; 
        } 
        return Query (L, MID, LSON [pre], LSON [CUR] , K); 
    } the else {
         // supra, to update the right subtree 
        for ( int I = . 1 ; I <= LS [ 0 ]; I ++ ) {
            ls[i] = rson[ls[i]];
        }
        for(int i = 1; i <= rs[0]; i++) {
            rs[i] = rson[rs[i]];
        }
        return query(mid + 1, r, rson[pre], rson[cur], k - res);
    }
}

int main() {
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &arr[i]);
        v.push_back(arr[i]);
    }
    for(int i = 0; i < m; i++) {
        char str[5];
        scanf("%s", str);
        if(str[0] == 'Q') {
            scanf("%d %d %d", &Q[i].l, &Q[i].r, &Q[i].k);
            Q[i].op = true;
        } else {
            scanf("%d %d", & Q [I] .L, & Q [I] .r); 
            Q [I] .OP = to false ; 
            v.push_back (Q [I] .r); 
        } 
    } 

    Sort (v.begin (), V. End ()); 
    v.erase (UNIQUE (v.begin (), v.end ()), v.end ());    // basic operation discrete 

    size = v.size ();     // Get the length of interval 

    Build ( . 1 , size, the root [ 0 ]);
     for ( int I = . 1 ; I <= n-; I ++) {        // create a tree static President 
        Update ( . 1 , size, the root [I - . 1 ], the root [I] , Find (ARR [I]), . 1  );
    }

    for ( int I = . 1 ; I <= n-; I ++) {        // Dynamic President initialization tree 
        T [I] = the root [ 0 ]; 
    } 
    for ( int I = 0 ; I <m; I ++ ) {
         IF (Q [ I] .OP) {
             // following two for loops are acquired during the interval required to give a root node in the tree about the array 
            LS [ 0 ] = RS [ 0 ] = 0 ;
             for ( int J = Q [I] .L - . 1 ; J> 0 ; J - = lowbit (J)) { 
                LS [ ++ LS [ 0]] = T [J]; 
            } 
            for ( int J = Q [I] .r; J> 0 ; J - = lowbit (J)) { 
                RS [ ++ RS [ 0 ]] = T [J]; 
            } 

            int POS = Query ( . 1 , size, the root [Q [I] .L - . 1 ], the root [Q [I] .r], Q [I] .K); 
            the printf ( " % D \ n- " , V [ POS - . 1 ]); 
        } the else {
             // The first is to delete the original win value, the second value obtained by adding the new 
            change (Q [i] .l, find (arr [Q [i] .l ]), - . 1 ); 
            ARR [Q [I] .L]Q = [I] .r;   // update the original array Found 
            Change (Q [I] .L, Find (Q [I] .r), . 1 ); 
        } 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11261001.html