简单树套树区间单点修改区间K值查询线段树套TREAP
Dynamic Rankings
Time Limit: 10 Seconds
Memory Limit: 32768 KB
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.
InputThe 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 formatQ i j k orC i tIt 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.
OutputFor 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 Input25 33 2 1 4 7Q 1 4 3C 2 6Q 2 5 35 33 2 1 4 7Q 1 4 3C 2 6Q 2 5 3
Sample Output3636
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
#define INF 1000000000
#define MAXN 50100
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
typedef struct
{
int l,r,key,fix;
int weight,sz;
}node;
node p[1000010];
int rt[MAXN<<2],size,a[MAXN],n,m;
void treapInit()
{
srand(time(0));
size=-1;
}
void maintan(int k)
{
int l=(p[k].l==-1?0:p[p[k].l].weight);
int r=(p[k].r==-1?0:p[p[k].r].weight);
p[k].weight=l+r+p[k].sz;
}
void rot_l(int &x)
{
int y=p[x].r;
p[x].r=p[y].l;p[y].l=x;
maintan(x);maintan(y);
x=y;
}
void rot_r(int &x)
{
int y=p[x].l;
p[x].l=p[y].r;p[y].r=x;
maintan(x);maintan(y);
x=y;
}
void insert(int &k,int tkey)
{
if (k==-1)
{
k=++size;
p[k].l=p[k].r=-1;
p[k].key=tkey;
p[k].sz=1;
p[k].fix=rand();
maintan(k);
}
else if (tkey<p[k].key)
{
insert(p[k].l,tkey);
maintan(k);
if (p[ p[k].l ].fix>p[k].fix) rot_r(k);
}
else if (tkey>p[k].key)
{
insert(p[k].r,tkey);
maintan(k);
if (p[ p[k].r ].fix>p[k].fix) rot_l(k);
}
else
{
p[k].sz++;
maintan(k);
}
}
int cntLess(int k,int x)
{
int ret=0;
while(k!=-1)
{
if(p[k].key>x)
k=p[k].l;
else
{
int l=p[k].l==-1?0:p[p[k].l].weight;
ret+=(p[k].sz+l);
k=p[k].r;
}
}
return ret;
}
void remove(int &k,int tkey)
{
if (k==-1) return;
if (tkey<p[k].key)
remove(p[k].l,tkey);
else if (tkey>p[k].key)
remove(p[k].r,tkey);
else
{
if(p[k].sz!=1)
p[k].sz--;
else if (p[k].l==-1 && p[k].r==-1)
k=-1;
else if (p[k].l==-1)
k=p[k].r;
else if (p[k].r==-1)
k=p[k].l;
else if (p[ p[k].l ].fix <= p[ p[k].r ].fix)
{
rot_l(k);
remove(p[k].l,tkey);
}
else
{
rot_r(k);
remove(p[k].r,tkey);
}
}
maintan(k);
}
void out(int k)
{
if(k==-1) return;
out(p[k].l);
cout<<p[k].key<<" "<<p[k].sz<<endl;;
out(p[k].r);
}
void outseg(int l,int r,int root)
{
cout<<" tag "<<l<<" "<<r<<" "<<endl;
out(rt[root]);
cout<<endl;
if(l==r) return ;
int m=(l+r)>>1;
outseg(lson);
outseg(rson);
}
void build(int l,int r,int root)
{
rt[root]=-1;
for(int i=l;i<=r;i++) insert(rt[root],a[i]);
if(l>=r) return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void update(int l,int r,int root,int i,int x)
{
remove(rt[root],a[i]);
insert(rt[root],x);
if(l>=r) return;
int m=(l+r)>>1;
if(i<=m) update(lson,i,x);
else update(rson,i,x);
}
int getnumLess(int l,int r,int root,int L,int R,int x)
{
if(l>=L && r<=R) return cntLess(rt[root],x);
int m=(l+r)>>1,ret=0;
if(L<=m) ret+=getnumLess(lson,L,R,x);
if(R>m) ret+=getnumLess(rson,L,R,x);
return ret;
}
int searchK(int L,int R,int k)
{
int l=0,r=INF,ans;
while(l<=r)
{
int m=(l+r)>>1;
int cnt=getnumLess(1,n,1,L,R,m);
if(cnt>=k) r=m-1,ans=m;
else l=m+1;
}
return ans;
}
int main()
{
int cs;
scanf("%d",&cs);
while(cs--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
treapInit();
build(1,n,1);
for(int i=0;i<m;i++)
{
int x,y,z;
char str[4];
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d",&x,&y);
update(1,n,1,x,y);
a[x]=y;
}
else
{
scanf("%d%d%d",&x,&y,&z);
int k=searchK(x,y,z);
printf("%d\n",k);
}
}
}
return 0;
}