爆马蜂
别想着直接复制粘贴啊 其实复制了也没事,这个代码我是照书写的但是马蜂是我的,后来为了嚼知识就没怎么用书了。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lowbit(x) ((x)&-(x))
const int N=100010;
ll tree1[N],tree2[N];
void update1(ll x,ll d){while(x<=N){tree1[x]+=d; x+=lowbit(x);}}
void update2(ll x,ll d){while(x<=N){tree2[x]+=d; x+=lowbit(x);}}
ll sum1(ll x){ll ans=0; while(x>0){ans+=tree1[x]; x-=lowbit(x);} return ans;}
ll sum2(ll x){ll ans=0; while(x>0){ans+=tree2[x]; x-=lowbit(x);} return ans;}
int main(){
ll n,m; cin>>n>>m; ll old=0,a;
for(int i=1;i<=n;i++){ cin>>a; update1(i,a-old); update2(i,(i-1)*(a-old)); old=a; }
while(m--){
ll q,L,R,d; cin>>q;
if(q==1){ cin>>L>>R>>d; update1(L,d);update1(R+1,-d);update2(L,d*(L-1));update2(R+1,-d*R);}
else{cin>>L>>R; cout<<R*sum1(R)-sum2(R)-(L-1)*sum1(L-1)+sum2(L-1)<<endl;}
}
return 0;
}