P2617 Dynamic Rankings【动态第K大 树状数组+主席树】

传送门

中文题目,题意不再叙述!
实现方法:静态第K大,我们利用就是前缀和的思想来建立的线段树,[L,R]的状态由R状态的线段树减去L-1状态的线段树得到的。既然就是前缀和而且存在修改操作,我们可以利用树状数组来解决这个问题。
原本处于下标 i 的线段树 代表[1 - i ]所有元素的线段树,那么在树状数组中我们要怎么得到同样的一棵树呢?
我们知道树状数组中存在 lowbit(x) 操作,我们可以知道上面那种状态的线段树是由多棵树组成的。
更新操作:

void update(int sub, int pos, int add) {    ///sub树状数组下标,pos更新的位置,add更新的值
    for (int i = sub; i <= n; i += lowbit(i))
        insert(root[i], root[i], 1, d, pos, add);
}

 查找操作:

cntl = cntr = 0;
for (int j = op[i].r; j > 0; j -= lowbit(j)) { tempr[++cntr] = root[j]; }
for (int j = op[i].l - 1; j > 0; j -= lowbit(j)) { templ[++cntl] = root[j]; }
printf("%d\n", ranks[query(1, d, op[i].k)]);

 其实就是利用树状数组将一种状态的树,分割成了几棵树的加减。然后利用树状的单点更新,可以达到修改的操作。

///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>

#define MT(a, b) memset(a,b,sizeof(a)
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai = acos(-1.0);
const double E = 2.718281828459;
const ll mod = 20071027;
const ll INF = 0x3f3f3f3f3f3f;
const int maxn = 1e5 + 5;

int n, num[maxn], ranks[maxn << 1], d;///原数组和离散化数组
int root[maxn], times;  ///主席树
int templ[maxn], tempr[maxn], cntl, cntr;///临时记录树状数组中主席树的时间戳
struct node {
    int ls, rs, cnt;
} p[maxn * 400];
struct OP {
    int mark, l, r, k;
} op[maxn];

void insert(int &now, int old, int l, int r, int pos, int x) {
    now = ++times;
    p[now] = p[old], p[now].cnt += x;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid) { insert(p[now].ls, p[old].ls, l, mid, pos, x); }
    else { insert(p[now].rs, p[old].rs, mid + 1, r, pos, x); }
}

void update(int sub, int pos, int add) {    ///sub树状数组下标,pos更新的位置,add更新的值
    for (int i = sub; i <= n; i += lowbit(i))
        insert(root[i], root[i], 1, d, pos, add);
}

int query(int l, int r, int k) {
    if (l == r) { return l; }
    int mid = (l + r) >> 1, cnt = 0;
    for (int i = 1; i <= cntr; i++) { cnt += p[p[tempr[i]].ls].cnt; }
    for (int i = 1; i <= cntl; i++) { cnt -= p[p[templ[i]].ls].cnt; }
    if (cnt >= k) {
        for (int i = 1; i <= cntr; i++) { tempr[i] = p[tempr[i]].ls; }
        for (int i = 1; i <= cntl; i++) { templ[i] = p[templ[i]].ls; }
        return query(l, mid, k);
    } else {
        for (int i = 1; i <= cntr; i++) { tempr[i] = p[tempr[i]].rs; }
        for (int i = 1; i <= cntl; i++) { templ[i] = p[templ[i]].rs; }
        return query(mid + 1, r, k - cnt);
    }
}

int get_pos(int x) {
    return lower_bound(ranks + 1, ranks + 1 + d, x) - ranks;
}

int main() {
    char x[10];
    int q, up = 0;
    scanf("%d %d", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &num[i]);
        ranks[++up] = num[i];
    }
    for (int i = 1; i <= q; i++) {  ///离线询问
        scanf("%s", x);
        if (x[0] == 'Q') {
            op[i].mark = 1;           ///mark==1  查询
            scanf("%d %d %d", &op[i].l, &op[i].r, &op[i].k);
        } else {
            op[i].mark = 0;
            scanf("%d %d", &op[i].l, &op[i].r);
            ranks[++up] = op[i].r;
        }
    }
    sort(ranks + 1, ranks + 1 + up);
    d = unique(ranks + 1, ranks + 1 + up) - (ranks + 1);
    for (int i = 1; i <= n; i++) { update(i, get_pos(num[i]), 1); }
    for (int i = 1; i <= q; i++) {
        if (op[i].mark == 1) {   ///查询操作
            cntl = cntr = 0;    ///r和l状态的树由那几棵树构成
            for (int j = op[i].r; j > 0; j -= lowbit(j)) { tempr[++cntr] = root[j]; }
            for (int j = op[i].l - 1; j > 0; j -= lowbit(j)) { templ[++cntl] = root[j]; }
            printf("%d\n", ranks[query(1, d, op[i].k)]);
        } else {        ///更新操作
            update(op[i].l, get_pos(num[op[i].l]), -1);
            update(op[i].l, get_pos(op[i].r), 1);
            num[op[i].l] = op[i].r;
        }
    }
    return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值