0分没过样例
查看原帖
0分没过样例
777844
lgpi3141592楼主2023/12/18 13:53
#include <bits/stdc++.h>
using namespace std;
int n,m,v[100005];
struct node
{
	int sum;
	int lazy;
}tree[400005];
#define lc root*2
#define rc root*2+1
void build(int root,int l,int r)//建树
{
	if(l==r)
	{
		tree[root].sum=v[l];
		return ;
	}
	int mid=l+(r-l)/2;
	build(lc,l,mid);
	build(rc,mid+1,r);
	tree[root].sum=tree[lc].sum+tree[rc].sum;
}
void updata(int root,int len)//下传懒标记
{
	tree[lc].lazy+=tree[root].lazy;
	tree[rc].lazy+=tree[root].lazy;
	tree[lc].sum+=tree[root].lazy*(len-len/2);
	tree[rc].sum+=tree[root].lazy*(len/2);
	tree[root].lazy=0;
}
void change1(int l,int r,int L,int R,int vaule,int root)//区间操作
{
	if(l<=L&&R<=r)
	{
		tree[root].lazy+=vaule;
		tree[root].sum+=vaule*(R-L+1);
		return ;
	}
	int mid=L+(R-L)/2;
	if(tree[root].lazy&&L!=R)
		updata(root,R-L+1);
	if(l<=mid)
		change1(l,r,L,mid,vaule,lc);
	if(r>mid)
		change1(l,r,mid+1,R,vaule,rc);
	tree[root].sum=tree[lc].sum+tree[rc].sum;
}
int get1(int l,int r,int L,int R,int root)//求值
{
	if(l<=L&&R<=r)
		return tree[root].sum;
	int mid=L+(R-L)/2;
	if(tree[root].lazy)
		updata(root,R-L+1);
	long long ans=0;
	if(l<=mid)
		ans+=get1(l,r,L,mid,lc);
	if(r>mid)
		ans+=get1(l,r,mid+1,R,rc);
	return ans;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>v[i];
	build(1,1,n);
	/*
	  for(int i=1;i<=3;i++)
	  cout<<tree[i].sum<<" ";
	  //debug
	 */
	while(m--)
	{
		int op1;
		cin>>op1;
		if(op1==1)
		{
			int op2,op3;
			cin>>op1>>op2>>op3;
			change1(op1,op2,1,n,op3,1);
		}
		if(op1==2)
		{
			int op2;
			cin>>op1>>op2;
			cout<<get1(op1,op2,1,n,1)<<"\n";
		}
	}
	return 0;
}
2023/12/18 13:53
加载中...