样例过了,全WA,求助

P3372 【模板】线段树 1

__LePetitPrince__ @ 2023-08-04 21:18:48

#include <stdio.h>
#define lc (o << 1)
#define rc (o << 1 | 1)
typedef long long LL;
#define S (1e5 + 5)
long long sum[400005], tag[400005];
long long a[100005];
int n, q;
void pushup(int o) {
    sum[o] = sum[lc] + sum[rc];
}
void pushdown(int o, int l, int r) {
    tag[lc] += tag[o];
    tag[rc] += tag[o];
    int mid = (l + r) / 2;
    sum[lc] += ((mid - l + 1) * tag[o]);
    sum[rc] += ((r - mid) * tag[o]);
    tag[o] = 0;
}
void build(int o, int l, int r) {
    if (l == r) {
        sum[o] = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    pushup(o);
}
LL query(int o, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
        return sum[o];
    }
    LL mid = (l + r) / 2, ans = 0;
    pushdown(o, l, r);
    if (ql <= mid) {
        ans += query(lc, l, mid, ql, qr);
    }
    if (mid < qr) {
        ans += query(rc, mid + 1, r, ql, qr);
    }
    return ans;
}
void update(int o, int l, int r, int pl, int pr, int v) {
    if (pl <= l && r <= pr) {
        tag[o] += v;
        sum[o] += ((r - l + 1) * v);
        return;
    }
    if (r < pl || l > pr) {
        return;
    }
    pushdown(o, l, r);
    int mid = (l + r) / 2;
    update(lc, l, mid, pl, pr, v);
    update(rc, mid + 1, r, pl, pr, v);
    pushup(o);
}
int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
    }
    build(1, 1, n);
    LL op, x, y, k;
    while (q--) {
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%d%d", &x, &y, &k);
            update(1, 1, n, x, y, k);
        } else {
            scanf("%d%d", &x, &y);
            printf("%lld\n", query(1, 1, n, x, y));
        }
    }
    return 0;
}

Orz


by 北射天狼 @ 2023-08-05 08:32:41

@LePetitPrince 兄弟,#define int long long 即可。


by 北射天狼 @ 2023-08-05 08:33:13

@LePetitPrince 我给你全部开了 long long 就过了,应该是在哪里炸了。


by __LePetitPrince__ @ 2023-08-05 08:35:20

@北射天狼 害,查了一下,把有些 %d 改 %lld 就过了


by 半只蒟蒻 @ 2023-08-05 08:37:13

@LePetitPrince 建议将 scanf("%d%d%d", &x, &y, &k); 改为 scanf("%lld%lld%lld", &x, &y, &k);


by 半只蒟蒻 @ 2023-08-05 08:37:56

@LePetitPrince 另外,update 的参数列表里面 v 的类型改成 long long


by __LePetitPrince__ @ 2023-08-05 08:41:19

@半只蒟蒻 谢谢


by CSP_AK_zc @ 2023-08-05 08:55:23

@LePetitPrince 要用long long


by 初星逝者 @ 2023-08-05 10:27:13

@LePetitPrince 不是昨天才刚帮你调了嘛,还没过?


|