# 【BZOJ4552】排序（线段树，二分答案）

BZOJ

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 222222
#define lson (now<<1)
#define rson (now<<1|1)
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Work{int l,r,opt;}p[MAX];
int t[MAX<<2],tag[MAX<<2];
int a[MAX],b[MAX],n,m,Q;
void Build(int now,int l,int r)
{
tag[now]=-1;
if(l==r){t[now]=b[l];return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
t[now]=t[lson]+t[rson];
}
void puttag(int now,int l,int r,int w)
{
tag[now]=w;
t[now]=(r-l+1)*w;
}
void pushdown(int now,int l,int r)
{
if(tag[now]==-1)return;
int mid=(l+r)>>1;
puttag(lson,l,mid,tag[now]);
puttag(rson,mid+1,r,tag[now]);
tag[now]=-1;
}
int Query(int now,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return t[now];
pushdown(now,l,r);
int ret=0,mid=(l+r)>>1;
if(L<=mid)ret+=Query(lson,l,mid,L,R);
if(R>mid)ret+=Query(rson,mid+1,r,L,R);
return ret;
}
void Modify(int now,int l,int r,int L,int R,int w)
{
if(L<=l&&r<=R){puttag(now,l,r,w);return;}
pushdown(now,l,r);
int mid=(l+r)>>1;
if(L<=mid)Modify(lson,l,mid,L,R,w);
if(R>mid)Modify(rson,mid+1,r,L,R,w);
t[now]=t[lson]+t[rson];
}
void Work(int i)
{
int ss=Query(1,1,n,p[i].l,p[i].r);
if(p[i].opt==0)
{
ss=p[i].r-p[i].l+1-ss;
if(ss!=0)Modify(1,1,n,p[i].l,p[i].l+ss-1,0);
if(ss!=p[i].r-p[i].l+1)Modify(1,1,n,p[i].l+ss,p[i].r,1);
}
else
{
if(ss!=0)Modify(1,1,n,p[i].l,p[i].l+ss-1,1);
if(ss!=p[i].r-p[i].l+1)Modify(1,1,n,p[i].l+ss,p[i].r,0);
}
}
bool check(int mid)
{
for(int i=1;i<=n;++i)if(a[i]>=mid)b[i]=1;else b[i]=0;
Build(1,1,n);
for(int i=1;i<=m;++i)Work(i);
return Query(1,1,n,Q,Q);
}
int main()
{