# BZOJ1901 Zju2112 Dynamic Rankings 【树状数组套主席树】

## 输入格式

Q i j k 或者 C i t
Q i j k （i,j,k是数字，1≤i≤j≤n, 1≤k≤j-i+1）

C i t (1≤i≤n，0≤t≤10^9)表示把a[i]改变成为t
m,n≤10000

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3

3

6

## 题解

【debug弄得要死，调用时(1,tot)写成了(l,r) QAQ】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 10005,maxm = 10000005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
int rt[maxn],A[maxn],B[2 * maxn],n,m,tot = 1,siz,N;
int sum[maxm],ls[maxm],rs[maxm],a[2][20];
struct Que{int opt,l,r,k;}Q[maxn];
int getn(int x){
int l = 1,r = tot,mid;
while (l <= r){
mid = l + r >> 1;
if (B[mid] < x) l = mid + 1;
else r = mid - 1;
}
return l;
}
void update(int& u,int l,int r,int pos,int v){
if (!u) u = ++siz; sum[u] += v;
if (l == r) return;
int mid = l + r >> 1;
if (mid >= pos) update(ls[u],l,mid,pos,v);
else update(rs[u],mid + 1,r,pos,v);
}
int query(int l,int r,int k){
if (l == r) return l;
int mid = l + r >> 1,t = 0;
for (int i = 1; i <= a[0][0]; i++) t += sum[ls[a[0][i]]];
for (int i = 1; i <= a[1][0]; i++) t -= sum[ls[a[1][i]]];
if (t >= k){
for (int i = 1; i <= a[0][0]; i++) a[0][i] = ls[a[0][i]];
for (int i = 1; i <= a[1][0]; i++) a[1][i] = ls[a[1][i]];
return query(l,mid,k);
}else {
for (int i = 1; i <= a[0][0]; i++) a[0][i] = rs[a[0][i]];
for (int i = 1; i <= a[1][0]; i++) a[1][i] = rs[a[1][i]];
return query(mid + 1,r,k - t);
}
}
void add(int u,int x,int v){while (u <= n) update(rt[u],1,tot,x,v),u += lbt(u);}
int solve(int l,int r,int k){
a[0][0] = a[1][0] = 0;
for (int i = r; i; i -= lbt(i)) a[0][++a[0][0]] = rt[i];
for (int i = l - 1; i; i -= lbt(i)) a[1][++a[1][0]] = rt[i];
return query(1,tot,k);
}
int main(){
REP(i,n) A[i] = B[++N] = read();
REP(i,m){
c = getchar(); while (c != 'Q' && c != 'C') c = getchar();
}
sort(B + 1,B + 1 + N);
for (int i = 2; i <= N; i++) if (B[i] != B[tot]) B[++tot] = B[i];