萌新的线段树找不出错QWQ
  • 板块P2357 守墓人
  • 楼主黄舀啊
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/4/25 18:27
  • 上次更新2023/10/28 02:55:15
查看原帖
萌新的线段树找不出错QWQ
496009
黄舀啊楼主2022/4/25 18:27
#include<iostream>
#define ll long long
using namespace std;

const int MAXN = 2*1e5+10;
ll n,f,va[MAXN];
struct node{
	ll l,r;
	ll data,add; 
}tree[MAXN<<2];
inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void build(ll p,ll l,ll r){
	if(l == r){
		tree[p].l = l;
		tree[p].r = r;
		tree[p].data = va[l];
		return ;
	}
	int mid = (l+r)/2;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	tree[p].l = l;
	tree[p].r = r;
	tree[p].data = tree[p<<1].data+tree[p<<1|1].data;
	return ;
}
inline void spread(int p) {
    tree[p<<1].data += tree[p].add*(tree[p<<1].r-tree[p<<1].l+1);
    tree[p<<1|1].data += tree[p].add*(tree[p<<1|1].r-tree[p<<1|1].l+1);
    tree[p<<1].add += tree[p].add;
    tree[p<<1|1].add += tree[p].add;
    tree[p].add = 0;
}
inline void change(ll p,ll x,ll y,ll w){
	if(tree[p].l>=x && tree[p].r<=y){
		tree[p].data += w*(tree[p].r-tree[p].l+1);
		tree[p].add += w;
		return ;
	}
	spread(p);
	int mid = (tree[p].l+tree[p].r)>>1;
	if(x<=mid) change(p<<1,x,y,w);
	if(y>mid)  change(p<<1|1,x,y,w);
	tree[p].data = tree[p<<1].data+tree[p<<1|1].data;
	return ;
}
inline ll ask(int p,int l,int r){
	if(tree[p].l>=l && tree[p].r<=r){
		return tree[p].data;
	}
	spread(p);
	int mid = (tree[p].l+tree[p].r)>>1;
	int ans = 0;
	if(l <= mid) ans += ask(p<<1,l,r);
	if(r > mid)  ans += ask(p<<1|1,l,r);
	return ans;
}

int main(){
	
	n = read();
	f = read();
	for(int i=1 ; i<=n ; i++)
		va[i] = read();
	build(1,1,n);
	for(ll i=1,t,l,r,k ; i<=f ; i++){
		cin >> t;
		if(t == 1){
			cin >> l >> r >> k;
			change(1,l,r,k);
		}
		if(t == 2){
			cin >> k;
			change(1,1,1,k);
		}
		if(t == 3){
			cin >> k;
			change(1,1,1,k*(-1));
		}
		if(t == 4){
			cin >> l >> r;
			cout << ask(1,l,r) << endl;
		}
		if(t == 5)
			cout << ask(1,1,1) << endl;
	}
	
	return 0;
}
2022/4/25 18:27
加载中...