最后三个wa了,求教

P3372 【模板】线段树 1

隐公元年 @ 2022-10-22 15:35:18

//
// Created by xuqing on 2022/10/22.
//

#include<iostream>

#define N 100005
long long int l[N << 2], r[N << 2];
long long int add[N << 2], res[N << 2];
long long int li[N];
int n, m;
using namespace std;

void pushup(int root) {
    res[root] = (res[root << 1 | 0] + res[root << 1 | 1]);
}

void pushdown(int root) {
    res[root << 1 | 0] += add[root] * (r[root << 1 | 0] - l[root << 1 | 0] + 1);
    res[root << 1 | 1] += add[root] * (r[root << 1 | 1] - l[root << 1 | 1] + 1);

    add[root << 1 | 0] += add[root];
    add[root << 1 | 1] += add[root];

    add[root] = 0;
}

long long int build(int root, int lef, int rig, long long int li[]) {
    l[root] = lef, r[root] = rig, add[root] = 0;
    if (lef == rig) {
        return res[root] = li[lef];
    }
    int mid = (lef + rig) >> 1;
    return res[root] = build(root << 1 | 0, lef, mid, li) + build(root << 1 | 1, mid + 1, rig, li);
}

void addx(int root, int lef, int rig, int x) {
    if (lef <= l[root] && r[root] <= rig) {
        add[root] += x;
        res[root] += x * (r[root] - l[root] + 1);
        return;
    }
    if (l[root] > rig || r[root] < lef) {
        return;
    }

    pushdown(root);
    addx(root << 1 | 0, lef, rig, x);
    addx(root << 1 | 1, lef, rig, x);
    pushup(root);
}

int query(int root, int lef, int rig) {
    if (lef <= l[root] && r[root] <= rig) {
        return res[root];
    }
    if (l[root] > rig || r[root] < lef) {
        return 0;
    }
    pushdown(root);
    int x = query(root << 1 | 0, lef, rig) + query(root << 1 | 1, lef, rig);
    pushup(root);
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> li[i];
    }
    build(1, 0, n - 1, li);

    for (int i = 0; i < m; i++) {
        int temp;
        cin >> temp;
        if (temp == 1) {
            int x, y, k;
            cin >> x >> y >> k;
            addx(1, x - 1, y - 1, k);
        } else {
            int x, y;
            cin >> x >> y;
            cout << query(1, x - 1, y - 1) << endl;
        }
    }
    return 0;
}

by Micnation_AFO @ 2022-10-22 15:50:22

@隐公元年

void addx(int root, int lef, int rig, int x)

xlong long


by songtj @ 2022-10-22 15:54:01

@隐公元年

\LARGE{\textbf{十年OI一场空,不开long long见祖宗}}

by dodo487 @ 2022-10-26 18:58:30

十年OI一场空,___


by 隐公元年 @ 2022-10-30 22:41:34

AC了,感谢大佬的帮助。 query还有addx是应该开long long 0rz


|