BZOJ 4552 排序

Posted on 2016-06-12 00:03  ziliuziliu  阅读(158)  评论(0编辑  收藏  举报

省选TM都能有BC原题?

。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100500
#define inf 1000000007
using namespace std;
int n,m,a[maxn],l=inf,r=0,mi,q;
int ls[maxn<<2],rs[maxn<<2],lazy[maxn<<2],sum[maxn<<2],root,tot=0;
int pos[maxn],ans;
struct move
{
    int type,l,r;
}p[maxn];
void build(int &now,int left,int right)
{
    now=++tot;lazy[now]=-1;
    if (left==right)
    {
        pos[left]=now;
        if (a[left]>=mi) {lazy[now]=1;sum[now]=1;}
        else {lazy[now]=0;sum[now]=0;}
        return;
    }
    int mid=(left+right)>>1;
    build(ls[now],left,mid);
    build(rs[now],mid+1,right);
    sum[now]=sum[ls[now]]+sum[rs[now]];
}
void pushdown(int now,int left,int right)
{
    if (lazy[now]==-1) return;
    if (left==right) return;
    int mid=(left+right)>>1;
    if (lazy[now]==0)
    {
        lazy[ls[now]]=0;sum[ls[now]]=0;
        lazy[rs[now]]=0;sum[rs[now]]=0;
    }
    else
    {
        lazy[ls[now]]=1;sum[ls[now]]=mid-left+1;
        lazy[rs[now]]=1;sum[rs[now]]=right-mid;
    }
    lazy[now]=-1;
}
int query(int now,int left,int right,int l,int r)
{
    pushdown(now,left,right);
    if ((left==l) && (right==r))
        return sum[now];
    int mid=(left+right)>>1;
    if (r<=mid) return query(ls[now],left,mid,l,r);
    else if (l>=mid+1) return query(rs[now],mid+1,right,l,r);
    else return query(ls[now],left,mid,l,mid)+query(rs[now],mid+1,right,mid+1,r);
}
void modify(int now,int left,int right,int l,int r,int p)
{
    pushdown(now,left,right);
    if ((left==l) && (right==r))
    {
        lazy[now]=p;
        if (p==0) sum[now]=0;
        else sum[now]=right-left+1;
        return;
    }
    int mid=(left+right)>>1;
    if (r<=mid) modify(ls[now],left,mid,l,r,p);
    else if (l>=mid+1) modify(rs[now],mid+1,right,l,r,p);
    else
    {
        modify(ls[now],left,mid,l,mid,p);
        modify(rs[now],mid+1,right,mid+1,r,p);
    }
    sum[now]=sum[ls[now]]+sum[rs[now]];
}
void work0(int x)
{
    int len=p[x].r-p[x].l+1,lenr=query(root,1,n,p[x].l,p[x].r);
    if (p[x].l<=p[x].l+(len-lenr)-1) modify(root,1,n,p[x].l,p[x].l+(len-lenr)-1,0);
    if (p[x].l+(len-lenr)<=p[x].r) modify(root,1,n,p[x].l+(len-lenr),p[x].r,1);
}
void work1(int x)
{
    int len=p[x].r-p[x].l+1,lenl=query(root,1,n,p[x].l,p[x].r);
    if (p[x].l<=p[x].l+lenl-1) modify(root,1,n,p[x].l,p[x].l+lenl-1,1);
    if (p[x].l+lenl<=p[x].r) modify(root,1,n,p[x].l+lenl,p[x].r,0);
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        l=min(l,a[i]);
        r=max(r,a[i]);
    }
    for (int i=1;i<=m;i++)
        scanf("%d%d%d",&p[i].type,&p[i].l,&p[i].r);
    scanf("%d",&q);
    while (l<=r)
    {
        tot=0;
        mi=(l+r)>>1;
        build(root,1,n);
        for (int i=1;i<=m;i++)
        {
            if (p[i].type==0) work0(i);
            else work1(i);
        }
        int regis=query(root,1,n,q,q);
        if (regis==1) {ans=mi;l=mi+1;}
        else r=mi-1;
    }
    printf("%d\n",ans);
    return 0;
}