RE 求调!
查看原帖
RE 求调!
1128039
ybc2026lhy楼主2024/7/6 16:54
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
struct S{
	int l,r;
	long long sum,add;
}tree[MAXN*4];
int n,m,a[MAXN];
void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r;
	if(l==r){
		tree[p].sum=a[l];
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
}
void sp(int p){
	if(tree[p].add){
		tree[p*2].sum+=tree[p].add*(tree[p*2].r-tree[p*2].l+1);
		tree[p*2+1].sum+=tree[p].add*(tree[p*2+1].r-tree[p*2+1].l+1);
		tree[p*2].add+=tree[p].add;
		tree[p*2+1].add+=tree[p].add;
		tree[p].add=0;
	}
}
void ch(int p,int l,int r,int d){
	if(l<=tree[p].l&&r>=tree[p].r){
		tree[p].sum+=(long long)d*(tree[p].r-tree[p].l+1);
		tree[p].add+=d;
		return;
	}
	sp(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid){ch(p*2,l,r,d);}
	if(r>mid){ch(p*2+1,l,r,d);}
	tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;	
}
long long ask(int p,int l,int r){
	if(l<=tree[p].l&&r>tree[p].r){
		return tree[p].sum;
	}
	sp(p);
	int mid=(tree[p].l+tree[p].r)/2;
	long long val=0;
	if(l<=mid){val+=ask(p*2,l,r);}
	if(r>mid){val+=ask(p*2+1,l,r);}
	return val;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	while(m--){
		int op,l,r,d;
		cin>>op>>l>>r;
		if(op==1){
			cin>>d;
			ch(1,l,r,d);
		} else {
			cout<<ask(1,l,r)<<"\n";
		}
	}
	return 0;
}
2024/7/6 16:54
加载中...