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;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值