# 少年，想学带修改主席树吗 | BZOJ1901 带修改区间第k小

sum数组可以用来不带修改求前缀和，那么假如我们要求带修改的前缀和呢？树状数组可以做到。每次在位置p加入一个元素x的时候，对树状数组中每个 {p, p + (p & -p), ...} 都加上x；询问前缀和的时候，则是求树状数组中每个 {p, p - (p & -p), ...} 的和。

int query(int ql, int qr, int k){
int l = 1, r = idx;
for(int p = ql; p; p -= p & -p) cur1[p] = root[p];
for(int p = qr; p; p -= p & -p) cur2[p] = root[p];
while(l < r){
int mid = (l + r) >> 1, sum1 = 0, sum2 = 0;
for(int p = ql; p; p -= p & -p) sum1 += data[ls[cur1[p]]];
for(int p = qr; p; p -= p & -p) sum2 += data[ls[cur2[p]]];
if(sum2 - sum1 >= k){
for(int p = ql; p; p -= p & -p) cur1[p] = ls[cur1[p]];
for(int p = qr; p; p -= p & -p) cur2[p] = ls[cur2[p]];
r = mid;
}
else{
l = mid + 1, k -= sum2 - sum1;
for(int p = ql; p; p -= p & -p) cur1[p] = rs[cur1[p]];
for(int p = qr; p; p -= p & -p) cur2[p] = rs[cur2[p]];
}
}
return lst[l];
}


void change(int old, int &k, int l, int r, int p, int x){
k = ++tot;
data[k] = data[old] + x, ls[k] = ls[old], rs[k] = rs[old];
if(l == r) return;
int mid = (l + r) >> 1;
if(p <= mid) change(ls[old], ls[k], l, mid, p, x);
else change(rs[old], rs[k], mid + 1, r, p, x);
}
void add(int p, int num, int x){
while(p <= n) change(root[p], root[p], 1, idx, num, x), p += p & -p;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 100005, M = 6000005;
int n, m, a[N], lst[N], idx;
int tot, root[N], data[M], ls[M], rs[M], cur1[N], cur2[N];
int qtype[N], q1[N], q2[N], q3[N];
void build(int &k, int l, int r){
k = ++tot;
if(l == r) return;
int mid = (l + r) >> 1;
build(ls[k], l, mid);
build(rs[k], mid + 1, r);
}
void change(int old, int &k, int l, int r, int p, int x){
k = ++tot;
data[k] = data[old] + x, ls[k] = ls[old], rs[k] = rs[old];
if(l == r) return;
int mid = (l + r) >> 1;
if(p <= mid) change(ls[old], ls[k], l, mid, p, x);
else change(rs[old], rs[k], mid + 1, r, p, x);
}
void add(int p, int num, int x){
while(p <= n) change(root[p], root[p], 1, idx, num, x), p += p & -p;
}
int query(int ql, int qr, int k){
int l = 1, r = idx;
for(int p = ql; p; p -= p & -p) cur1[p] = root[p];
for(int p = qr; p; p -= p & -p) cur2[p] = root[p];
while(l < r){
int mid = (l + r) >> 1, sum1 = 0, sum2 = 0;
for(int p = ql; p; p -= p & -p) sum1 += data[ls[cur1[p]]];
for(int p = qr; p; p -= p & -p) sum2 += data[ls[cur2[p]]];
if(sum2 - sum1 >= k){
for(int p = ql; p; p -= p & -p) cur1[p] = ls[cur1[p]];
for(int p = qr; p; p -= p & -p) cur2[p] = ls[cur2[p]];
r = mid;
}
else{
l = mid + 1, k -= sum2 - sum1;
for(int p = ql; p; p -= p & -p) cur1[p] = rs[cur1[p]];
for(int p = qr; p; p -= p & -p) cur2[p] = rs[cur2[p]];
}
}
return lst[l];
}
int getpos(int x){
return lower_bound(lst + 1, lst + idx + 1, x) - lst;
}
bool isQ(){
char c;
while(c = getchar(), c != 'Q' && c != 'C');
return c == 'Q';
}
int main(){
for(int i = 1; i <= n; i++)
for(int i = 1; i <= m; i++){
else lst[++idx] = q2[i];
}
sort(lst + 1, lst + idx + 1);
idx = unique(lst + 1, lst + idx + 1) - lst - 1;
build(root[0], 1, idx);
for(int i = 1; i <= n; i++) root[i] = root[0];
for(int i = 1; i <= n; i++) add(i, getpos(a[i]), 1);
for(int i = 1; i <= m; i++){
if(qtype[i]) write(query(q1[i] - 1, q2[i], q3[i])), enter;
else{