复习:树状数组的区间修改和区间查询~ 理解区间修改,单点查询后这个就很容易理解啦~
原理:超级树状数组
AC代码:
#include<bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%d\n",x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
ll n,m;
ll c1[maxn],c2[maxn];
inline ll read()
{
char x;
ll u;
int flag = 0;
while(x = getchar(),x<'0'||x>'9') if(x == '-') flag = 1;
u = x-'0';
while(x = getchar(),x>='0' && x<='9') u = (u<<3)+(u<<1)+x-'0';
if(flag) u = -u;
return u;
}
inline ll lowbit(ll x){return x&(-x);}
void update(ll *tree,ll x,ll val)
{
for(ll i=x;i<=n;i+=lowbit(i)) tree[i]+=val;
}
ll getSum(ll *tree,ll x)
{
ll res = 0;
for(ll i=x;i;i-=lowbit(i)) res+=tree[i];
return res;
}
ll query(ll x)
{
ll ans1,ans2;
ans1 = x*getSum(c1,x);
ans2 = getSum(c2,x);
return ans1-ans2;
}
ll range_query(ll l,ll r)
{
return query(r)-query(l-1);
}
int main()
{
#ifdef LOCAL_FILE
freopen("in.txt","r",stdin);
#endif // LOCAL_FILE
n = read();m = read();
ll l=0,r;
for(ll i=1;i<=n;i++)
{
r = read();
update(c1,i,r-l);//差分
update(c2,i,(i-1)*(r-l));
l = r;
}
while(m--)
{
int c;
sc(c);
if(c == 1){
ll l,r,x;
l = read();r = read();x = read();
update(c1,l,x);
update(c1,r+1,-x);
update(c2,l,x*(l-1));
update(c2,r+1,-x*(r));
}else{
ll l,r;
l = read(); r = read();
printf("%lld\n",range_query(l,r));
}
}
return 0;
}