# 【BZOJ1901】Zju2112 Dynamic Rankings【树状数组套主席树 / 整体二分】

/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 10005, maxm = 10005, maxnode = 2200005;

int n, m, num[maxn], disc[maxn + maxm], tot;

struct _data {
int opt, x, y, c;
} que[maxm];

int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}

inline int find(int x) {
int l = 1, r = tot;
while(l <= r) {
int mid = l + r >> 1;
if(disc[mid] < x) l = mid + 1;
else if(disc[mid] == x) return mid;
else r = mid - 1;
}
}

int root[maxn], son[maxnode][2], sum[maxnode], trcnt;

inline void change(int last, int &now, int l, int r, int x, int c) {
now = ++trcnt;
son[now][0] = son[last][0]; son[now][1] = son[last][1];
sum[now] = sum[last] + c;
if(l == r) return;
int mid = l + r >> 1;
if(x <= mid) change(son[last][0], son[now][0], l, mid, x, c);
else change(son[last][1], son[now][1], mid + 1, r, x, c);
}

inline void bitadd(int x, int c) {
for(int i = x; i <= n; i += i & -i) change(root[i], root[i], 1, tot, c, 1);
}

inline void bitchange(int x, int c) {
int tmp = find(num[x]);
for(int i = x; i <= n; i += i & -i) change(root[i], root[i], 1, tot, tmp, -1);
tmp = find(num[x] = c);
for(int i = x; i <= n; i += i & -i) change(root[i], root[i], 1, tot, tmp, 1);
}

int X, Y, L[maxn], R[maxn];

inline int query(int l, int r, int k) {
if(l == r) return l;
int suml = 0, sumr = 0;
for(int i = 1; i <= X; i++) suml += sum[son[L[i]][0]];
for(int i = 1; i <= Y; i++) sumr += sum[son[R[i]][0]];
int mid = l + r >> 1;
if(sumr - suml >= k) {
for(int i = 1; i <= X; i++) L[i] = son[L[i]][0];
for(int i = 1; i <= Y; i++) R[i] = son[R[i]][0];
return query(l, mid, k);
} else {
for(int i = 1; i <= X; i++) L[i] = son[L[i]][1];
for(int i = 1; i <= Y; i++) R[i] = son[R[i]][1];
return query(mid + 1, r, k - (sumr - suml));
}
}

inline int bitquery(int x, int y, int c) {
X = Y = 0; x--;
for(int i = x; i; i -= i & -i) L[++X] = root[i];
for(int i = y; i; i -= i & -i) R[++Y] = root[i];
return disc[query(1, tot, c)];
}

int main() {
for(int i = 1; i <= n; i++) disc[++tot] = num[i] = iread();
for(int i = 1; i <= m; i++) {
char ch = getchar(); for(; ch != 'Q' && ch != 'C'; ch = getchar());
if(ch == 'Q') {
que[i] = ((_data){1, x, y, c});
} else {
que[i] = ((_data){2, x, 0, c});
disc[++tot] = c;
}
}

sort(disc + 1, disc + 1 + tot);
tot = unique(disc + 1, disc + 1 + tot) - (disc + 1);

for(int i = 1; i <= n; i++) bitadd(i, find(num[i]));
for(int i = 1; i <= m; i++)
if(que[i].opt == 1) printf("%d\n", bitquery(que[i].x, que[i].y, que[i].c));
else bitchange(que[i].x, que[i].c);
return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rec(i, x, n) for(int i = x; i <= n; i++)
const int maxn = 30010;
int n, m, cnt, tot, k1[maxn], k2[maxn], k[maxn], tr[maxn], tmp[maxn], ans[maxn], op[maxn], rank[maxn << 1], num[maxn];
struct _opt {int tp, id, k1, k2, kth, cur;} c[maxn], c1[maxn], c2[maxn];
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline int get(int x) {return std::lower_bound(rank + 1, rank + 1 + tot, x) - rank;}
void add(int x, int w) {
for(; x <= n; x += x & -x) tr[x] += w;
}
int sum(int x) {
int ans = 0;
for(; x; x -= x & -x) ans += tr[x];
return ans;
}
void solve(int h, int t, int l, int r) {
if(h > t) return;
if(l == r) {
rec(i, h, t) if(c[i].tp == 3) ans[c[i].id] = l;
return;
}
int mid = (l + r) >> 1;
rec(i, h, t) {
if(c[i].tp == 1 && c[i].k1 <= mid) add(c[i].id, 1);
if(c[i].tp == 2 && c[i].k1 <= mid) add(c[i].id, -1);
if(c[i].tp == 3) tmp[i] = sum(c[i].k2) - sum(c[i].k1 - 1);
}
rec(i, h, t) {
if(c[i].tp == 1 && c[i].k1 <= mid) add(c[i].id, -1);
if(c[i].tp == 2 && c[i].k1 <= mid) add(c[i].id, 1);
}
int q1 = 0, q2 = 0;
rec(i, h, t) {
if(c[i].tp == 3) {
if(c[i].cur + tmp[i] >= c[i].kth) c1[q1++] = c[i];
else c[i].cur += tmp[i], c2[q2++] = c[i];
}
if(c[i].tp == 1 || c[i].tp == 2) c[i].k1 <= mid ? c1[q1++] = c[i] : c2[q2++] = c[i];
}
rec(i, 0, q1 - 1) c[h + i] = c1[i];
rec(i, 0, q2 - 1) c[h + i + q1] = c2[i];
solve(h, h + q1 - 1, l, mid); solve(h + q1, t, mid + 1, r);
}
int main() {
rec(i, 1, n) rank[++tot] = num[i] = iread();
rec(i, 1, m) {
if(ch[0] == 'Q') op[i] = 1, k[i] = iread();
if(ch[0] == 'C') op[i] = 2, rank[++tot] = k2[i];
}
std::sort(rank + 1, rank + 1 + tot);
tot = std::unique(rank + 1, rank + 1 + tot) - rank - 1;
rec(i, 1, n) c[++cnt] = (_opt){1, i, get(num[i])};
rec(i, 1, m)
if(op[i] == 1) c[++cnt] = (_opt){3, i, k1[i], k2[i], k[i]};
else c[++cnt] = (_opt){2, k1[i], get(num[k1[i]])}, c[++cnt] = (_opt){1, k1[i], get(num[k1[i]] = k2[i])};
memset(ans, -1, sizeof(ans)); solve(1, cnt, 1, tot);
rec(i, 1, m) if(ans[i] >= 0) printf("%d\n", rank[ans[i]]);
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客