cpp
复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
using ll = long long;
ll s[N];
ll b[N];
ll w[N];
ll add[N];
int l[N],r[N];
int tot,n,len,q;
int belong[N];
inline void init()
{
len = sqrt(n),tot = (n+len-1)/len;
for(int i=1;i<=tot;i++)
l[i] = r[i-1]+1,r[i] = i*len;
for(int i=1;i<=tot;i++){
for(int j=l[i];j<=r[i];j++)
b[i]+=w[j],belong[j] = i;
s[i] = s[i-1] + b[i];
}
}
inline void modify(int ql,int qr,int c){
int p = belong[ql],q = belong[qr];
if(p==q){
for(int i=ql;i<=qr;i++)w[i]+=c,b[p]+=c;
for(int i=p;i<=tot;i++)s[i] = s[i-1]+b[i];
return;
}
for(int i=ql;i<=r[p];i++)w[i]+=c,b[p]+=c;
for(int i=qr;i>=l[q];--i)w[i]+=c,b[q]+=c;
for(int i=p+1;i<=q-1;i++)add[i]+=c,b[i]+=(ll)(r[i]-l[i]+1)*c;
for(int i=p;i<=tot;i++)s[i] = s[i-1]+b[i];
}
inline ll query(int ql,int qr){
int p = belong[ql],q = belong[qr];
ll res = 0;
if(p==q){
for(int i=ql;i<=qr;i++)res+=w[i]+add[p];
return res;
}
res = s[q-1]-s[p];
for(int i=ql;i<=r[p];++i)res+=w[i]+add[p];
for(int i=qr;i>=l[q];--i)res+=w[i]+add[q];
return res;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>w[i];
init();
while(q--){
int op,ql,qr;cin>>op>>ql>>qr;
if(op==1){
int c;
cin>>c;modify(ql,qr,c);
}
else cout<<query(ql,qr)<<"\n";
}
}