样例不过?
查看原帖
样例不过?
1499743
ma_rui楼主2025/8/30 16:42
#include<bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define endl '\n'
#define int long long
using namespace std;
const int N=1e5+5,INF=0x3f3f3f3f,MOD=1e9+7;
const int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
int n,m,x,Left,Right,value;
struct node{
	int data,lazy=0;
};
vector<int>a(N);
vector<node>tree(N*4);
void build(int step,int l,int r){
	if(l==r){
		tree[step].data = a[l];
		return;
	}
	int mid=l+(r-l)/2;
	build(2*step,l,mid);
	build(2*step+1,mid+1,r);
	tree[step].data = tree[2*step].data+tree[2*step+1].data;
}
void lazydown(int step){
	if(tree[step].lazy){
		tree[2*step].data+=tree[step].lazy,
		tree[2*step].lazy+=tree[step].lazy,
		tree[2*step+1].data+=tree[step].lazy,
		tree[2*step+1].lazy+=tree[step].lazy,
		tree[step].lazy = 0;
	}
}
int sum(int step,int l,int r){
	if(Left>r||Right<l)return 0;
	if(Left<=l&&Right>=r)return tree[step].data;
	int mid=l+(r-l)/2;
	lazydown(step);
	return sum(2*step,l,mid)+sum(2*step+1,mid+1,r);
}
void Plus(int step,int l,int r){
	if(Left>r||Right<l)return;
	if(Left<=l&&Right>=r){
		tree[step].data+=value,tree[step].lazy+=value;
		return;
	}
	int mid=l+(r-l)/2;
	lazydown(step);
	Plus(2*step,l,mid);
	Plus(2*step+1,mid+1,r);
	tree[step].data = tree[2*step].data+tree[2*step+1].data;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	build(1,1,n);
	while(m--){
		cin>>x;
		if(x==1){
			cin>>Left>>Right>>value;
			Plus(1,1,n);
		}else{
			cin>>Left>>Right;
			cout<<sum(1,1,n)<<endl;
		}
	}
	return 0;
}

2025/8/30 16:42
加载中...