imnotcfz @ 2024-08-29 21:55:35
#include <iostream>
using namespace std;
#define ll long long
ll n, m;
ll a[100005];
ll tree[400005], tag[100005];
inline void add(ll p, ll l, ll r, ll k) {
tag[p] += k;
tree[p] += k * (r - l + 1);
return;
}
inline void pushup(ll p) {
tree[p] = tree[p << 1] + tree[p << 1 | 1];
return;
}
inline void pushdown(ll p, ll l, ll r) {
ll mid = l + r >> 1;
add(p << 1, l, mid, tag[p]);
add(p << 1 | 1, mid + 1, r, tag[p]);
tag[p] = 0;
return;
}
inline void update(ll nl, ll nr, ll l, ll r, ll p, ll k) {
if (nl <= l && r <= nr) {
add(p, l, r, k);
// tag[p] += k;
return;
}
pushdown(p, l, r);
ll mid = l + r >> 1;
if (nl <= mid) update(nl, nr, l, mid, p << 1, k);
if (nr > mid) update(nl, nr, mid + 1, r, p << 1 | 1, k);
pushup(p);
return;
}
inline void build(ll p, ll l, ll r) {
tag[p] = 0;
if (l == r) {
tree[p] = a[l];
return;
}
ll mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
return;
}
inline ll query(ll nl, ll nr, ll l, ll r, ll p) {
ll ans = 0;
if (nl <= l && r <= nr) return tree[p];
ll mid = l + r >> 1;
pushdown(p, l, r);
if (nl <= mid) ans += query(nl, nr, l, mid, p << 1);
if (nr > mid) ans += query(nl, nr, mid + 1, r, p << 1 | 1);
return ans;
}
int main() {
cin >> n >> m;
for (ll i = 1 ; i <= n ; i++) cin >> a[i];
build(1, 1, n);
ll op, x, y, k;
while (m--) {
cin >> op >> x >> y;
if (op == 1) {
cin >> k;
update(x, y, 1, n, 1, k);
} else cout << query(x, y, 1, n, 1) << endl;
}
return 0;
}
寄录
by postpone @ 2024-08-29 21:57:49
@imnotcfz 如果RE的话,可能是 tag
开小了,要和线段树节点数量一样
by imnotcfz @ 2024-08-29 22:01:43
寄录链接放错了c 正确链接
by imnotcfz @ 2024-08-29 22:02:27
@postpone 后3个点 WA 了,我怀疑是数据范围问题,但是我都开 long long 了
by postpone @ 2024-08-29 22:10:17
@imnotcfz tag 开小了,我就改了一下 tag 数组大小就AC了,你试试
by imnotcfz @ 2024-08-29 22:15:09
@postpone A 了,谢谢大佬