95pts WA on #14 树状数组求调
查看原帖
95pts WA on #14 树状数组求调
785657
jun666楼主2024/10/21 21:53
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll n, q, w;
ll a[N], c[N], c2[N];
int lowbit(int x) {
	return x & (-x);
}
void add1(int i, ll x) {
	while (i <= n) {
		c[i] += x;
		i += lowbit(i);
	}
}
void add2(int i, ll x) {
	while (i <= n) {
		c2[i] += x;
		i += lowbit(i);
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q >> w;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		c[i] = a[i] - a[i - 1];
		c2[i] = c[i] * i;
	}
	for (int i = 1; i <= n; i++) {
		if (i + lowbit(i) <= n) {
			c[i + lowbit(i)] += c[i];
			c2[i + lowbit(i)] += c2[i];
		}
	}
	while (q--) {
		ll l, r, d, ww = w, s = 0, mul = 1, ans = 0;
		cin >> l >> r >> d;
		add1(l, d), add1(r + 1, -d);
		add2(l, d * l), add2(r + 1, -d * (r + 1));
		for (int i = n; i >= 1; i -= lowbit(i))
			s += c[i] * (n + 1) - c2[i];
		while (ww > s) {
			ans += n;
			ww -= s;
			mul *= 2;
			s *= 2;
		}
		ll s1 = 0, s2 = 0, pos = 0, i = 1 << 18;
		while (i > n)
			i >>= 1;
		while (i) {
			if (((s1 + c[pos + i]) * (pos + i + 1) - s2 - c2[pos + i])*mul < ww) {
				pos += i;
				s1 += c[pos], s2 += c2[pos];
			}
			i >>= 1;
		}
		ans += pos;
		cout << ans << '\n';
	}
	return 0;
}
2024/10/21 21:53
加载中...