#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<string.h>
#include<vector>
#include<set>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
ll n,m,mark[4*maxn],tree[4*maxn],A[maxn];
inline ll read()
{
ll ans=0;
char c=getchar();
while(!isdigit(c))
{
c=getchar();
}
while(isdigit(c))
{
ans=ans*10+c-'0';
c=getchar();
}
return ans;
}
void build (ll l=1,ll r=n,ll p=1)
{
if(l==r)tree[p]=A[l];
else
{
ll mid=(l+r)/2;
build(l,mid,2*p);
build(mid+1,r,2*p+1);
tree[p]=tree[2*p]+tree[2*p+1];
}
}
void push_down(ll p,ll len)
{
mark[2*p]+=mark[p];
mark[2*p+1]+=mark[p];
tree[2*p]+=mark[p]*(len-len/2);
tree[2*p+1]+=mark[p]*(len/2);
mark[p]=0;
}
void update(ll l,ll r,ll d,ll p=1,ll cl=1,ll cr=n)
{
if(cl>r||cr<l)
return;
else if(cl>=l&&cr<=r)
{
tree[p]+=d*(cr-cl+1);
if(cr>cl)mark[p]+=d;
}
else
{
ll mid=(cl+cr)/2;
push_down(p,cr-cl+1);
update(l,r,d,2*p,cl,mid);
update(l,r,d,2*p+1,mid+1,cr);
tree[p]=tree[2*p]+tree[2*p+1];
}
}
ll query(ll l,ll r,ll p=1,ll cl=1,ll cr=n)
{
if(cl>r||cr<l)
return 0;
else if(cl>=l&&cr<=r)
{
return tree[p];
}
else
{
ll mid=(cl+cr)/2;
push_down(p,cr-cl+1);
return query(l,r,2*p,cl,mid)+query(l,r,2*p+1,mid+1,cr);
}
}
int main()
{
n=read();
m=read();
for(int i=1;i<=n;i++)
{
A[i]=read();
}
build();
for(int i=0;i<m;i++)
{
ll oper=read(),l=read(),r=read();
if(oper==1)
{
ll d=read();
update(l,r,d);
}
else
{
printf("%lld\n",query(l,r));
}
}
return 0;
}
08-09
1万+
12-14
09-18