ZOJ 2112 Dynamic Rankings链接如下：http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112

#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 1000000000

using namespace std;

int T, n, m, tot, cnt;
int a[50010], ans[10010], tree[50010], cur[70010];
char str[5];

void updata(int pos, int val) {
while (pos <= n) {
tree[pos] += val;
pos += pos & (-pos);
}
}

int tmp = 0;
while (pos > 0) {
tmp += tree[pos];
pos -= pos & (-pos);
}
return tmp;
}

struct N {
int l, r, k, id, cur, tp;
N() {}
N(int _l, int _r, int _k, int _id, int _cur, int _tp):
l(_l), r(_r), k(_k), id(_id), cur(_cur), tp(_tp) {}
};
N q[70010], q1[70010], q2[70010];

void ask(int fro, int las, int l, int r) {
if (fro > las) return ;
if (l == r) {
for (int i = fro; i <= las; i++) {
if (q[i].tp == 3) ans[q[i].id] = l;
}
return ;
}
int mid = (l + r) / 2;
for (int i = fro; i <= las; i++) {
if (q[i].tp == 1 && q[i].k <= mid) updata(q[i].l, 1);
else if (q[i].tp == 2 && q[i].k <= mid) updata(q[i].l, -1);
else if (q[i].tp == 3) cur[i] = read(q[i].r) - read(q[i].l - 1);
}
for (int i = fro; i <= las; i++) {
if (q[i].tp == 1 && q[i].k <= mid) updata(q[i].l, -1);
else if (q[i].tp == 2 && q[i].k <= mid) updata(q[i].l, 1);
}
int t1 = 0, t2 = 0;
for (int i = fro; i <= las; i++) {
if (q[i].tp == 3) {
if (q[i].cur + cur[i] >= q[i].k) {
q1[t1++] = q[i];
}
else {
q[i].cur += cur[i];
q2[t2++] = q[i];
}
}
else {
if (q[i].k <= mid) q1[t1++] = q[i];
else q2[t2++] = q[i];
}
}
for (int i = 0; i < t1; i++) q[fro + i] = q1[i];
for (int i = 0; i < t2; i++) q[fro + t1 + i] = q2[i];
ask(fro, fro + t1 - 1, l, mid);
ask(fro + t1, las, mid + 1, r);
}

int main() {
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
scanf("%d", &T);
while (T--) {
memset(tree, 0, sizeof(tree));
scanf("%d %d", &n, &m);
tot = cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
q[tot++] = N(i, i, a[i], 0, 0, 1);
}
int l, r, k;
for (int i = 0; i < m; i++) {
scanf("%s", str);
if (str[0] == 'Q') {
scanf("%d %d %d", &l, &r, &k);
q[tot++] = N(l, r, k, ++cnt, 0, 3);
}
else {
scanf("%d %d", &l, &k);
q[tot++] = N(l, l, a[l], 0, 0, 2);
q[tot++] = N(l, l, k, 0, 0, 1);
a[l] = k;
}
}
//printf("tot = %d cnt = %d\n", tot, cnt);
ask(0, tot - 1, 0, inf);
for (int i = 1; i <= cnt; i++)
printf("%d\n", ans[i]);
}
return 0;
}