# Zju2112 Dynamic Rankings

Description

Input

Output
Sample Input
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
Sample Output
3
6
HINT
20%的数据中，m,n≤100; 40%的数据中，m,n≤1000; 100%的数据中，m,n≤10000。

Solution :

Code

#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define MS(_) memset(_, 0, sizeof(_))
#define MP make_pair
#define PB push_back
typedef long long ll;
typedef pair<int, int> PII;
template<typename T> inline void read(T &x){
x = 0; T f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch))  {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}

const int N = 10000 + 10;
int n, m, top = 0, sz = 0, La, Lb, tot;
int num[N<<1], v[N], A[N], B[N], K[N], flag[N];
int root[N], lc[N*100], rc[N*100], sum[N*100], L[N], R[N];

inline int lowbit(int x) { return x&-x; }
inline void modify(int l, int r, int &x, int pos, int delta){
if (!x) x = ++sz; sum[x] += delta;
if (l == r) return;
int mid = l+r >> 1;
if (pos <= mid) modify(l, mid, lc[x], pos, delta);
else modify(mid+1, r, rc[x], pos, delta);
}
inline int query(int l, int r, int k){
if (l == r) return l;
int suml = 0, sumr = 0;
rep(i, 1, La) suml += sum[lc[L[i]]]; rep(i, 1, Lb) sumr += sum[lc[R[i]]];
int mid = (l+r)>>1;
if (k <= sumr - suml){
rep(i, 1, La) L[i] = lc[L[i]]; rep(i, 1, Lb) R[i] = lc[R[i]];
return query(l, mid, k);
}else{
rep(i, 1, La) L[i] = rc[L[i]]; rep(i, 1, Lb) R[i] = rc[R[i]];
return query(mid+1, r, k-(sumr-suml));
}
}
int main(){
rep(i, 1, n) scanf("%d", &v[i]), num[++top] = v[i];
rep(i, 1, m){ char ch[3];
scanf("%s%d%d", ch, &A[i], &B[i]);
if (ch[0] == 'Q') { scanf("%d", &K[i]); flag[i] = 1; }
else num[++top] = B[i];
}
sort(num+1, num+1+top);
tot = unique(num+1, num+1+top)-num-1;

rep(i, 1, n){
int pos = upper_bound(num+1, num+1+tot, v[i])-num-1;
for (int j = pos; j <= n; j += lowbit(j)) modify(1, tot, root[j], pos, 1);
}

rep(i, 1, m) if (flag[i]){
La = Lb = 0; A[i]--;
for (int j = A[i]; j; j -= lowbit(j)) L[++La] = root[j];
for (int j = B[i]; j; j -= lowbit(j)) R[++Lb] = root[j];
printf("%d\n", num[query(1, tot, K[i])]);
}else{
int pos = upper_bound(num+1, num+1+tot, v[A[i]])-num-1;
for (int j = A[i]; j <= n; j += lowbit(j)) modify(1, tot, root[j], pos, -1);
v[A[i]] = B[i];
pos = upper_bound(num+1, num+1+tot, v[A[i]])-num-1;
for (int j = A[i]; j <= n; j += lowbit(j)) modify(1, tot, root[j], pos, 1);
}

return 0;
}

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