RE 70分求助
查看原帖
RE 70分求助
81832
Starry___sky楼主2020/8/26 20:58

这里是线段树开小了还是?

我寻思也没啥了啊。

#include<bits/stdc++.h>

#define ll long long
#define ls(k) k << 1
#define rs(k) k << 1 | 1

using namespace std;

const int N = 2e5 + 100;

ll tree[N >> 2], tag[N >> 2];

ll a[N];
int n, f;

void build(int k, int l, int r)
{
	tag[k] = 0;
	if(l == r)
	{
		tree[k] = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls(k), l, mid);
	build(rs(k), mid + 1, r);
	tree[k] = tree[ls(k)] + tree[rs(k)];
}

void add(int k, int l, int r, ll v)
{
	tag[k] += v;
	tree[k] += (r - l + 1) * v;
}

void push_down(int k, int l, int r, int mid)
{
	add(ls(k), l, mid, tag[k]);
	add(rs(k), mid + 1, r, tag[k]);
	tag[k] = 0;
}

void update(int k, int l, int r, int x, int y, ll v)
{
	if(x <= l && r <= y)
	{
		add(k, l, r, v);
		return;
	}
	int mid = (l + r) >> 1;
	push_down(k, l, r, mid);
	if(x <= mid)
		update(ls(k), l, mid, x, y, v);
	if(y > mid)
		update(rs(k), mid + 1, r, x, y, v);
	tree[k] = tree[ls(k)] + tree[rs(k)];
}

ll query(int k, int l, int r, int x, int y)
{
	if(x <= l && r <= y)
		return tree[k];
	int mid = (l + r) >> 1;
	push_down(k, l, r, mid);
	ll ans = 0;
	if(x <= mid)
		ans += query(ls(k), l, mid, x, y);
	if(y > mid)
		ans += query(rs(k), mid + 1, r, x, y);
	return ans;
}

int main()
{
	cin >> n >> f;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	build(1, 1, n);
	for(int i = 1; i <= f; i++)
	{
		int op;
		cin >> op;
		if(op == 1)
		{
			int l, r;
			ll k;
			cin >> l >> r >> k;
			update(1, 1, n, l, r, k);
		}
		else if(op == 2)
		{
			ll k;
			cin >> k;
			update(1, 1, n, 1, 1, k);
		}
		else if(op == 3)
		{
			ll k;
			cin >> k;
			update(1, 1, n, 1, 1, -k);
		}
		else if(op == 4)
		{
			int l, r;
			cin >> l >> r;
			ll ans = query(1, 1, n, l, r);
			cout << ans << endl;
		}
		else if(op == 5)
		{
			cout << query(1, 1, n, 1, 1) << endl;
		}
	}
	return 0;
}
2020/8/26 20:58
加载中...