ZOJ 2112 Dynamic Rankings [樹狀數組套主席樹]

Description

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.



題意:給出N個數,M個詢問L,R,K,對於每個詢問給出區間(L,R)內的第K小值,支持修改。

解法:可以將求第K小問題轉換爲統計每個數出現的次數,求第一個在此區間內(1-X)的數出現>=K的數X。可以發現這個統計問題時滿足區間減法的,且要支持修改,可以考慮在樹狀數組上統計,因爲還需要求第K小,則需要一個數據結構來幫助維護,考慮使用主席樹來維護。


代碼:卡內存了,因爲我用了主席樹,每次插點都建了LogN個點,實際上是不需要建這麼多的,這個代碼可以過BZOJ的,但是不能過ZOJ的

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
 
#define lowbit(x) (x&(-x))
 
#define M 2500000
#define N 60060
int n,qn;
int a[N],b[N],bn;
int tot,c[M],lson[M],rson[M];
int t[N];
struct query{
    int l,r,k;
}q[10010];
 
 
void update(int l,int r,int pos,int pre,int cur,int val){
    lson[cur]=lson[pre];
    rson[cur]=rson[pre];
    c[cur]=c[pre]+val;
    if(l==r)return;
    int m=(l+r)>>1;
    if(pos<=m)
         update(  l,m,pos,lson[pre],lson[cur]=++tot,val);
    else update(m+1,r,pos,rson[pre],rson[cur]=++tot,val);
}
void update(int x,int pos,int val){
    for(int i=x;i<=n;i+=lowbit(i)){
        int temp=++tot;
        update(1,bn,pos,t[i],tot,val);
        t[i]=temp;
    }
}
 
int ln,rn,lx[111],rx[111];
int find_kth(int left,int right,int k){
    ln=rn=0;
    for(int i=left-1;i;i-=lowbit(i))lx[++ln]=t[i];
    for(int i=right;i;i-=lowbit(i))rx[++rn]=t[i];
    int l=1,r=bn;
    while(l!=r){
        int m=(l+r)>>1;
        int suml=0,sumr=0;
        rep(i,1,ln)suml+=c[lson[lx[i]]];
        rep(i,1,rn)sumr+=c[lson[rx[i]]];
        if(sumr-suml>=k){
            r=m;
            rep(i,1,ln)lx[i]=lson[lx[i]];
            rep(i,1,rn)rx[i]=lson[rx[i]];
        }
        else{
            l=m+1;
            k-=sumr-suml;
            rep(i,1,ln)lx[i]=rson[lx[i]];
            rep(i,1,rn)rx[i]=rson[rx[i]];
        }
    }
    return l;
}
 
int build(int l,int r){
    int x=++tot;
    c[x]=0;
    if(l!=r){
        int m=(l+r)>>1;
        lson[x]=build(l,m);
        rson[x]=build(m+1,r);
    }
    return x;
}
 
char s[5];
int main(){
 
    bn=0;
    scanff(n);scanff(qn);
    rep(i,1,n)scanff(a[i]),b[++bn]=a[i];
    rep(i,1,qn){
        scanf("%s",s);
        if(s[0]=='Q'){
            scanff(q[i].l);scanff(q[i].r);scanff(q[i].k);
        }
        else{
            scanff(q[i].l);scanff(q[i].r);q[i].k=-1;
            b[++bn]=q[i].r;
        }
    }
 
    //離散化
    sort(b+1,b+1+bn);
    bn=unique(b+1,b+1+bn)-b-1;
    rep(i,1,n)a[i]=findx(a[i]);
    rep(i,1,qn)if(q[i].k==-1)q[i].r=findx(q[i].r);
 
    //預處理
    tot=0;
    build(1,bn); ///注意0號樹直接建
    rep(i,0,n)t[i]=1;
    rep(i,1,n)update(i,a[i],1);
 
    rep(i,1,qn){
        if(q[i].k==-1){
            update(q[i].l,a[q[i].l],-1);  ///update注意寫對
            update(q[i].l,q[i].r,1);
            a[q[i].l]=q[i].r;
        }
        else{
            printf("%d\n",b[find_kth(q[i].l,q[i].r,q[i].k)]);
        }
    }
 
    return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章