60f分,其余TLE,求调
查看原帖
60f分,其余TLE,求调
1279073
nowornever0625楼主2024/10/22 17:27
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define lc p<<1
#define rc p<<1|1
const int maxn = 2e5 + 5;
LL n, q, w, ans, a[maxn];

struct node {
	LL l, r, sum, add;
} tr[maxn * 4];

inline LL fast_read_ll() {
	LL x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (LL)(ch - '0');
		ch = getchar();
	}
	return x * f;
}

void pushup(LL p) {
	tr[p].sum = tr[lc].sum + tr[rc].sum;
}

void pushdown(LL p) {
	if (tr[p].add) {
		tr[lc].sum += (tr[lc].r - tr[lc].l + 1) * tr[p].add;
		tr[rc].sum += (tr[rc].r - tr[rc].l + 1) * tr[p].add;
		tr[lc].add += tr[p].add;
		tr[rc].add += tr[p].add;
		tr[p].add = 0;
	}
	return;
}

void build(LL p, LL l, LL r) {
	tr[p] = {l, r, a[l], 0};
	if (l == r)
		return;
	LL mid = l + r >> 1;
	build(lc, l, mid);
	build(rc, mid + 1, r);
	pushup(p);
}

void update(LL p, LL x, LL y, LL k) {
	if (x <= tr[p].l && tr[p].r <= y) {
		tr[p].sum += (tr[p].r - tr[p].l + 1) * k;
		tr[p].add += k;
		return;
	}
	pushdown(p);
	LL mid = tr[p].l + tr[p].r >> 1;
	if (x <= mid)
		update(lc, x, y, k);
	if (y > mid)
		update(rc, x, y, k);
	pushup(p);
}

LL query(LL p, LL x, LL y) {
	if (x <= tr[p].l && tr[p].r <= y)
		return tr[p].sum;
	LL sum = 0;
	LL mid = tr[p].l + tr[p].r >> 1;
	pushdown(p);
	if (x <= mid)
		sum += query(lc, x, y);
	if (y > mid)
		sum += query(rc, x, y);
	return sum;
}

int main() {
	freopen("tmp.in", "r", stdin);
	n = fast_read_ll();
	q = fast_read_ll();
	w = fast_read_ll();

	for (int i = 1; i <= n; i++)
		a[i] = fast_read_ll();
	build(1, 1, n);
	LL blood = w;
	while (q--) {
		LL x, y, k, lvl = 1, w = blood;
		ans = 0;
		x = fast_read_ll();
		y = fast_read_ll();
		k = fast_read_ll();
		update(1, x, y, k);
		LL b = query(1, 1, n);
		while (1) {
			if (w <= b)
				break;
			w -= b;
			ans += n;
			b *= 2;
			lvl *= 2;
		}
		LL L = 1, R = n, mid = n;
		while (1) {
			if (w <= query(1, 1, mid)*lvl && w > query(1, 1, mid - 1)*lvl) {
				ans += mid - 1;
				break;
			}
			mid = L + R >> 1;
			if (w <= query(1, 1, mid)*lvl)
				R = mid;
			if (w > query(1, 1, mid)*lvl)
				L = mid + 1;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
2024/10/22 17:27
加载中...