这个题,*800 吧。
这个序列的操作其实很弱智,考虑我们其实是对一个这样的一个序列操作:
,把前后缀 减一。
有 conor case,如减完之后 序列会出现变化:
4 4 3 4,前缀操作 变为 3 3 3 4。
变为 3 3 3 4,但是我们只给 的 减去了 ,需继续覆盖 直到存在一个数大于它。
这种情况当且仅当 不是当前最大值时出现。
简易实现可以 BIT+ST 表直接求,因为变化量很小。
我实现了一个比较复杂的代码,包括动态把 序列添加进 序列之类,这个还可以求区间和,区间最大值等。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll1;
const ll1 mod=19260817;
const int N=3e5+5;
const int M=2e6+5;
const int inf=1e9+7;
int a[N];
//维护:c_i=min(a[i],cov)
#define ls (o<<1)
#define rs (ls|1)
#define mid ((l+r)>>1)
namespace sgt{
int L[M],R[M];
int mxa[M];//目前 a 序列的最大值
int mnf[M];//c[i]-a[i],报警用
int c[M];//min(premx,sufmx) 序列的最大值
int mx[M];//a[i]=c[i] 部分的最大值
int cntmx[M];
#define rmx(o) (max(mx[o],mxa[o]))
int cov[M],ad[M];
void Cov(int o,int x){
cov[o]=x,ad[o]=0,c[o]=x;
mnf[o]=x-mxa[o];
if(cntmx[o])mx[o]=cov[o];
}
void Add(int o,int x){
mnf[o]+=x;
c[o]+=x;
if(cntmx[o])mx[o]+=x;
ad[o]+=x;
}
void pushup(int o){
mx[o]=max(mx[ls],mx[rs]);
c[o]=max(c[ls],c[rs]);
mxa[o]=max(mxa[ls],mxa[rs]);
mnf[o]=min(mnf[ls],mnf[rs]);
cntmx[o]=cntmx[ls]+cntmx[rs];
}
void pushdown(int o){
if(cov[o]!=-1)Cov(ls,cov[o]),Cov(rs,cov[o]);
if(ad[o])Add(ls,ad[o]),Add(rs,ad[o]);
cov[o]=-1,ad[o]=0;
}
void build(int o,int l,int r){
L[o]=l,R[o]=r;
cov[o]=-1,ad[o]=0;cntmx[o]=0;
if(l==r){mxa[o]=a[l],c[o]=0,mnf[o]=-a[l],mx[o]=-inf;return;}
build(ls,l,mid),build(rs,mid+1,r);
pushup(o);
}
void cover(int o,int lt,int rt,int x){
int l=L[o],r=R[o];
if(l>=lt&&r<=rt)return Cov(o,x);
pushdown(o);
if(lt<=mid)cover(ls,lt,rt,x);
if(rt> mid)cover(rs,lt,rt,x);
pushup(o);
}
void add(int o,int lt,int rt,int x){
int l=L[o],r=R[o];
if(l>=lt&&r<=rt)return Add(o,x);
pushdown(o);
if(lt<=mid)add(ls,lt,rt,x);
if(rt> mid)add(rs,lt,rt,x);
pushup(o);
}
void check(int o){
assert(mnf[o]>=0);
if(mnf[o]>0)return;
if(L[o]==R[o]){
cntmx[o]=1;
mnf[o]=inf,mxa[o]=-inf;
mx[o]=c[o];
return;
}
pushdown(o);
check(ls),check(rs);
pushup(o);
}
int qryc(int o,int pos){
int l=L[o],r=R[o];
if(l==r)return c[o];
pushdown(o);
return qryc(pos<=mid?ls:rs,pos);
}
int getmxl(){
int o=1,res=0;
while(1){
if(L[o]==R[o]){res=L[o];break;}
pushdown(o);
if(rmx(ls)==rmx(o))o=ls;
else o=rs;
}
return res;
}
int getmxr(){
int o=1,res=0;
while(1){
if(L[o]==R[o]){res=L[o];break;}
pushdown(o);
if(rmx(rs)==rmx(o))o=rs;
else o=ls;
}
return res;
}
int findfir(int lim){
int o=1,res=inf;
while(1){
if(rmx(o)<=lim)break;
if(L[o]==R[o]){res=L[o];break;}
pushdown(o);
if(rmx(ls)>lim)o=ls;
else o=rs;
}
assert(res<inf);
return res;
}
int findlst(int lim){
int o=1,res=0;
while(1){
if(rmx(o)<=lim)break;
if(L[o]==R[o]){res=L[o];break;}
pushdown(o);
if(rmx(rs)>lim)o=rs;
else o=ls;
}
assert(res>0);
return res;
}
int getmx(){return rmx(1);}
}
int n,q,V=0;
int pmx[N],smx[N];
void init(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),V=max(a[i],V);
sgt::build(1,1,n);
for(int i=1;i<=n;i++)pmx[i]=max(pmx[i-1],a[i]);
for(int i=n;i;i--)smx[i]=max(smx[i+1],a[i]);
for(int i=1;i<=n;i++)sgt::cover(1,i,i,min(pmx[i],smx[i]));
sgt::check(1);
}
void work(){
int opt=0;
int x;
while(q--){
scanf("%d%d",&opt,&x);
if(opt==1){
int lt=1,rt=min(sgt::getmxr(),x);
sgt::add(1,lt,rt,-1);
sgt::check(1);
V=sgt::getmx();int nw=sgt::qryc(1,rt);
if(nw<V){
int rt1=sgt::findfir(nw)-1;
assert(rt1>=rt);
sgt::cover(1,rt,rt1,nw);
}
sgt::check(1);
}
if(opt==2){
x=n-x+1;
int rt=n,lt=max(sgt::getmxl(),x);
sgt::add(1,lt,rt,-1);
sgt::check(1);
V=sgt::getmx();int nw=sgt::qryc(1,lt);
if(nw<V){
int lt1=sgt::findlst(nw)+1;
assert(lt1<=lt);
sgt::cover(1,lt1,lt,nw);
}
sgt::check(1);
}
if(opt==3){
int Ans=min(sgt::qryc(1,x),a[x]);
printf("%d\n",Ans);
}
}
}
int main(){
init();
work();
return 0;
}