qz
查看原帖
qz
577241
违规用户名577241楼主2024/4/6 18:27
#include <iostream>
#include <climits>
#include <cstdio>
using namespace std;
int n, q;
const int maxn = 1E6 + 5;
int a[maxn];
const long long Inf = LLONG_MAX;
struct segment_tree
{
	long long value;
	long long tag1, tag2;
};
segment_tree tree[maxn * 4];
void pushup(int root)
{
	tree[root].value = max(tree[root << 1].value, tree[(root << 1) + 1].value);
}
void build(int root, int l, int r)
{
	tree[root].tag1 = Inf;
	if (l == r)
	{
		tree[root].value = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(root << 1, l, mid);
	build((root << 1) + 1, mid + 1, r);
	pushup(root);
}
void pushdown(int root)
{
	if (tree[root].tag1 == Inf) 
	{
		tree[root << 1].tag2 += tree[root].tag2;
		tree[root << 1].value += tree[root].tag2;
		tree[(root << 1) + 1].tag2 += tree[root].tag2;
		tree[(root << 1) + 1].value += tree[root].tag2;
		tree[root].tag2 = 0;
		return;
	}
	else
	{
		tree[root << 1].tag2 = tree[(root << 1) + 1].tag2 = 0;
		tree[root << 1].tag1 = tree[(root << 1) + 1].tag1 = tree[root].tag1;
		tree[root << 1].value = tree[(root << 1) + 1].value = tree[root].tag1;
		tree[root].tag1 = Inf;
		tree[root].tag2 = 0;
		return;
	}
}
void update(int root, int l, int r, int L, int R, int x, int opt)
{
	if (L <= l && r <= R)
	{
		if (opt == 1)
		{
			tree[root].tag1 = x;
			tree[root].tag2 = 0;
			tree[root].value = x;
		}
		else
		{
			if (tree[root].tag1 == Inf)
			{
				tree[root].tag2 += x;
				tree[root].value += x;
			}
			else
			{
				tree[root].tag1 += x;
				tree[root].value += x;
				tree[root].tag2 = 0;
			}
		}
		return;
	}
	int mid = (l + r) >> 1;
	pushdown(root);
	if (L <= mid) update(root << 1, l, mid, L, R, x, opt);
	if (R > mid) update((root << 1) + 1, mid + 1, r, L, R, x, opt);
	pushup(root);
}
long long query(int root, int l, int r, int L, int R)
{
	if (L <= l && r <= R)
		return tree[root].value;
	int mid = (l + r) >> 1;
	pushdown(root);
	long long res = 0;
	if (L <= mid) res += query(root << 1, l, mid, L, R);
	if (R > mid) res += query((root << 1) + 1, mid + 1, r, L, R);
	return res;
}
int main()
{
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	while (q--)
	{
		int opt;
		scanf("%d", &opt);
		if(opt != 3)
		{
			int l, r;
			int x;
			scanf("%d%d%d", &l, &r, &x);
			update(1, 1, n, l, r, x, opt) ;
		}
		else
		{
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%lld\n", query(1, 1, n, l, r));
		}
	}
	return 0;
}
2024/4/6 18:27
加载中...