二分一个值,然后线段树上模拟。
#include<bits/stdc++.h> #define lson (o<<1) #define rson (o<<1|1) const int N=1e5+10; 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<<2],setv[N<<2]; inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];} inline void pushdown(int o,int l,int r){ if(setv[o]==-1)return; int tag=setv[o],mid=(l+r)>>1; setv[lson]=tag;sumv[lson]=tag*(mid-l+1); setv[rson]=tag;sumv[rson]=tag*(r-mid); setv[o]=-1; } inline void build(int o,int l,int r){ sumv[o]=0;setv[o]=-1; if(l==r){sumv[o]=a[l]>x;return;} int mid=(l+r)>>1; build(lson,l,mid);build(rson,mid+1,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)>>1,ans=0; pushdown(o,l,r); if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr); if(qr>mid)ans+=querysum(rson,mid+1,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+1);return;} int mid=(l+r)>>1; pushdown(o,l,r); if(ql<=mid)iset(lson,l,mid,ql,qr,v); if(qr>mid)iset(rson,mid+1,r,ql,qr,v); pushup(o); } }T; inline bool check(int vv){ x=vv; T.build(1,1,n); for(int i=1;i<=m;i++){ int opt=b[i].opt,l=b[i].l,r=b[i].r; int tmp=T.querysum(1,1,n,l,r); if(opt==0){ T.iset(1,1,n,l,r-tmp,0); T.iset(1,1,n,r-tmp+1,r,1); } else{ T.iset(1,1,n,l,l+tmp-1,1); T.iset(1,1,n,l+tmp,r,0); } } return !T.querysum(1,1,n,q,q); } inline int read(){ int f=1,x=0;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); return f*x; } int main(){ n=read();m=read();for(int i=1;i<=n;i++)a[i]=read(); for(int i=1;i<=m;i++){b[i].opt=read();b[i].l=read();b[i].r=read();} q=read();int l=1,r=n; while(l<r){ int mid=(l+r)>>1; if(check(mid))r=mid; else l=mid+1; } printf("%d\n",r); }