求助,内存访问出问题+全是0
查看原帖
求助,内存访问出问题+全是0
956382
Ravener楼主2024/9/27 22:18
#include<bits/stdc++.h>
#define int long long
#define il inline
#define rt return
#define opr operator

using namespace std;

il int read()
{
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c))
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(isdigit(c))
	{
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
	rt x*f;
}
il void write(int x)
{
	if(x<0)
	{
		putchar('-');
		write(-x);
		rt;
	}
	if(x/10) write(x/10);
	putchar(x%10+48);
}

template<typename T,
		 typename Container >
class Segment_Tree
{
protected:
	vector<T> _tree;
	vector<T> _lazy;
	//vector<bool> _is_lazy;
	il int Left(const int x){rt x<<1;}
	il int Right(const int x){rt x<<1|1;}
public:
	Segment_Tree(const Container _con,int _l,int _r,int _root)
	{
		_tree.resize((_r<<2)+5,0);
		_lazy.resize((_r<<2)+5,0);
		//_is_lazy.resize(_r<<2+5,0);
		if(_l==_r)
		{
			_tree[_root]=_con[_l];
			rt;
		}
		int _mid=_l+((_r-_l)>>1);
		Segment_Tree(_con,_l,_mid,Left(_root));
		Segment_Tree(_con,_mid+1,_r,Right(_root));
		_tree[_root]=_tree[Left(_root)]+_tree[Right(_root)];
	}
	void maintain(int &_l,int &_r,int &_pos)
	{
		int _mid=_l+((_r-_l)<<1);
		if(_lazy[_pos] && _l!=_r)
		{
			_tree[Left(_pos)]+=_lazy[_pos]*(_mid-_l+1);
			_tree[Right(_pos)]+=_lazy[_pos]*(_r-_mid);
			_lazy[_pos]=0;
		}
	}
	void update(int _ul,int _ur,T _val,int _l,int _r,int _pos)
	{
		if(_ul<=_l && _ur>=_r)
		{
			_tree[_pos]+=(_r-_l+1)*_val;
			_lazy[_pos]+=_val;
			rt;
		}
		int _mid=_l+((_r-_l)<<1);
		if(_lazy[_pos] && _l!=_r)
		{
			_tree[Left(_pos)]+=_lazy[_pos]*(_mid-_l+1);
			_tree[Right(_pos)]+=_lazy[_pos]*(_r-_mid);
			_lazy[_pos]=0;
		}
		if(_l<=_mid) update(_ul,_ur,_val,_l,_mid,Left(_pos));
		if(_r>_mid) update(_ul,_ur,_val,_mid+1,_r,Right(_pos));
		_tree[_pos]=_tree[Left(_pos)]+_tree[Right(_pos)];
	}
	T sum(int _ql,int _qr,int _l,int _r,int _pos)
	{
		if(_ql<=_l && _qr>=_r)
			rt _tree[_pos];
		int _mid=_l+((_r-_l)<<1);
		T _res=0;
		maintain(_l,_r,_pos);
		if(_l<=_mid) _res+=sum(_ql,_qr,_l,_mid,Left(_pos));
		if(_r>_mid) _res+=sum(_ql,_qr,_mid+1,_r,Right(_pos));
		rt _res;
	}
};
int n,m,x,y,k,op;
vector<int> a;
signed main()
{
	n=read(),m=read();
	a.resize(n+1);
	for(int i=1;i<=n;++i)
		a[i]=read();
	Segment_Tree<int,vector<int> > ST(a,1,n,1);
	while(m--)
	{
		op=read(),x=read(),y=read();
		if(op==1)
		{
			k=read();
			ST.update(x,y,k,1,n,1);
		}
		else
		{
			write(ST.sum(x,y,1,n,1));
			puts("");
		}
	}
}
2024/9/27 22:18
加载中...