终于抽出时间学了一发树状数组的区间修改
用
c[i]=a[i]−a[i−1]
很容易得到:
a[i]=∑i=1ci
s[i]=(i−1)⋅c[i]
那么到n的答案为
ans=(n−1)⋅∑i=1ci−∑i=1si
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,m;
ll delta[N],deltai[N],sum[N],a[N];
#define lowbit(x) ((x)&(-x))
ll query(ll *array,int pos)
{
ll temp = 0ll;
while(pos > 0) {
temp += array[pos];
pos -= lowbit(pos);
}
return temp;
}
void update(ll *array,int pos,int x)
{
while(pos <= N)
{
array[pos] += x;
pos += lowbit(pos);
}
}
int main()
{
scanf("%d",&n);
fo(i,1,n) {scanf("%lld",&a[i]);sum[i]=sum[i-1]+a[i];}
scanf("%d",&m);
for(int op,l,r,x,i=1;i<=m;i++) {
scanf("%d",&op);
if(op==1) {
scanf("%d%d%d",&l,&r,&x);
update(delta,l,x);
update(delta,r+1,-x);
update(deltai,l,x*l);
update(deltai,r+1,-x*(r+1));
}
else {
scanf("%d%d",&l,&r);
ll suml=sum[l-1]+l*query(delta,l-1)-query(deltai,l-1);
ll sumr=sum[r]+(r+1)*query(delta,r)-query(deltai,r);
printf("%lld\n",sumr-suml);
}
}
return 0;
}