Dynamic Rankings

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.


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

分析:主席树套树状数组;

   主席树可以看成一棵棵前缀权值线段树,单点修改p,就要改p及其之后所有线段树;

   运用树状数组思想只需要修改log个即可;

   注意内存严格,所以一开始静态建树,动态修改即可;

   时间空间复杂度O(Nlog2N);

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=5e4+10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,a[maxn],b[60001],root[maxn],rx[maxn],lp[21],rp[21],ls[maxn*51],rs[maxn*51],s[maxn*51],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 gao(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;
    rep(i,1,lp[0])p-=s[ls[lp[i]]];
    rep(i,1,rp[0])p+=s[ls[rp[i]]];
    q=s[ls[y]]-s[ls[x]];
    if(z<=q+p)
    {
        rep(i,1,lp[0])lp[i]=ls[lp[i]];
        rep(i,1,rp[0])rp[i]=ls[rp[i]];
        return gao(l,mid,ls[x],ls[y],z);
    }
    else
    {
        rep(i,1,lp[0])lp[i]=rs[lp[i]];
        rep(i,1,rp[0])rp[i]=rs[rp[i]];
        return gao(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));
        rep(i,1,n)scanf("%d",&a[i]),b[++num]=a[i];
        rep(i,1,m)
        {
            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;
        rep(i,1,n)a[i]=lower_bound(b+1,b+num+1,a[i])-b;
        rep(i,1,n)insert(1,num,root[i-1],root[i],a[i],1);
        rep(i,1,m)
        {
            if(q[i])
            {
                init(ql[i]-1,qr[i]);
                printf("%d\n",b[gao(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;
}
posted @ 2017-02-05 20:47  mxzf0213  阅读(243)  评论(0编辑  收藏  举报