# BZOJ4552：[HEOI2016/TJOI2016]排序——题解

https://www.lydsy.com/JudgeOnline/problem.php?id=4552

https://www.luogu.org/problemnew/show/P2824

（好的啥也不会的我瑟瑟发抖……）

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef double dl;
const int N=1e5+5;
int X=0,w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
struct data{
int op,l,r;
}q[N];
int n,m,p,b[N],c[N];
int tr[N*4],lazy[N*4];
inline void push(int a,int l,int r){
if(lazy[a]==-1)return;
int mid=(l+r)>>1;
tr[a<<1]=(mid-l+1)*lazy[a];tr[a<<1|1]=(r-mid)*lazy[a];
lazy[a<<1]=lazy[a<<1|1]=lazy[a];
lazy[a]=-1;
}
inline void build(int a,int l,int r){
if(l==r){
tr[a]=c[l];
return;
}
int mid=(l+r)>>1;
build(a<<1,l,mid);build(a<<1|1,mid+1,r);
tr[a]=tr[a<<1]+tr[a<<1|1];
}
inline int query(int a,int l,int r,int l1,int r1){
if(r<l1||r1<l)return 0;
if(l1<=l&&r<=r1)return tr[a];
int mid=(l+r)>>1;
push(a,l,r);
return query(a<<1,l,mid,l1,r1)+query(a<<1|1,mid+1,r,l1,r1);
}
inline void modify(int a,int l,int r,int l1,int r1,int w){
if(r<l1||r1<l)return;
if(l1<=l&&r<=r1){
tr[a]=(r-l+1)*w;
lazy[a]=w;
return;
}
int mid=(l+r)>>1;
push(a,l,r);
modify(a<<1,l,mid,l1,r1,w);modify(a<<1|1,mid+1,r,l1,r1,w);
tr[a]=tr[a<<1]+tr[a<<1|1];
}
bool check(int k){
memset(lazy,-1,sizeof(lazy));
for(int i=1;i<=n;i++)
if(b[i]>=k)c[i]=1;
else c[i]=0;
build(1,1,n);
for(int i=1;i<=m;i++){
int l=q[i].l,r=q[i].r;
int cnt=query(1,1,n,l,r);
if(!q[i].op){
modify(1,1,n,l,r-cnt,0);
modify(1,1,n,r-cnt+1,r,1);
}else{
modify(1,1,n,l,l+cnt-1,1);
modify(1,1,n,l+cnt,r,0);
}
}
return query(1,1,n,p,p);
}
int main(){
for(int i=1;i<=m;i++){
}
int l=1,r=n;
while(l<r){
int mid=(l+r+1)>>1;
if(check(mid))l=mid;
else r=mid-1;
}
printf("%d\n",l);
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者：luyouqi233。 　　　　　　　　　　　　　　+

+欢迎访问我的博客：http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

posted @ 2018-05-01 10:28  luyouqi233  阅读(202)  评论(0编辑  收藏  举报