题意:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112
由于要更新主席树其中一个点都会对后面的点有影响,要是也更新后面所有点复杂度会比较大,这种对后面的造成影响的会想到用树状数组解决。
#include <iostream>
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = (5e4 + 5)*2;
int tot, sz, T[MAXN], a[MAXN], f[MAXN], S[MAXN], rt[MAXN], L, R;
int sum[MAXN*20], lson[MAXN*20], rson[MAXN*20];
struct question{
int l, r, type, k;
}q[MAXN];
inline int newnode(){//获取新点
++tot;
sum[tot] = lson[tot] = rson[tot] = 0;
return tot;
}
inline int Hash(int x){//离散数据点
return lower_bound(f, f+sz, x) - f;
}
inline int lowbit(int x){
return x & (-x);
}
void build(int &root, int l, int r){//初始建树
root = newnode();
if(l == r) return;
int mid = (l + r) >> 1;
build(lson[root], l, mid);
build(rson[root], mid+1, r);
}
void update(int &lroot, int &rroot, int l, int r, int target, int k){//更新
rroot = newnode();
sum[rroot] = sum[lroot] + k;
lson[rroot] = lson[lroot], rson[rroot] = rson[lroot];
if(l == r) return;
int mid = (l + r) >> 1;
if(target <= mid) update(lson[lroot], lson[rroot], l, mid, target, k);
else update(rson[lroot], rson[rroot], mid+1, r, target, k);
}
void add(int x, int target, int k){//树状数组差分,记录对后面的影响
int tmp;
for(int i = x; i < MAXN; i += lowbit(i)){
update(S[i], tmp, 0, sz, target, k);
S[i] = tmp;
}
}
int Sum(int x){//获取区间影响
int ans = 0;
for(int i = x; i; i -= lowbit(i)) ans += sum[lson[rt[i]]];
return ans;
}
int query(int &lroot, int &rroot, int l, int r, int k){
if(l == r) return f[l];
int mid = (l + r) >> 1;
int tmp = sum[lson[rroot]] - sum[lson[lroot]] + Sum(R) - Sum(L);//在原来基础上加上前面更新点对后面造成的影响
if(k <= tmp){
for(int i = L; i; i -= lowbit(i)) rt[i] = lson[rt[i]];
for(int i = R; i; i -= lowbit(i)) rt[i] = lson[rt[i]];
return query(lson[lroot], lson[rroot], l, mid, k);
}
else{
for(int i = L; i; i -= lowbit(i)) rt[i] = rson[rt[i]];
for(int i = R; i; i -= lowbit(i)) rt[i] = rson[rt[i]];
return query(rson[lroot], rson[rroot], mid+1, r, k - tmp);
}
}
void debug(int &root, int l, int r){
printf("root: %d, l: %d, r: %d, sum: %d\n", root, l, r, sum[root]);
if(l == r) return;
int mid = (l + r) >> 1;
debug(lson[root], l, mid);
debug(rson[root], mid+1, r);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t, n, m;
char op[2];
cin >> t;
while(t--){
cin >> n >> m;
tot = 0, sz = 0;
for(int i = 1; i <= n; i++) cin >> a[i], f[sz++] = a[i];
for(int i = 1; i <= m; i++){
cin >> op;
if(op[0] == 'Q'){
q[i].type = 1;
cin >> q[i].l >> q[i].r >> q[i].k;
}
else{
q[i].type = 0;
cin >> q[i].l >> q[i].k;
f[sz++] = q[i].k;
}
}
sort(f, f+sz);
sz = unique(f, f+sz) - f;
build(T[0], 0, sz);
for(int i = 1; i <= n; i++) update(T[i-1], T[i], 0, sz, Hash(a[i]), 1), S[i] = T[0];
for(int i = 1; i <= m; i++){
if(q[i].type){
L = q[i].l-1, R = q[i].r;
for(int i = L; i; i -= lowbit(i)) rt[i] = S[i];//预处理要用到的树
for(int i = R; i; i -= lowbit(i)) rt[i] = S[i];
cout << query(T[L], T[R], 0, sz, q[i].k) << endl;
}
else{
add(q[i].l, Hash(a[q[i].l]), -1);
add(q[i].l, Hash(q[i].k), 1);
a[q[i].l] = q[i].k;
}
}
}
return 0;
}