【BZOJ1901】【ZOJ2112】【带修主席树模板题】Dynamic Rankings

Description

辣鸡BZOJ抄了zoj的题tmd还是权限题
给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤j-i+1),并且,你可以改变一些a[i]的值,改变后,程序还能针对改变后的a继续回答上面的问题。你需要编一个这样的程序,从输入文件中读入序列a,然后读入一系列的指令,包括询问指令和修改指令。

对于每一个询问指令,你必须输出正确的回答。

Solution

带修主席树模板题。
考虑每一个主席树的根节点维护类似树状数组的ilowbit(i)+1i内的权值分布。
直接艹就行了

Code

/************************************************
 * Au: Hany01
 * Date: 
 * Prob: [BZOJ1901][ZOJ2112] Dynamic Ranking
 * Email: hany01@foxmail.com
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 10005, maxm = 10005;

struct Node
{
    int l, r, val;
}tr[maxn << 9];
int n, m, ls[maxn + maxm], cnt, tot, A[maxm], B[maxm], C[maxm], a[maxn], rt[maxn], lrt[40], rrt[40], cntl, cntr;
char type[maxm];

#define lb(x) ((x) & -(x))
#define mid ((l + r) >> 1)

int update(int las, int l, int r, int rk, int dt)
{
    int now;
    tr[now = ++ cnt] = tr[las], tr[now].val += dt;
    if (l != r) {
        if (rk <= mid) tr[now].l = update(tr[las].l, l, mid, rk, dt);
        else tr[now].r = update(tr[las].r, mid + 1, r, rk, dt);
    }
    return now;
}

int query(int l, int r, int rk)
{
    if (l == r) return l;
    int lsum = 0, rsum = 0;
    For(i, 1, cntl) lsum += tr[tr[lrt[i]].l].val;
    For(i, 1, cntr) rsum += tr[tr[rrt[i]].l].val;
    if (rsum - lsum >= rk) {
        For(i, 1, cntl) lrt[i] = tr[lrt[i]].l;
        For(i, 1, cntr) rrt[i] = tr[rrt[i]].l;
        return query(l, mid, rk);
    } else {
        For(i, 1, cntl) lrt[i] = tr[lrt[i]].r;
        For(i, 1, cntr) rrt[i] = tr[rrt[i]].r;
        return query(mid + 1, r, rk - (rsum - lsum));
    }
}

int main()
{
#ifdef hany01
    File("bzoj1901");
#endif

    //Initialize
    n = read(), m = read();
    For(i, 1, n) a[i] = ls[i] = read();
    tot = n;
    char sss[3];
    For(i, 1, m) {
        scanf("%s", sss);
        type[i] = sss[0];
        A[i] = read(), B[i] = read();
        if (type[i] == 'Q') C[i] = read();
        else ls[++ tot] = B[i];
    }
    sort(ls + 1, ls + 1 + tot);
    tot = unique(ls + 1, ls + 1 + tot) - ls - 1;

    //Build the president tree
    For(i, 1, n) {
        a[i] = lower_bound(ls + 1, ls + 1 + tot, a[i]) - ls;
        for (int x = i; x <= n; x += lb(x)) rt[x] = update(rt[x], 1, tot, a[i], 1);
    }

    //Deal with queries
    For(i, 1, m)
        if (type[i] == 'Q') {
            cntl = cntr = 0;
            for (register int j = A[i] - 1; j; j -= lb(j)) lrt[++ cntl] = rt[j];
            for (register int j = B[i]; j; j -= lb(j)) rrt[++ cntr] = rt[j];
            printf("%d\n", ls[query(1, tot, C[i])]);
        } else {
            for (register int j = A[i]; j <= n; j += lb(j)) rt[j] = update(rt[j], 1, tot, a[A[i]], -1);
            a[A[i]] = lower_bound(ls + 1, ls + 1 + tot, B[i]) - ls;
            for (register int j = A[i]; j <= n; j += lb(j)) rt[j] = update(rt[j], 1, tot, a[A[i]], 1);
        }

    return 0;
}
//四月南风大麦黄,枣花未落桐叶长。
//    -- 李颀《送陈章甫》
发布了393 篇原创文章 · 获赞 410 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览