SingKwenCat @ 2024-09-13 15:04:05
#include <iostream>
#include <algorithm>
#include <functional>
#define IOS std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define endl "\n"
using namespace std;
constexpr const int N = 1e5 + 5;
constexpr const int INF = 0x3f3f3f3f;
#define lc p<<1
#define rc p<<1|1
struct Node {
long long l, r, sum, add;
};
Node tr[N*4];
long long in[N];
long long n, m, op, x, y, k;
void pushup(long long p) {
tr[p].sum = tr[lc].sum + tr[rc].sum;
}
void build(long long l, long long r, long long p) {
tr[p] = {l, r, in[l], 0};
if (l == r) return ;
long long mid = (l + r) >> 1;
build(l, mid, lc);
build(mid + 1, r, rc);
pushup(p);
}
void pushdown(long long p) {
if (tr[p].add) {
tr[lc].sum += (tr[lc].r - tr[lc].l + 1) * tr[p].add;
tr[lc].add += tr[p].add;
tr[rc].sum += (tr[rc].r - tr[rc].l + 1) * tr[p].add;
tr[rc].add += tr[p].add;
tr[p].add = 0;
}
}
void modify(long long x, long long y, long long p, long long k) {
if (tr[p].l >= x && tr[p].r <= y) {
tr[p].add += k;
tr[p].sum += (tr[p].r - tr[p].l + 1) * k;
return ;
}
long long mid = (tr[p].l + tr[p].r) >> 1;
pushdown(p);
if (x <= mid) modify(x, y, lc, k);
if (y >= mid) modify(x, y, rc, k);
pushup(p);// 不能像上面一样左右之差加k,因为不知道究竟包含x~y的多少,
// 而在儿子节点更新完之后更新该节点就可以简单地得到该节点的值
}
long long query(long long x, long long y, long long p) {
if (tr[p].l >= x && tr[p].r <= y) return tr[p].sum;
long long mid = (tr[p].l + tr[p].r) >> 1;
long long sum = 0;
pushdown(p);
if (x <= mid) sum += query(x, y, lc);
if (y >= mid) sum += query(x, y, rc);
// pushup(p);// 没有修改,不用加k,所以也不用pushup
return sum;
}
int main() {
//IOS
scanf("%lld%lld", &n, &m);
for (long long i = 1; i <= n; ++i) scanf("%lld", &in[i]);
build(1, n, 1);
for (long long i = 0; i < m; ++i) {
scanf("%lld", &op);
if (op == 1) {
scanf("%lld%lld%lld", &x, &y, &k);
modify(x, y, 1, k);
}else if (op == 2) {
scanf("%lld%lld", &x, &y);
printf("%lld\n", query(x, y, 1));
}
}
return 0;
}
估计问题出在query和modify里面,注释掉不会报错,应该是两个都有问题,其他部分大概没有问题 编译没有出错,洛谷报了re,本地报的segmentation fault 猜测应该是数组越界,但是我找不到哪里越界了啊~~~
by SingKwenCat @ 2024-09-14 16:24:41
@SingKwenCat