跳到正文

个人记录

xxx

cuiluwen约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月29日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define lc p<<1
#define rc p<<1|1
#define N 500050
#define int long long
int n,m,a[N];
struct node{
	int l,r,sum,add;
}tr[N*4];
void build(int p,int l,int r){
	tr[p]={l,r,a[l]};
	if(l==r)return;
	int m=l+r>>1;
	build(lc,l,m);
	build(rc,m+1,r);
	tr[p].sum=tr[lc].sum+tr[rc].sum;
}
void pushdown(int p){
	if(tr[p].add){
		tr[lc].sum+=tr[p].add*(tr[lc].r-tr[lc].l+1);
		tr[rc].sum+=tr[p].add*(tr[rc].r-tr[rc].l+1);
		tr[lc].add+=tr[p].add;
		tr[rc].add+=tr[p].add;
		tr[p].add=0;
	}
}
void update(int p,int x,int y,int k){
	if(x<=tr[p].l&&tr[p].r<=y){
		tr[p].sum+=(tr[p].r-tr[p].l+1)*k;
		tr[p].add+=k;
		return ;
	}
	int m=tr[p].l+tr[p].r>>1;
	pushdown(p);
	if(x<=m)update(lc,x,y,k);
	if(y>m)update(rc,x,y,k);
	tr[p].sum=tr[lc].sum+tr[rc].sum;
}
int query(int p,int x,int y){
	if(x<=tr[p].l&&tr[p].r<=y)return tr[p].sum;
	int m=tr[p].l+tr[p].r>>1;
	pushdown(p);
	int sum=0;
	if(x<=m)sum+=query(lc,x,y);
	if(y>m)sum+=query(rc,x,y);
	return sum;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++){
		int q;
		cin>>q;
		if(q==1){
			int x,y,k;
			cin>>x>>y>>k;
			update(1,x,y,k);
		}
		else {
			int x,y;
			cin>>x>>y;
			cout<<query(1,x,y);
		}
	}
	return 0;
}
原文