BZOJ 1901: Zju2112 Dynamic Rankings

应该算是整体二分的裸题了吧。

#include<bits/stdc++.h>
using namespace std;
const int N=10010,M=N<<2,inf=1e9+7;
inline int read()
{
    char ch=getchar(); int x=0,f=1;
    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;
}
struct node{int l,r,c,s,id;}a[M],b[M],br[M];
int n,m,q,nw[N],ans[M],t[N];
inline void add(int x,int u){for(;x<=n;x+=x&-x)t[x]+=u;}
inline int get(int x){int s=0;for(;x;x-=x&-x)s+=t[x];return s;}
inline void solve(int l,int r,int lc,int rc)
{
    if(l>r)return;
    if(lc==rc)
    {
        for(int i=l;i<=r;i++)
            if(a[i].s==2)ans[a[i].id]=lc;
        return;
    }
    int mid=(lc+rc)>>1,bn=0,brn=0,i;
    for(i=l;i<=r;i++)
    {
        if(a[i].s<2)
        {
            if(a[i].c<=mid){b[++bn]=a[i]; add(a[i].l,!a[i].s?1:-1);}
            else br[++brn]=a[i];
        }
        else
        {
            int u=get(a[i].r)-get(a[i].l-1);
            if(u>=a[i].c)b[++bn]=a[i];
            else a[i].c-=u,br[++brn]=a[i];
        }
    }
    for(i=l;i<=r;i++)
        if(a[i].s<2 && a[i].c<=mid)add(a[i].l,!a[i].s?-1:1);
    for(i=1;i<=bn;i++)a[l+i-1]=b[i];
    for(i=1;i<=brn;i++)a[l+bn+i-1]=br[i];
    solve(l,l+bn-1,lc,mid);
    solve(l+bn,r,mid+1,rc);
}
int main()
{
    int i,j,x,y,k; char c;
    n=read(),m=read(),q=0;
    for(i=1;i<=n;i++)
    {
        nw[i]=x=read();
        a[++q]=(node){i,i,x,0,0};
    }
    for(i=1;i<=m;i++)
    {
        scanf("\n%c",&c);
        if(c=='Q')
            a[++q]=(node){read(),read(),read(),2,i};
        else
        {
            x=read(),k=read();
            a[++q]=(node){x,x,nw[x],1,0};
            a[++q]=(node){x,x,nw[x]=k,0,0};
        }
    }
    for(i=1;i<=m;i++)ans[i]=-1;
    solve(1,q,0,inf);
    for(i=1;i<=m;i++)if(ans[i]!=-1)printf("%d\n",ans[i]);
    return 0;
}

你可能感兴趣的:(BZOJ 1901: Zju2112 Dynamic Rankings)