求调
查看原帖
求调
1041857
INTP_A楼主2024/11/21 20:13
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,a[200010];
struct node{
	int sum,add;
}c[800010];
void pushup(int k){
	c[k].sum=c[k<<1|1].sum+c[k<<1].sum;
	return;
}
void build(int k,int l,int r){
	if(l==r){
		c[k].sum=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	pushup(k);
	return;
}
void pushdown(int k,int l,int r){
	int mid=(l+r)>>1;
	c[k<<1].add+=c[k].add;
	c[k<<1|1].add+=c[k].add;
	c[k<<1].sum+=(mid-l+1)*c[k].add;
	c[k<<1|1].sum+=(r-mid)*c[k].add;
	return;
}
void ope(int k,int x,int y,int l,int r,int ll){
	if(l>y||r<x)return;
	if(l>=x&&r<=y){
		c[k].add+=ll;
		c[k].sum+=ll*(r-l+1);
		return;
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r);
	ope(k<<1,x,y,l,mid,ll);
	ope(k<<1|1,x,y,mid+1,r,ll);
	pushup(k);
	return;
}
int fanhui(int k,int l,int r,int x,int y){
	int ans=0;
	if(l>y||r<x)return 0;
	if(l>=x&&r<=y){
		return c[k].sum;
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r);
	ans+=(fanhui(k<<1,l,mid,x,y)+fanhui(k<<1|1,mid+1,r,x,y));
	return ans;
}
signed 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 g;
		cin>>g;
		if(g==1){
			int x,y,k;
			cin>>x>>y>>k;
			ope(1,x,y,1,n,k);
		}else {
			int x,y;
			cin>>x>>y;
			cout<<fanhui(1,1,n,x,y)<<endl;
		}
	}
}

样例能过,但洛谷只有十分求调

2024/11/21 20:13
加载中...