树状数组,全 wa是怎么回事?
查看原帖
树状数组,全 wa是怎么回事?
481621
Zhang_Wenjie楼主2024/3/3 13:47

推的式子:SSp=(n+1)∑i=1pai−∑i=1pai×iSS_p=(n+1)\sum\limits_{i=1}^{p}a_i - \sum\limits_{i=1}^{p}a_i\times i

#include <bits/stdc++.h>
#define re long long 
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
LL n, m, a[N], c0[N], c1[N];

inline LL lowbit(LL x) { return x & -x; }

inline void add(LL *c, LL x, LL k)
{
	while (x <= n) 
	{
		c[x] += k;
		x += lowbit(x);
	}
}

inline LL query(LL *c, LL x)
{
	LL res = 0;
	while (x > 0)
	{
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> m;
	for (re i = 1; i <= n; i ++)
	{
		cin >> a[i];
		add(c0, i, a[i]);
		add(c1, i, a[i] * i);
	}
	while (m --)
	{
		string s;
		LL x, k;
		cin >> s;
		if (s[0] == 'Q')
		{
			cin >> x;
			cout << (n + 1) * query(c0, x) - query(c1, x) << "\n";
		}
		else
		{
			cin >> x >> k;
			add(c0, x, k - a[x]);
			add(c1, x, (k - a[x]) * x); 
			a[x] = k;
		}
	}
	return 0;
}
2024/3/3 13:47
加载中...