题目地址:http://codevs.cn/problem/1082/

参考博客:https://blog.csdn.net/zars19

代码:


#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <fstream>
#define  maxn 500005
#define lowbit(x) ((x)&(-x))
typedef long long ll;
using namespace std;
ll n,q,now,op,l,r,v,last=0,c[maxn][2]={0};
void update(ll x,ll v,ll w)
{
    for(ll i=x;i<=n;i+=lowbit(i))
        c[i][w]+=v;
}
ll getsum(ll x,ll w)
{
    ll sum=0;
    for(ll i=x;i>0;i-=lowbit(i))
        sum+=c[i][w];
    return sum;
}
ll getans(ll x)
{
    return (x+1)*getsum(x,0)-getsum(x,1);
}
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&now);
        update(i,now-last,0);//di
        update(i,(now-last)*i,1);//di*i
        last=now;
    }
    scanf("%lld",&q);
    while(q--)
    {
        scanf("%lld",&op);
        if(op==1)
        {
            scanf("%lld %lld %lld",&l,&r,&v);
            update(l,v,0);update(r+1,-v,0);
            update(l,v*l,1);update(r+1,-v*(r+1),1);
        }
        else
        {
            scanf("%lld %lld",&l,&r);
            printf("%lld\n",getans(r)-getans(l-1));
        }
    }
    return 0;
}