哇二分..真厉害!
每次二分一个数,然后把小于x的数当做0,大于的当做1
然后对于每一段,想把它排好序,也就是把1和0排好序,也就是把部分改成1、部分改成0。而这一段里面有几个1就可以区间和解决了。
即如果已经求出这段有cnt个1,升序操作[l,r-cnt]=0,[r-cnt+1,r]=1;降序操作[l,l+cnt-1]=1,[l-cnt,r]=0
#include
#define N 100010
int n,m,ans,k,a[N];
struct node{int op,x,y;}b[N];
struct node1{int l,r,v,sum,f;}tree[N*4];
inline int read(){
int x=0,f=1;char ch=getchar();
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;
}
void update(int v){
if(tree[v].f!=-1) tree[v].sum=tree[v].f*(tree[v].r-tree[v].l+1);
else tree[v].sum=tree[v<<1].sum+tree[v<<1|1].sum;
}
void pushdown(int v){
if(tree[v].f==-1) return;
tree[v<<1].v=tree[v<<1|1].v=tree[v<<1].f=tree[v<<1|1].f=tree[v].f;
tree[v<<1].sum=tree[v].f*(tree[v<<1].r-tree[v<<1].l+1);
tree[v<<1|1].sum=tree[v].f*(tree[v<<1|1].r-tree[v<<1|1].l+1);
tree[v].f=-1;
}
void build(int v,int l,int r,int x){
tree[v].l=l;tree[v].r=r;tree[v].f=-1;
if(l==r){
tree[v].v=tree[v].sum=a[l]>=x;
return;
}int mid=l+r>>1;
build(v<<1,l,mid,x);build(v<<1|1,mid+1,r,x);
update(v);
}
int query(int v,int l,int r){
if(l<=tree[v].l && tree[v].r<=r) return tree[v].sum;
pushdown(v);
int mid=tree[v].l+tree[v].r>>1,s1=0,s2=0;
if(l<=mid) s1=query(v<<1,l,r);
if(mid1|1,l,r);
return s1+s2;
}
void add(int v,int l,int r,int x){
if(l<=tree[v].l && tree[v].r<=r){
tree[v].v=x;tree[v].sum=x*(tree[v].r-tree[v].l+1);tree[v].f=x;
return;
}pushdown(v);
int mid=tree[v].l+tree[v].r>>1;
if(l<=mid) add(v<<1,l,r,x);
if(mid1|1,l,r,x);
update(v);
}
bool check(int x){
build(1,1,n,x);
for(int i=1;i<=m;i++){
int y=query(1,b[i].x,b[i].y);
if(!y || y==b[i].y-b[i].x+1) continue;
if(b[i].op) add(1,b[i].x,b[i].x+y-1,1),add(1,b[i].x+y,b[i].y,0);
else add(1,b[i].x,b[i].y-y,0),add(1,b[i].y-y+1,b[i].y,1);
}
return query(1,k,k);
}
int main(){
freopen("a.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=m;i++) b[i].op=read(),b[i].x=read(),b[i].y=read();
k=read();int l=1,r=n;
while(l<=r){
int mid=l+r>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}printf("%d\n",ans);
return 0;
}