[BZOJ 1901][ZOJ 2112]Dynamic Rankings(树状数组套主席树、动态区间第k大值查询)

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112

思路

动态单点修改求区间第k大数,就是个树状数组套主席树的裸题。
树状数组中每一位维护的是一个前缀的线段树,每个线段树保存的都是对应前缀里每个离散化后的数字的出现次数。
具体细节还是看别人的主席树的讲义吧。。。
下面的代码应该能过BZOJ 1901,但是由于空间复杂度是 O(nlog2n) 的,而ZOJ丧病地只给了30MB内存,序列长度的范围大到50000,直接RE跪掉了。。。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXN 51000
#define MAXM 3000000
#define lowbit(x) ((x)&(-(x)))

using namespace std;

int root[MAXN]; //root[i]=前缀i的根
int hash[MAXN],tot=0,val[MAXN]; //hash[i]=所有数中第i小的数
int stack[MAXN],top=0;
int sum[MAXM],lc[MAXM],rc[MAXM],nCount=0;
int nodeL[MAXN],nodeR[MAXN],K[MAXN],totL,totR; //当前做query操作时,需要求nodeL[1]~nodeL[totL](BIT中L-1的前缀)、nodeR[1]~nodeR[totR](BIT中R-1的前缀)这些线段树的出现数字和,若第i次询问的是查询,则查询区间是[A[i],B[i]],若是修改,则修改nodeL[i]位置的值为nodeR[i]
int A[11000],B[11000];
bool flag[11000]; //flag[i]=true表示第i次询问是查询

int findhash(int x) //查询数字x在所有数字中的排名(求离散化后的x)
{
    int lowerBound=1,upperBound=top;
    while(lowerBound<=upperBound)
    {
        int mid=(lowerBound+upperBound)>>1;
        if(hash[mid]==x) return mid;
        if(hash[mid]>x) upperBound=mid-1;
        else lowerBound=mid+1;
    }
    return lowerBound;
}

void update(int fa,int &o,int L,int R,int pos,int val) //在代表区间[L,R]的结点o里修改位置pos的值为val,该节点父亲为fa
{
    o=++nCount;
    sum[o]=sum[fa]+val;
    lc[o]=lc[fa],rc[o]=rc[fa];
    if(L==R) return;
    int M=(L+R)>>1;
    if(pos<=M) update(lc[fa],lc[o],L,M,pos,val);
    else update(rc[fa],rc[o],M+1,R,pos,val);
}

int query(int L,int R,int k) //查询区间[L,R]的第k大数(离散化后的数)
{
    if(L==R) return L;
    int sumL=0,sumR=0; //sumL=左子树里所有数字出现次数之和,sumR=右子树里所有数字出现次数之和
    for(int i=1;i<=totL;i++) sumL+=sum[lc[nodeL[i]]];
    for(int i=1;i<=totR;i++) sumR+=sum[lc[nodeR[i]]]; //!!!!!
    int M=(L+R)>>1;
    if(sumR-sumL>=k)
    {
        for(int i=1;i<=totL;i++) nodeL[i]=lc[nodeL[i]];
        for(int i=1;i<=totR;i++) nodeR[i]=lc[nodeR[i]];
        return query(L,M,k);
    }
    else
    {
        for(int i=1;i<=totL;i++) nodeL[i]=rc[nodeL[i]];
        for(int i=1;i<=totR;i++) nodeR[i]=rc[nodeR[i]];
        return query(M+1,R,k-(sumR-sumL));
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(flag,0,sizeof(flag));
        memset(sum,0,sizeof(sum));
        memset(root,0,sizeof(root));
        nCount=0;
        tot=top=0;
        int n,m;
        char cmd[3];
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            stack[++top]=val[i];
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%s",cmd);
            scanf("%d%d",&A[i],&B[i]);
            if(cmd[0]=='Q') { scanf("%d",&K[i]); flag[i]=true; }
            else stack[++top]=B[i];
        }
        sort(stack+1,stack+top+1); //对所有数离散化,离散化后的所有数字范围是[1,tot]
        hash[++tot]=stack[1];
        for(int i=2;i<=top;i++)
            if(stack[i]!=stack[i-1])
                hash[++tot]=stack[i];
        //建立初始的主席树
        for(int i=1;i<=n;i++)
        {
            int tmp=findhash(val[i]);
            for(int j=i;j<=n;j+=lowbit(j)) //树状数组套主席树,这里的树状数组维护的是前缀,每个前缀对应一个线段树
                update(root[j],root[j],1,tot,tmp,1);
        }
        //离线回答询问
        for(int i=1;i<=m;i++)
        {
            if(flag[i])
            {
                totL=0,totR=0,A[i]--;
                for(int j=A[i];j>0;j-=lowbit(j))
                    nodeL[++totL]=root[j];
                for(int j=B[i];j>0;j-=lowbit(j))
                    nodeR[++totR]=root[j];
                printf("%d\n",hash[query(1,tot,K[i])]);
            }
            else
            {
                int tmp=findhash(val[A[i]]); //tmp=被修改的那个位置的数字原来的值
                for(int j=A[i];j<=n;j+=lowbit(j))
                    update(root[j],root[j],1,tot,tmp,-1);
                val[A[i]]=B[i];
                tmp=findhash(val[A[i]]); //此时tmp=被修改的那个位置的数字的新值
                for(int j=A[i];j<=n;j+=lowbit(j))
                    update(root[j],root[j],1,tot,tmp,1);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值