跳到正文

个人记录

1

Pizar约 2 分钟
原文

这是 2026年10月2日 存档的第 5 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月1日
收录
2026年10月1日
更新
2026年11月10日
篇幅
约 2 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls ((p)<<1)
#define rs ((p)<<1|1)
#define mid ((x+y)>>1)
const int N=1e5+5;
ll a[N<<2],tag[N<<2];
int siz[N<<2];
void push(int p,ll v){
	tag[p]+=v;
	a[p]+=v*siz[p];
}
void pushdown(int p){
	if(tag[p]){
		push(ls,tag[p]);
		push(rs,tag[p]);
		tag[p]=0;
	}
}
void mdf(int x,int y,int l,int r,ll v,int p){
	if(l>r)return;
	if(l<=x&&r>=y){
		push(p,v);
		return;
	}
	pushdown(p);
	if(l<=mid)mdf(x,mid,l,r,v,ls);
	if(r>mid)mdf(mid+1,y,l,r,v,rs);
	a[p]=a[ls]+a[rs];
}
ll qry(int x,int y,int l,int r,int p){
	if(l>r)return 0;
	if(l<=x&&r>=y)
		return a[p];
	pushdown(p);
	ll ans=0;
	if(l<=mid)ans+=qry(x,mid,l,r,ls);
	if(r>mid)ans+=qry(mid+1,y,l,r,rs);
	return ans;
}
ll val[N];
void build(int x,int y,int p){
	tag[p]=0;
	siz[p]=y-x+1;
	if(x==y){
		a[p]=val[x];
		return;
	}
	build(x,mid,ls);
	build(mid+1,y,rs);
	a[p]=a[ls]+a[rs];
}
int n,m;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)
		scanf("%lld",&val[i]);
	build(1,n,1);
	while(m--){
		int op,l,r,x;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1){
			scanf("%d",&x);
			mdf(1,n,l,r,x,1);
		}
		else printf("%lld\n",qry(1,n,l,r,1));
	}
}