停止工作了,求助

P3372 【模板】线段树 1

__LePetitPrince__ @ 2023-08-04 16:13:03

#include <stdio.h>
#define lc (o << 1)
#define rc (o << 1 | 1)
typedef long long LL;
const int S = 1e5 + 5;
LL sum[S * 4], tag[S * 4];
LL a[S];
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 p, int v) {
    if (l == r) {
        sum[o] += v;
        return;
    }
    int mid = (l + r) / 2;
    if (p <= mid) {
        update(lc, l, mid, p, v);
    } else {
        update(rc, mid + 1, r, p, v);
    }
    pushup(o);
}*/
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%d", &x, &y);
            printf("%lld\n", query(1, 1, n, x, y));
        }
    }
    return 0;
}

by 初星逝者 @ 2023-08-04 16:44:13

而且最好long long 全用%lld


by 初星逝者 @ 2023-08-04 16:59:00

@LePetitPrince 就这几个问题,然后就A了


by __LePetitPrince__ @ 2023-08-04 19:31:20

@初星逝者 我是sb,多谢 Orz


上一页 |