线段树板子求助,悬赏关注
查看原帖
线段树板子求助,悬赏关注
547658
Shiota_Nagisa楼主2023/5/16 21:52
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100011];
struct tree{
	int l,r,cnt,lazy;
}t[100011];
void build(int i,int f,int s){
	t[i].l=f;
	t[i].r=s;
	if(f==s){
		t[i].cnt=a[i];
		return ;
	}
	int mid=(f+s)>>1;
	build(i*2,f,mid);
	build(i*2+1,mid+1,s);
	t[i].cnt=t[i*2].cnt+t[i*2+1].cnt;
}
void lan(int i){
	t[i*2].lazy=t[i].lazy;
	t[i*2].cnt+=(t[i*2].r-t[i*2].l+1)*t[i].lazy;
	t[i*2+1].lazy=t[i].lazy;
	t[i*2+1].cnt=(t[i*2+1].r-t[i*2+1].l+1)*t[i].lazy;
	t[i].lazy=0;
}
void add(int i,int f,int s,int v){
	if(t[i].l>s||t[i].r<f) return ;
	if(t[i].l>=f&&t[i].r<=s){
		t[i].cnt+=v*(t[i].r-t[i].l+1);
		t[i].lazy+=v;
		return ;
	}
	if(t[i].lazy>0){
		lan(i);
	}
	add(i*2,f,s,v);
	add(i*2+1,f,s,v);
	t[i].cnt=t[i*2].cnt+t[i*2+1].cnt;
}
int getsum(int i,int f,int s){
	if(t[i].l>s||t[i].r<f){
		return 0;
	}
	if(t[i].l>=f&&t[i].r<=s){
		return t[i].cnt;
	}
	if(t[i].lazy) lan(i);
	return getsum(i*2,f,s)+getsum(i*2+1,f,s);
}
int	main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int ask,x,y;
		cin>>ask>>x>>y;
		if(ask==2){
			cout<<getsum(1,x,y)<<endl;
		}
		else if(ask==1){
			int val;
			cin>>val;
			add(1,x,y,val);
		}
	}
	return 0;
}
2023/5/16 21:52
加载中...