#include<iostream> #include<queue> #include<cstdio> #include<math.h> #include<cstring> #include<algorithm> using namespace std; int n,Q; long long ans; long long sum[4000009]; int dx[4000009]; int a[2000009]; void build(int l,int r,int id) { if(l==r) {sum[id]=a[l];return;} int mid=(l+r)/2; build(l,mid,id*2);build(mid+1,r,id*2+1); sum[id]=sum[id*2]+sum[id*2+1]; return; } void add(int l,int r,int id,int tl,int tr,int x) { if(tl<=l&&r<=tr) { dx[id]+=x; sum[id]+=(r-l+1)*x; return; } if(tl>r||tr<l) return; if(max(l,tl)<=(min(r,tr))) sum[id]+=((min(r,tr))-max(l,tl)+1)*x; int mid=(l+r)/2; add(l,mid,id*2,tl,tr,x); add(mid+1,r,id*2+1,tl,tr,x); return ; } void set(int l,int r,int id) { int mid=(l+r)/2; dx[id*2]+=dx[id];sum[id*2]+=(mid-l+1)*dx[id]; dx[id*2+1]+=dx[id];sum[id*2+1]+=(r-mid)*dx[id]; dx[id]=0; } long long ask(int l,int r,int id,int tl,int tr) { if(tl<=l&&r<=tr) return sum[id]; if(tr<l||tl>r) return 0; if(dx[id]) set(l,r,id); int mid=(l+r)/2; long long ans=0; if(tl <= mid) ans+=ask(l,mid,id*2,tl,tr); if(tr >= mid+1) ans+=ask(mid+1,r,id*2+1,tl,tr); return ans; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,n,1); scanf("%d",&Q); for(int i=1,A,l,r,x;i<=Q;i++) { scanf("%d",&A); if(A==1) { scanf("%d%d%d",&l,&r,&x); add(1,n,1,l,r,x); }else { scanf("%d%d",&l,&r); ans=0; cout<<ask(1,n,1,l,r)<<endl; } } return 0; }
时间: 2024-09-27 21:42:43