2pow10 @ 2023-10-09 21:15:08
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 270000*4;
int b[N];
int d[N],a[N];
int n,m;
//建图
void build(int s,int t,int p){
if(s==t){
d[p] = a[s];
return ;
}
int mid=(s+t)>>1;
build(s,mid,p*2);
build(mid+1,t,p*2+1);
d[p] = d[p*2] + d[p*2+1];
}
//区间求和
int getsum(int l,int r,int s,int t,int p){
if(l<=s&&r>=t) return d[p];
int m=(s+t)>>1;
if(b[p]){
d[p*2] += b[p]*(m-s+1) , d[p*2+1] += b[p]*(t-m);
b[p*2] += b[p],b[p*2+1] += b[p];
b[p] = 0;
}
int num=0;
if(l<=m) num += getsum(l,r,s,m,p*2);
if(r>m) num += getsum(l,r,m+1,t,p*2+1);
return num;
}
// 区间加值
void add(int l,int r,int c,int s,int t,int p){
if(l<=s&&r>=t){
d[p] += (t-s+1)*c;
b[p] += c;
return ;
}
int m=(s+t)>>1;
if(b[p]){
d[p*2] += b[p]*(m-s+1),d[p*2+1] += b[p]*(t-m);
b[p*2] = b[p] , b[p*2+1] = b[p];
b[p] = 0;
}
if(l<=m) add(l,r,c,s,m,p*2);
if(r>m) add(l,r,c,m+1,t,p*2+1);
d[p] = d[p*2] + d [p*2+1];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> a[i];
build(1,n,1);
while(m--){
int shu;
cin >> shu;
if(shu==1){
int x,y,k;
cin >> x >> y >> k;
add(x,y,k,1,n,1);
}
else {
int x,y;
cin >> x >> y;
cout << getsum(x,y,1,n,1) << "\n";
}
}
return 0;
}
by Drimpossible @ 2023-10-09 21:44:40
add函数里b[p 2]+=b[p]写成b[p 2]=b[p]了
by 2pow10 @ 2023-10-09 21:46:16
好的,谢谢La~ @King_Rider