简单题,但是代码有点难写。
考虑推式子,考虑点 上有多少雨水。答案是,查询左右两侧的最高柱子的位置 ,则它的答案是 。
写成式子:
最后的减法提出来是好处理的。
考虑经典 trick:。这样我们将 套 转化为了纯 处理:
后面的是区间 ,也是好处理的。
问题转化为求区间前缀最大值之和、后缀最大值之和。这两个是对称的,我们只考虑前缀怎么做。
考虑兔队线段树,不知道的去Link。维护区间 ,区间前缀最大值之和 。难点在于 push up 部分。一般的 push up 我们是用左右儿子 更新的,在兔队线段树里,我们是用类似线段树二分的东西来更新。
首先我们继承左儿子的 ,然后用左儿子的 计算右儿子贡献。称前面计算出来的的最大值是 ,我们需要求 这个东西。若递归到了叶子 ,直接返回 ;若节点 的左儿子最大值 ,那么右儿子一定会全变成 ,只需递归左儿子;否则右儿子一定不会变成 ,使用 的答案 减去左儿子已经计算好的答案 即可。
证明一下这个减法的正确性,我们 push up 到 的时候,调用了右儿子 的线段树二分,所以右儿子全部都计算过了,可以直接使用。因此正确。
push down 和 update 是简单的,不做赘述。
考虑查询。我们的 query 函数返回一个 pair,分别是区间的 和区间的 。如果当前线段树节点 是查询区间 的子集,调用我们 push up 的时候编写的线段树二分函数即可计算。如果点 的左右儿子只有一个是与查询区间 有交,直接递归;否则需要合并左右儿子的 query 答案。我们先计算左儿子的答案,然后用左儿子的最大值查询右儿子的答案,最后返回左右儿子的答案和,以及右儿子的最大值。
然后就做完了!证明时间复杂度,线段树二分是 的,所以整体 。
Code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define mkp make_pair
#define fi first
#define se second
const ll MAXN=3e5+35;
struct sgtree{
ll l,r,sum,maxx,pms,sms,tag;
sgtree(const ll &A=0,const ll &B=0,const ll &C=0,const ll &D=0,const ll &E=0,const ll &F=-4e18,const ll &G=0):l(A),r(B),sum(C),maxx(D),pms(E),sms(F),tag(G){}
};
ll n,q,op,l,r,x;
ll a[MAXN];
sgtree sgt[MAXN<<2];
void read(ll &x){
x=0;
int f=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-'){
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
x*=f;
return;
}
#define ls(p) ((p)<<1)
#define rs(p) ((p)<<1|1)
void pushdown(const ll &p){
if(sgt[p].tag){
sgt[ls(p)].sum+=(sgt[ls(p)].r-sgt[ls(p)].l+1)*sgt[p].tag;
sgt[ls(p)].maxx+=sgt[p].tag;
sgt[ls(p)].pms+=(sgt[ls(p)].r-sgt[ls(p)].l+1)*sgt[p].tag;
sgt[ls(p)].sms+=(sgt[ls(p)].r-sgt[ls(p)].l+1)*sgt[p].tag;
sgt[ls(p)].tag+=sgt[p].tag;
sgt[rs(p)].sum+=(sgt[rs(p)].r-sgt[rs(p)].l+1)*sgt[p].tag;
sgt[rs(p)].maxx+=sgt[p].tag;
sgt[rs(p)].pms+=(sgt[rs(p)].r-sgt[rs(p)].l+1)*sgt[p].tag;
sgt[rs(p)].sms+=(sgt[rs(p)].r-sgt[rs(p)].l+1)*sgt[p].tag;
sgt[rs(p)].tag+=sgt[p].tag;
sgt[p].tag=0;
}
return;
}
ll queryp(const ll &p,const ll &c){
if(sgt[p].l==sgt[p].r) return max(sgt[p].maxx,c);
pushdown(p);
if(sgt[ls(p)].maxx<=c) return c*(sgt[ls(p)].r-sgt[ls(p)].l+1)+queryp(rs(p),c);
return queryp(ls(p),c)+sgt[p].pms-sgt[ls(p)].pms;
}
ll querys(const ll &p,const ll &c){
if(sgt[p].l==sgt[p].r) return max(sgt[p].maxx,c);
pushdown(p);
if(sgt[rs(p)].maxx<=c) return c*(sgt[rs(p)].r-sgt[rs(p)].l+1)+querys(ls(p),c);
return querys(rs(p),c)+sgt[p].sms-sgt[rs(p)].sms;
}
void pushup(const ll &p){
sgt[p].sum=sgt[ls(p)].sum+sgt[rs(p)].sum;
sgt[p].maxx=max(sgt[ls(p)].maxx,sgt[rs(p)].maxx);
sgt[p].pms=sgt[ls(p)].pms+queryp(rs(p),sgt[ls(p)].maxx);
sgt[p].sms=sgt[rs(p)].sms+querys(ls(p),sgt[rs(p)].maxx);
return;
}
void build(const ll &l,const ll &r,const ll &p){
sgt[p]=sgtree(l,r);
if(l==r){
sgt[p].sum=sgt[p].maxx=sgt[p].pms=sgt[p].sms=a[l];
return;
}
ll mid=l+r>>1;
build(l,mid,ls(p));
build(mid+1,r,rs(p));
pushup(p);
return;
}
void update(const ll &ql,const ll &qr,const ll &p,const ll &c){
if(sgt[p].l>=ql&&sgt[p].r<=qr){
sgt[p].sum+=(sgt[p].r-sgt[p].l+1)*c;
sgt[p].maxx+=c;
sgt[p].pms+=(sgt[p].r-sgt[p].l+1)*c;
sgt[p].sms+=(sgt[p].r-sgt[p].l+1)*c;
sgt[p].tag+=c;
return;
}
pushdown(p);
if(sgt[ls(p)].r>=ql) update(ql,qr,ls(p),c);
if(sgt[rs(p)].l<=qr) update(ql,qr,rs(p),c);
pushup(p);
return;
}
pll query1(const ll &ql,const ll &qr,const ll &p,const ll &c){
if(sgt[p].l>=ql&&sgt[p].r<=qr) return mkp(queryp(p,c),max(sgt[p].maxx,c));
pushdown(p);
if(sgt[ls(p)].r<ql) return query1(ql,qr,rs(p),c);
if(sgt[rs(p)].l>qr) return query1(ql,qr,ls(p),c);
pll res1,res2;
res1=query1(ql,qr,ls(p),c);
res2=query1(ql,qr,rs(p),res1.se);
return mkp(res1.fi+res2.fi,res2.se);
}
pll query2(const ll &ql,const ll &qr,const ll &p,const ll &c){
if(sgt[p].l>=ql&&sgt[p].r<=qr) return mkp(querys(p,c),max(sgt[p].maxx,c));
pushdown(p);
if(sgt[ls(p)].r<ql) return query2(ql,qr,rs(p),c);
if(sgt[rs(p)].l>qr) return query2(ql,qr,ls(p),c);
pll res1,res2;
res1=query2(ql,qr,rs(p),c);
res2=query2(ql,qr,ls(p),res1.se);
return mkp(res1.fi+res2.fi,res2.se);
}
ll query3(const ll &ql,const ll &qr,const ll &p){
if(sgt[p].l>=ql&&sgt[p].r<=qr) return sgt[p].maxx;
pushdown(p);
ll res=-4e18;
if(sgt[ls(p)].r>=ql) res=max(query3(ql,qr,ls(p)),res);
if(sgt[rs(p)].l<=qr) res=max(query3(ql,qr,rs(p)),res);
return res;
}
ll query4(const ll &ql,const ll &qr,const ll &p){
if(sgt[p].l>=ql&&sgt[p].r<=qr) return sgt[p].sum;
pushdown(p);
ll res=0;
if(sgt[ls(p)].r>=ql) res+=query4(ql,qr,ls(p));
if(sgt[rs(p)].l<=qr) res+=query4(ql,qr,rs(p));
return res;
}
#undef ls
#undef rs
int main(){
read(n);read(q);
for(ll i=1;i<=n;i++) read(a[i]);
build(1,n,1);
while(q--){
read(op);read(l);read(r);
l++,r++;
if(op==0){
read(x);
update(l,r,1,x);
}
else cout<<query1(l,r,1,-4e18).fi+query2(l,r,1,-4e18).fi-query3(l,r,1)*(r-l+1)-query4(l,r,1)<<'\n';
}
return 0;
}