BZOJ1901 dynamic ranking-(带修主席树套树状数组:动态查询区间第k小)

Dynamic Rankings


Time Limit: 10 Seconds      Memory Limit: 32768 KB


The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.


Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or
C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.


Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.


Sample Input

2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3


Sample Output

3
6
3
6

 

题意:

区间第k大+单点修改

分析:

普通的主席树上求第k大上面,增加了单点修改的需求.

因为主席树是离线数据结构,这样就导致了单点修改比较麻烦,所以我们可以利用树状数组来维护区间和,主席树的作用是维护位置.

但是因为主席树是离线数据结构,我们需要对所有的查询离线处理,先把所有可能出现的节点信息保存下来,然后再去重离散化,

我们现在建出来的主席树,第i个根维护的是[i−lowbit(i)+1,i]区间的信息,而不是像以前的前缀树一样。
 

/*​​​​​​​区间第k大+单点修改*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010, INF = 1e9;
struct rec {int op, x, y, z;} q[3 * N], lq[3 * N], rq[3 * N];
int T, n, m, t, p, a[N], c[N], ans[N];

int ask(int x) {
    int y = 0;
    for (; x; x -= x & -x) y += c[x];
    return y;
}

void change(int x, int y) {
    for (; x <= n; x += x & -x) c[x] += y;
}

void solve(int lval, int rval, int st, int ed) {
    if (st > ed) return;
    if (lval == rval) {
        for (int i = st; i <= ed; i++)
            if (q[i].op > 0) ans[q[i].op] = lval;
        return;
    }
    int mid = (lval + rval) >> 1;
    int lt = 0, rt = 0;
    for (int i = st; i <= ed; i++) {
        if (q[i].op <= 0) { // 是一次修改
            if (q[i].y <= mid) change(q[i].x, q[i].z), lq[++lt] = q[i];
            else rq[++rt] = q[i];
        } else { // 是一次询问
            int cnt = ask(q[i].y) - ask(q[i].x - 1);
            if (cnt >= q[i].z) lq[++lt] = q[i];
            else q[i].z -= cnt, rq[++rt] = q[i];
        }
    }
    for (int i = ed; i >= st; i--) { // 还原树状数组
        if (q[i].op <= 0 && q[i].y <= mid) change(q[i].x, -q[i].z);
    }
    for (int i = 1; i <= lt; i++) q[st + i - 1] = lq[i];
    for (int i = 1; i <= rt; i++) q[st + lt + i - 1] = rq[i];
    solve(lval, mid, st, st + lt - 1);
    solve(mid + 1, rval, st + lt, ed);
}

int main() {
    cin >> T;
    while (T--) {
        cin >> n >> m;
        t = p = 0;
        for (int i = 1; i <= n; i++) {
            int val; scanf("%d", &val);
            // 等价于在第i个位置上加入一个数val
            q[++t].op = 0, q[t].x = i, q[t].y = val, q[i].z = 1;
            a[i] = val;
        }
        for (int i = 1; i <= m; i++) {
            char op[2]; scanf("%s", op);
            if (op[0] == 'Q') {
                int l, r, k; scanf("%d%d%d", &l, &r, &k);
                // 记录一次询问
                q[++t].op = ++p, q[t].x = l, q[t].y = r, q[t].z = k;
            } else {
                int x, y; scanf("%d%d", &x, &y);
                // 去掉原来的数a[x]
                q[++t].op = -1, q[t].x = x, q[t].y = a[x], q[t].z = -1;
                // 在第x个位置上加入一个新的数y
                q[++t].op = 0, q[t].x = x, q[t].y = y, q[t].z = 1;
                a[x] = y;
            }
        }
        // 基于值域对t=n+m个操作进行整体分治
        solve(0, INF, 1, t);
        for (int i = 1; i <= p; i++)
            printf("%d\n", ans[i]);
    }
}

 

发布了1334 篇原创文章 · 获赞 422 · 访问量 27万+
展开阅读全文

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

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

分享到微信朋友圈

×

扫一扫,手机浏览