二分一个值,然后线段树上模拟。

#include<bits/stdc++.h>
#define lson (o<<1)
#define rson (o<<1|1)
const int N=1e5+;
using namespace std;
int n,m,a[N],x,q;
struct Opt{int opt,l,r,id;}b[N];
struct Segment_Tree{
int sumv[N<<],setv[N<<];
inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];}
inline void pushdown(int o,int l,int r){
if(setv[o]==-)return;
int tag=setv[o],mid=(l+r)>>;
setv[lson]=tag;sumv[lson]=tag*(mid-l+);
setv[rson]=tag;sumv[rson]=tag*(r-mid);
setv[o]=-;
}
inline void build(int o,int l,int r){
sumv[o]=;setv[o]=-;
if(l==r){sumv[o]=a[l]>x;return;}
int mid=(l+r)>>;
build(lson,l,mid);build(rson,mid+,r);
pushup(o);
}
int querysum(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return sumv[o];
int mid=(l+r)>>,ans=;
pushdown(o,l,r);
if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr);
if(qr>mid)ans+=querysum(rson,mid+,r,ql,qr);
return ans;
}
void iset(int o,int l,int r,int ql,int qr,int v){
if(ql<=l&&r<=qr){setv[o]=v;sumv[o]=v*(r-l+);return;}
int mid=(l+r)>>;
pushdown(o,l,r);
if(ql<=mid)iset(lson,l,mid,ql,qr,v);
if(qr>mid)iset(rson,mid+,r,ql,qr,v);
pushup(o);
}
}T;
inline bool check(int vv){
x=vv;
T.build(,,n);
for(int i=;i<=m;i++){
int opt=b[i].opt,l=b[i].l,r=b[i].r;
int tmp=T.querysum(,,n,l,r);
if(opt==){
T.iset(,,n,l,r-tmp,);
T.iset(,,n,r-tmp+,r,);
}
else{
T.iset(,,n,l,l+tmp-,);
T.iset(,,n,l+tmp,r,);
}
}
return !T.querysum(,,n,q,q);
}
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int main(){
n=read();m=read();for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<=m;i++){b[i].opt=read();b[i].l=read();b[i].r=read();}
q=read();int l=,r=n;
while(l<r){
int mid=(l+r)>>;
if(check(mid))r=mid;
else l=mid+;
}
printf("%d\n",r);
}
05-22 08:51