#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
struct node{
int sz,sum,lenl,lenr,vall,valr;
}tr[N<<2];
int n,q,a[N];
node operator + (node a,node b){
node ret;
ret.sz=a.sz+b.sz;
ret.sum=a.sum+b.sum+(a.valr<=b.vall?a.lenr*b.lenl:0);
// cout<<a.valr<<" "<<b.vall<<endl;
ret.lenl=a.lenl+(a.lenl==a.sz&&a.valr<=b.vall?b.lenl:0);
ret.lenr=b.lenr+(b.lenr==b.sz&&a.valr<=b.vall?a.lenr:0);
ret.vall=a.vall;ret.valr=b.valr;
return ret;
}
void pushup(int x){
tr[x]=tr[x<<1]+tr[x<<1|1];
}
void build(int p,int l,int r){
if(l>r) return;
if(l==r){
// cout<<p<<" "<<a[l]<<endl;
tr[p]={1,1,1,1,a[l],a[l]};
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
void update(int p,int l,int r,int pos,int x){
if(l>r) return ;
if(l==r){
tr[p].vall=tr[p].valr=x;
return ;
}
int mid=(l+r)>>1;
if(mid<pos) update(p<<1|1,mid+1,r,pos,x);
else update(p<<1,l,mid,pos,x);
pushup(p);
}
node query(int p,int l,int r,int ll,int rr){
// cout<<p<<endl;
// cout<<l<<" "<<r<<" "<<p<<" "<<tr[p].sum<<endl;
if(ll<=l&&r<=rr){
return tr[p];
}
int mid=(l+r)>>1;
if(ll>mid) return query(p<<1|1,mid+1,r,ll,rr);
else if(rr<=mid) return query(p<<1,l,mid,ll,rr);
else{
return query(p<<1,l,mid,ll,rr)+query(p<<1|1,mid+1,r,ll,rr);
}
}
int32_t main(){
scanf("%lld%lld",&n,&q);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,1,n);
// cout<<tr[5].vall<<" "<<tr[5].valr<<endl;
// node tp=tr[5]+tr[9];
// cout<<tp.sum<<endl;
// cout<<tr[1].sum<<endl;
while(q--){
int op,x,y;
scanf("%lld%lld%lld",&op,&x,&y);
if(op==1){
update(1,1,n,x,y);
}else{
cout<<query(1,1,n,x,y).sum<<endl;
}
}
}
E. Non-Decreasing Dilemma
最新推荐文章于 2021-11-20 13:12:14 发布