洛谷P2617 Dynamic Rankings

带修主席树模板题

主席树的单点修改就是把前缀和(大概)的形式改成用树状数组维护,每个树状数组的元素都套了一个主席树(相当于每个数组的元素root[i]都是主席树,且这个主席树维护了(i - lowbit(i) + 1, i)这个区间的值域信息)

修改的时候就是沿着lowbit把包含了该点的区间全部替换成新的线段树就行了~

回答和静态主席树差不多,不过不是两颗树相减,因为要知道前缀所有值域的信息,所以区间左边和右边都要同时往后沿着lowbit跳完所有的主席树。

注意的是主席树修改需要离线,因为我们要先离散化,如果提前处理好修改的值的话,离散化之后可能会没有新数的位置。

#include 
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 300005;
int n, m, cnt, tot, x, y, a[N], b[N], lc[N<<8], rc[N<<8], root[N], tree[N<<8], lt[N], rt[N];
struct Query{
    bool isq;
    int l, r, pos, k;
}query[100005];

int modify(int rt, int l, int r, int pos, int k){
    int cur = ++cnt;
    tree[cur] = tree[rt] + k, lc[cur] = lc[rt], rc[cur] = rc[rt];
    if(l == r) return cur;
    int mid = (l + r) >> 1;
    if(pos <= mid) lc[cur] = modify(lc[rt], l, mid, pos, k);
    else rc[cur] = modify(rc[rt], mid + 1, r, pos, k);
    return cur;
}

void add(int k, int x){
    int p = (int)(lower_bound(b + 1, b + tot + 1, a[k]) - b);
    for(int i = k; i <= n; i += lowbit(i))
        root[i] = modify(root[i], 1, tot, p, x);
}

int queryQAQ(int k, int l, int r){
    if(l == r) return l;
    int suml = 0, sumr = 0;
    for(int i = 1; i <= x; i ++) suml += tree[lc[lt[i]]];
    for(int i = 1; i <= y; i ++) sumr += tree[lc[rt[i]]];
    int mid = (l + r) >> 1;
    if(sumr - suml >= k){
        for(int i = 1; i <= x; i ++) lt[i] = lc[lt[i]];
        for(int i = 1; i <= y; i ++) rt[i] = lc[rt[i]];
        return queryQAQ(k, l, mid);
    }
    else{
        for(int i = 1; i <= x; i ++) lt[i] = rc[lt[i]];
        for(int i = 1; i <= y; i ++) rt[i] = rc[rt[i]];
        return queryQAQ(k - (sumr - suml), mid + 1, r);
    }
}

int main(){

    n = read(), m = read();
    for(int i = 1; i <= n; i ++){
        a[i] = read();
        b[++tot] = a[i];
    }
    for(int i = 1; i <= m; i ++){
        char opt[3]; scanf("%s", opt);
        if(opt[0] == 'C'){
            query[i].isq = true;
            query[i].pos = read(), query[i].k = read();
            b[++tot] = query[i].k;
        }
        else{
            query[i].isq = false;
            query[i].l = read(), query[i].r = read(), query[i].k = read();
        }
    }
    sort(b + 1, b + tot + 1);
    tot = (int)(unique(b + 1, b + tot + 1) - b - 1);
    for(int i = 1; i <= n; i ++) add(i, 1);
    for(int i = 1; i <= m; i ++){
        if(query[i].isq){
            int pos = query[i].pos;
            add(pos, -1);
            a[pos] = query[i].k;
            add(pos, 1);
        }
        else{
            x = y = 0;
            for(int j = query[i].l - 1; j; j -= lowbit(j)) lt[++x] = root[j];
            for(int j = query[i].r; j; j -= lowbit(j)) rt[++y] = root[j];
            printf("%d\n", b[queryQAQ(query[i].k, 1, tot)]);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/onionQAQ/p/10776556.html

你可能感兴趣的:(洛谷P2617 Dynamic Rankings)