半天过不了例题,救救孩子吧
查看原帖
半天过不了例题,救救孩子吧
50497
weird_coder楼主2020/10/1 19:26
#include<iostream>
#include<cstdio>
#include<cmath>

#define lc (o*2)
#define rc (o*2+1)
#define mid ((r+l)/2)
using namespace std;
typedef long long ll;
const ll N = 1e6 + 5;
ll sumv[N] = {}, addv[N] = {}, a[N] = {};
ll x, y, k;
ll m, n;
ll t[N];
ll L[N], R[N];
void build(ll o, ll l, ll r);
ll query(ll o, ll l, ll r);
void push_up(ll o);
void push_down(ll o, ll l, ll r);
void update(ll o, ll l, ll r);
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}

	build(1, 1, n);
	while (m--)
	{
		ll op;
		cin >> op;
		if (op == 1)
		{
			cin >> x >> y >> k;
			update(1, 1, n);
		}
		else
		{
			cin >> x >> y;
			cout << query(1, 1, n) << endl;
		}
	}
	return 0;
}

void build(ll o, ll l, ll r)
{
	if (l > r) return;
	if (l == r)
	{
		sumv[o] = a[l];
		return;
	}
	L[o] = l;
	R[o] = r;
	build(lc, l, mid);
	build(rc, mid + 1, r);
	push_up(o);
	return;
}
ll query(ll o, ll l, ll r)
{
	if (l > y || r < x)  return 0;
	if (l >= x && r <= y) return sumv[o];
	push_down(o,l,r);
	return query(lc, l, mid) + query(rc, mid + 1, r);
}

void push_up(ll o)
{
	sumv[o] = sumv[lc] + sumv[rc];
}

void push_down(ll o, ll l, ll r)
{
	addv[lc] += addv[o];
	addv[rc] += addv[o];
	sumv[lc] += addv[o] * (R[lc] - L[lc] + 1);
	sumv[rc] += addv[o] * (R[rc] - L[rc] + 1);
	addv[o] = 0;
}

void update(ll o, ll l, ll r)
{
	if (l > y || r < x) return;
	if (l >= x && r <= y)
	{
		addv[o] += k;
		push_up(o);
		return;
	}
	push_down(o,l,r);
	update(lc, l, mid);
	update(rc, mid + 1, r);
	push_up(o);
}

2020/10/1 19:26
加载中...