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大
因为要修改,所以要离线建主席树
简单说明一下为什么不能套线段树2333
1.如果在主席树上套线段树,则要修改之后所有的主席树,复杂度O(nlogn)
2.如果在主席树之外额外加一个线段树,很明显在区间访问的时候会出现问题,访问前面的区间就会出现问题
而树状数组单点修改之后不会影响它前面的点,并且可以修改之后的每一个点
#include<bits/stdc++.h>
#define maxn 50005
using namespace std;
int n,m,k,t,a[maxn],b[60001],root[maxn],rx[maxn],lp[21],rp[21],ls[maxn*40],rs[maxn*40],s[maxn*40],sz,num;
int q[10001],ql[10001],qr[10001],qk[10001];
void insert(int l,int r,int x,int &y,int z,int p)
{
y=++sz;
s[y]=s[x]+p;
if(l==r)return;
int mid=l+r>>1;
ls[y]=ls[x],rs[y]=rs[x];
if(z<=mid)insert(l,mid,ls[x],ls[y],z,p);
else insert(mid+1,r,rs[x],rs[y],z,p);
}
void init(int x,int y)
{
lp[0]=rp[0]=0;
while(x){lp[++lp[0]]=rx[x],x-=x&(-x);}
while(y){rp[++rp[0]]=rx[y],y-=y&(-y);}
}
void add(int pos,int x,int cnt)
{
while(pos<=n)
{
insert(1,num,rx[pos],rx[pos],x,cnt);
pos+=pos&(-pos);
}
}
int query(int l,int r,int x,int y,int z)
{
if(l==r)return l;
int mid=l+r>>1;
int p=0,q=0,i;
for(int i=1;i<=lp[0];i++)
p-=s[ls[lp[i]]];
for(int i=1;i<=rp[0];i++)
p+=s[ls[rp[i]]];
q=s[ls[y]]-s[ls[x]];
if(z<=q+p)
{
for(int i=1;i<=lp[0];i++)
lp[i]=ls[lp[i]];
for(int i=1;i<=rp[0];i++)
rp[i]=ls[rp[i]];
return query(l,mid,ls[x],ls[y],z);
}
else
{
for(int i=1;i<=lp[0];i++)
lp[i]=rs[lp[i]];
for(int i=1;i<=rp[0];i++)
rp[i]=rs[rp[i]];
return query(mid+1,r,rs[x],rs[y],z-q-p);
}
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
num=0;
sz=0;
memset(rx,0,sizeof(rx));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),b[++num]=a[i];
for(int i=1;i<=m;i++)
{
char str[2];
scanf("%s%d%d",str,&ql[i],&qr[i]);
q[i]=str[0]=='Q';
if(q[i])scanf("%d",&qk[i]);
else b[++num]=qr[i];
}
sort(b+1,b+num+1);
num=unique(b+1,b+num+1)-b-1;
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+num+1,a[i])-b;
for(int i=1;i<=n;i++)
insert(1,num,root[i-1],root[i],a[i],1);
for(int i=1;i<=m;i++)
{
if(q[i])
{
init(ql[i]-1,qr[i]);
printf("%d\n",b[query(1,num,root[ql[i]-1],root[qr[i]],qk[i])]);
}
else
{
qr[i]=lower_bound(b+1,b+num+1,qr[i])-b;
add(ql[i],a[ql[i]],-1);
add(ql[i],qr[i],1);
a[ql[i]]=qr[i];
}
}
}
return 0;
}