求助玄关
查看原帖
求助玄关
1439731
yzbzz147268楼主2025/1/24 15:53
#include<bits/stdc++.h>
using namespace std;
int a[100050];
int n,m;
struct Tree{
	int l,r;
	long long v,lazy;
}tree[500050];
void build(int l,int r,int p){
	tree[p].l=l;
	tree[p].r=r;
	if(l==r){
		tree[p].v=a[l];
		return ;
	}
	build(l,(r+l)/2,p*2);
	build((r+l)/2+1,r,p*2+1);
	tree[p].v=tree[p*2].v+tree[p*2+1].v;
	return ;
}
void down(int p){
	tree[p*2].lazy+=tree[p].lazy;
	tree[p*2+1].lazy+=tree[p].lazy;
	tree[p*2].v+=(tree[p*2].r-tree[p*2].l+1)*tree[p].lazy;
	tree[p*2+1].v+=(tree[p*2+1].r-tree[p*2+1].l+1)*tree[p].lazy;
	tree[p].lazy=0;
	return ;
}
void add(int p,int l,int r,int x){
	if(tree[p].l>r||tree[p].r<l) return ;
	if(tree[p].l>=l&&tree[p].r<=r){
		tree[p].lazy+=x;
		tree[p].v+=(tree[p].r-tree[p].l+1)*x;
		return ;
	}
	if(tree[p].lazy>0) down(p);
	add(p*2,l,r,x);
	add(p*2+1,l,r,x);
	tree[p].v=tree[p*2+1].v+tree[p*2].v;
	return ; 
}
long long query(int p,int l,int r){
	if(tree[p].l>r||tree[p].r<l) return 0;
	if(tree[p].l>=l&&tree[p].r<=r) return tree[p].v;
	if(tree[p].lazy>0) down(p);
	return query(p*2,l,r)+query(p*2+1,l,r);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int q;
		scanf("%d",&q);
		if(q==1){
			int b,c,d;
			scanf("%d%d%d",&b,&c,&d);
			add(1,b,c,d);
		}
		if(q==2){
			int b,c;
			scanf("%d%d",&b,&c);
			printf("%lld\n",query(1,b,c));
		}
	}
	return 0;
}

求助,检查过几遍了都没发现问题

2025/1/24 15:53
加载中...