RE求调
查看原帖
RE求调
1379071
LOVE_WHY楼主2025/8/5 10:44
#include <bits/stdc++.h>
#define ll unsigned long long
#define int ll
using namespace std;
const int N=1e5+10;
int n,m,a[N];
struct ST{
	struct point{
		int l,r;
		int maxn;//懒得改名了 
		int add; 
	}tr[N<<2];
	void push_up(int u){
		tr[u].maxn=tr[2*u].maxn+tr[2*u+1].maxn;
	}
	void push_down(int u){
		tr[2*u].maxn+=tr[u].add*(tr[2*u].r-tr[2*u].l+1);
		tr[2*u+1].maxn+=tr[u].add*(tr[2*u+1].r-tr[2*u+1].l+1);
		tr[2*u].add+=tr[u].add;
		tr[2*u+1].add+=tr[u].add;
		tr[u].add=0;
	}
	void build(int u,int l,int r){
		if (l==r){
			tr[u]={l,r,a[l]};
		}
		else{
			tr[u]={l,r};
			int mid=(l+r)>>1;
			build(2*u,l,mid);
			build(2*u+1,mid+1,r);
			push_up(u);
		}
	}
	void update(int u,int l,int r,int c){
		if (tr[u].l>=l && tr[u].r<=r){
			tr[u].maxn+=c*(tr[u].r-tr[u].l+1);
			tr[u].add+=c;
			return ;
		}
		push_down(u);
		int mid=(l+r)/2;
		if (l<=mid) update(2*u,l,r,c);
		if (r>mid) update(2*u+1,l,r,c);
		push_up(u);
	}
	int query(int u,int l,int r){
		if (tr[u].l>=l && tr[u].r<=r) return tr[u].maxn;
		int mid=(tr[u].l+tr[u].r)>>1;
		int tot=0;
		if (l<=mid) tot=tot+query(2*u,l,r);
		if (r>mid) tot=tot+query(2*u+1,l,r);
		return tot;
	}
};  
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for (int i=1;i<=n;++i) cin>>a[i];
	ST tre;tre.build(1,1,n);
	while (m--){
		int tpe,x,y,z;cin>>tpe;
		if (tpe==1){
			cin>>x>>y;
			cout<<tre.query(1,x,y)<<"\n";
		}
		else{
			cin>>x>>y>>z;
			tre.update(1,x,y,z);
		}
	}
	return 0;
} 
2025/8/5 10:44
加载中...