The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], …, a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], …, a[j]? (For some i<=j, 0 < k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.
Your task is to write a program for this computer, which
Reads N numbers from the input (1 <= N <= 50,000)
Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], …, a[j] and change some a[i] to t.
Input
The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.
The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format
Q i j k or
C i t
It represents to query the k-th number of a[i], a[i+1], …, a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.
There’re NO breakline between two continuous test cases.
Output
For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],…, a[j])
There’re NO breakline between two continuous test cases.
Sample Input
2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
Sample Output
3
6
3
6
题意:可修改区间第k大。
做法:静态主席树处理原数组,用树套树维护修改的贡献。
用二分查找边界的时候可以优化查询主席树的log。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+7;
int num[N];
int op[N],a[N],b[N],c[N];
vector<int> vp;
int geth(int x){
return lower_bound(vp.begin(),vp.end(),x) - vp.begin()+1;
}
struct Tree{
int root[N],T[N],ls[N*24],rs[N*24],su[N*24];
int top,mx,mn;
void init(int n,int x){
memset(T,0,sizeof(T));
top = 1;
mx = x,mn = n;
root[0] = 0;
for(int i = 1;i <= n; i++){
//update(root[i],root[i-1],mp[num[i]],1,mx);
update(root[i],root[i-1],geth(num[i]),1,mx);
}
}
int newnode(){
ls[top] = rs[top] = su[top] = 0;
return top ++;
}
void update(int &rt,int prt,int x,int l,int r){
rt = newnode();
if(l == r){
su[rt] = su[prt]+1;
//cout << "!!!" << l << ' '<< su[rt] << endl;
return ;
}
int mid = l+r>>1;
if(mid >= x) update(ls[rt],ls[prt],x,l,mid),rs[rt] = rs[prt];
else update(rs[rt],rs[prt],x,mid+1,r),ls[rt] = ls[prt];
su[rt] = su[ls[rt]]+su[rs[rt]];
}
void set(int pos,int x,int y){
while(pos <= mx){
define(T[pos],x,-1,1,mx);
define(T[pos],y,1,1,mx);
pos += lowbit(pos);
}
}
void define(int &rt,int x,int d,int l,int r){
if(rt == 0) rt = newnode();
if(l == r){
su[rt] += d;
return ;
}
int mid = l+r>>1;
if(mid >= x) define(ls[rt],x,d,l,mid);
else define(rs[rt],x,d,mid+1,r);
su[rt] = su[ls[rt]]+su[rs[rt]];
}
int get(int rt,int L,int R,int l,int r){
if(rt == 0) return 0;
if(L <= l && R>= r) return su[rt];
int mid = l+r>>1;
int ret = 0;
if(mid >= L) ret += get(ls[rt],L,R,l,mid);
if(mid < R) ret += get(rs[rt],L,R,mid+1,r);
return ret;
}
int check(int x,int y){
int ret = 0;
while(x){
ret += get(T[x],1,y,1,mx);
x -= lowbit(x);
}
return ret;
}
int query(int L,int R,int k){
int l = 1,r = mx;
int lrt = root[L-1],rrt = root[R];
while(r != l){
int mid = l+r>>1;
int la = su[ls[lrt]] + check(L-1,mid);
int lb = su[ls[rrt]] + check(R,mid);
//cout <<la << ' '<<lb << endl;
//cout << l << ' '<< r << ' '<< lrt << ' '<< rrt << ' ' << su[ls[lrt]] << ' '<<su[ls[rrt]] << endl;
if(lb - la >= k) {r = mid,lrt = ls[lrt],rrt = ls[rrt];}
else {k -= su[ls[rrt]]-su[ls[lrt]]; l = mid+1,lrt = rs[lrt],rrt = rs[rrt];}
}
return r;
}
int lowbit(int x) {return x&-x;}
}tree;
int main(){
int T;
cin >> T;
while(T--){
int n,m;
scanf("%d %d",&n,&m);
vp.clear();
for(int i = 1;i <= n;i ++) scanf("%d",&num[i]);
for(int i= 1;i <= m;i ++){
char st[10];
scanf("%s",st);
if(st[0] == 'C'){
op[i] = 1;
scanf("%d %d",&a[i],&b[i]);
vp.push_back(b[i]);
}
else {
op[i] = 2;
scanf("%d %d %d",&a[i],&b[i],&c[i]);
}
}
for(int i= 1;i <= n;i ++) vp.push_back(num[i]);
sort(vp.begin(),vp.end());
vp.resize(unique(vp.begin(),vp.end())-vp.begin());
tree.init(n,vp.size());
for(int i = 1;i <= m;i ++){
//cout <<"!!!"<< i << ' '<<op[i] << endl;
if(op[i] == 2){
printf("%d\n",vp[tree.query(a[i],b[i],c[i])-1]);
}
else{
//tree.set(a[i],mp[num[a[i]]],mp[b[i]]);
tree.set(a[i],geth(num[a[i]]),geth(b[i]));
num[a[i]] = b[i];
}
}
}
return 0;
}