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

## 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; }

{
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
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];
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;
}
//四月南风大麦黄，枣花未落桐叶长。
//    -- 李颀《送陈章甫》

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