树状数组求调QwQ,20分爆了
查看原帖
树状数组求调QwQ,20分爆了
532686
Zongmingjun楼主2023/2/7 09:23
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int n, m;
int a[N], c1[N], c2[N], d[N];
int lowbit(int i)
{
	return i & (-i);
}
void update(int c[], int i, int k)
{
	while(i <= n)
	{
		c[i] += k;
		i += lowbit(i);
	}
}
int getsum(int c[], int i)
{
	int res = 0;
	while(i > 0)
	{
		res += c[i];
		i -= lowbit(i);
	}
	return res;
}
int getans(int x)
{
	return (x + 1) * getsum(c1, x) - getsum(c2, x);
}
signed main()
{
	scanf("%lld %lld", &n, &m);
	for(int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		d[i] = a[i] - a[i - 1];
		update(c1, i, d[i]);
		update(c2, i, d[i] * i);
	}
	for(int i = 1; i <= m; i++)
	{
		int flag;
		scanf("%lld", &flag);
		if(flag == 1)
		{
			int x, y, z;
			scanf("%lld %lld %lld", &x, &y, &z);
			update(c1, x, z);
			update(c1, y + 1, -z);
			update(c2, x, x * z);
			update(c2, y + 1, -(y + 1) * z);
		}
		if(flag == 2)
		{
			int z;
			scanf("%d", &z);
			update(c1, 1, z);
			update(c1, 2, -z);
			update(c2, 1, z);
			update(c2, 2, -z);
		}
		if(flag == 3)
		{
			int z;
			scanf("%d", &z);
			update(c1, 1, -z);
			update(c1, 2, z);
			update(c2, 1, -z);
			update(c2, 2, z);
		}
		if(flag == 4)
		{
			int x, y;
			scanf("%lld %lld", &x, &y);
			printf("%lld\n", getans(y) - getans(x - 1));
		}
		if(flag == 5)
		{
			printf("%d\n", getans(1));
		}
	}
	return 0;
}
2023/2/7 09:23
加载中...