zoukankan      html  css  js  c++  java
  • bzoj1901: Zju2112 Dynamic Rankings 主席树+树状数组

    主席树+树状数组求带修改的区间第k大

    #include<bits/stdc++.h>
    using namespace std;
    int n,m,root[200100],q1[100000],q2[100000],tot=0,dd,len=0,a[100010],hash[200010],sum[5001000],l[5010000],r[5010000],b[1000100],c[1000010],d[1000010];
    char s[10010];
    int lowbit(int pp)
    {return pp&(-pp);}
    void update(int &rt,int L,int R,int x,int val)
    {
        if(!rt)rt=++len;
        sum[rt]+=val;
        if(L<R)
        {
            int mid=(L+R)>>1;
            if(x<=mid)update(l[rt],L,mid,x,val);
            else update(r[rt],mid+1,R,x,val);
        }
    }
    int main()
    {
        //freopen("xf.in","r",stdin);
        //freopen("xf.out","w",stdout);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            hash[++tot]=a[i];
        }
        for(int i=1;i<=m;i++)
        {
            cin>>s[i];
            if(s[i]=='Q')scanf("%d%d%d",&b[i],&c[i],&d[i]);
            else {scanf("%d%d",&b[i],&c[i]);hash[++tot]=c[i];}
        }
        sort(hash+1,hash+tot+1);
        dd=unique(hash+1,hash+tot+1)-hash-1;
        for(int i=1;i<=n;i++)
        {
            int x=lower_bound(hash+1,hash+dd+1,a[i])-hash;
            for(int j=i;j<=n;j+=lowbit(j))update(root[j],1,dd,x,1);
        }
        for(int i=1;i<=m;i++)
        {
            if(s[i]=='Q')
            {
                int xx=b[i]-1,yy=c[i],L=1,R=dd;
                int x=d[i];
                int tail1=0,tail2=0;
                while(xx)
                    {
                        q1[++tail1]=root[xx];xx-=lowbit(xx);
                    }
                    while(yy)
                    {
                        q2[++tail2]=root[yy];yy-=lowbit(yy);
                    }
                while(L<R)
                {
                     
                    int ans=0;
                    for(int j=1;j<=tail1;j++)ans-=sum[l[q1[j]]];
                    for(int j=1;j<=tail2;j++)ans+=sum[l[q2[j]]];
                    int mid=(L+R)>>1;
                    if(x<=ans)
                    {
                        R=mid;
                         
                        for(int j=1;j<=tail1;j++)
                            q1[j]=l[q1[j]];
                        for(int j=1;j<=tail2;j++)
                            q2[j]=l[q2[j]];
                    }
                    else
                    {
                        L=mid+1;
                        x-=ans;
                        for(int j=1;j<=tail1;j++)
                            q1[j]=r[q1[j]];
                        for(int j=1;j<=tail2;j++)
                            q2[j]=r[q2[j]];
                    }
                }
                printf("%d
    ",hash[L]);
            }
            else
            {
                int x=lower_bound(hash+1,hash+dd+1,a[b[i]])-hash;
                for(int j=b[i];j<=n;j+=lowbit(j))update(root[j],1,dd,x,-1);
                x=lower_bound(hash+1,hash+dd+1,c[i])-hash;
                for(int j=b[i];j<=n;j+=lowbit(j))update(root[j],1,dd,x,1);
                a[b[i]]=c[i];
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    cd 好吃的 收藏
    2011 无代码无意义…test 指针 v1
    转 云中漫步的 电子书 from simon
    2011无代码无意义 test_gets_scanf连用 等
    svn—relocate 的原因
    转 CString,string,char*的综合比较
    2011 无代码无意义 test_内存之 变量的边界 (图)
    转 解决"应用程序配置不正确,程序无法启动"
    转 删除已存在的SVN账户信息
    C#中IO类FileInfo和Directory操作实例
  • 原文地址:https://www.cnblogs.com/mybing/p/8553361.html
Copyright © 2011-2022 走看看