线段树模板求调
查看原帖
线段树模板求调
427623
XiaoQuQu楼主2021/8/19 13:41
#include <algorithm>
#include <iostream>
#include <cstring>
#include <sstream>
#include <bitset>
#include <numeric>
#include <cstdio>
#include <vector>
#include <string>
#include <limits>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define RE_OK 0
#define RE_DEBUG 2
#define RE_ERROR 5
#define RE_ANTICOPY 15
#define lson (p << 1)
#define rson (p << 1 | 1)
#define ll long long
#define ull unsigned long long
#define ini inline int
#define intInf 0x7f7f7f7f
#define llInf numeric_limits<ll>::max();
#define ullInf numeric_limits<ull>::max();
#define mid ((l + r) >> 1)
#define inv inline void
#define inl inline
#define reg register
#define tio()                    \
	ios::sync_with_stdio(false); \
	cin.tie(NULL);               \
	cout.tie(NULL)
#define mst(a) memset((a), 0, sizeof(a))
#define fr(t, begin, end) for (register int(t) = (begin); (t) <= (end); ++(t))
#define rp(t, begin, end, change) for (register int(t) = (begin); (t) <= (end); (t) = (t) + (change))
#define fp(t, begin, end) for (register int(t) = (begin); (t) < (end); ++(t))
#define srt(begin, end) std::sort(begin, end)
#define unq(begin, end) std::unique(begin, end)

const int MAXN=100005;

int tree[MAXN<<2],add[MAXN<<2],a[MAXN];
int n,m;

int Push_up(int p){
	tree[p]=tree[lson]+tree[rson];
	return RE_OK;
}

inline void f(ll p,ll l,ll r,ll k)
{
    add[p]=add[p]+k;
    tree[p]=tree[p]+k*(r-l+1);
}
inline void push_down(ll p,ll l,ll r)
{
    f(lson,l,mid,add[p]);
    f(rson,mid+1,r,add[p]);
    add[p]=0;
}

int build(int l,int r,int p){
	if(l==r){
		tree[p]=a[l];
		return RE_OK;
	}
	build(l,mid,lson);
	build(mid+1,r,rson);
	if(Push_up(p)){
		cerr<<"\"PushUp\" function not return 0"<<endl;
		exit(RE_ERROR);
	}
	return RE_OK;
}

int update(int L,int val,int l,int r,int p){
	if(l==r){
		tree[p]+=val;
		return RE_OK;
	}
	if(L<mid){
		update(L,val,l,mid,lson);
	}
	else{
		update(L,val,mid+1,r,rson);
	}
	if(Push_up(p)){
		cerr<<"\"PushUp\" function not return 0"<<endl;
		exit(RE_ERROR);
	}
	return RE_OK;
}

int update_range(int L,int R,int val,int l,int r,int p){
	if(L<=l&&r<=R){
		tree[p]+=val*(r-l+1);
		add[p]+=val;
		return RE_OK;
	}
	push_down(p,l,r);
	if(L<mid){
		update_range(L,R,val,l,mid,lson);
	}
	else{
		update_range(L,R,val,mid+1,r,rson);
	}
	if(Push_up(p)){
		cerr<<"\"PushUp\" function not return 0"<<endl;
		exit(RE_ERROR);
	}
	return RE_OK;
}

int query_range(int L,int R,int l,int r,int p){
	if(L<=l&&r<=R){
		return tree[p];
	}
	push_down(p,l,r);
	int ret=0;
	if(L<=mid){
		ret+=query_range(L,R,l,mid,lson);
	}
	if(R>mid){
		ret+=query_range(L,R,mid+1,r,rson);
	}
	return ret;
}

int main(void){
	//scan data
	tio();
	cin>>n>>m;
	fr(i,1,n){
		cin>>a[i];
	}
	//build Segment tree
	build(1,n,1);
	while(m--){
		int mode,x,y,z;
		cin>>mode>>x>>y;
		if(mode==1){
			cin>>z;
			update_range(x,y,z,1,n,1);
		}
		else{
			cout<<query_range(x+1,y,1,n,1)<<endl;
		}
	}
	return 0;
}

在线等,挺急的。

2021/8/19 13:41
加载中...