萌新才看了一点点线段树,为什么我样例不对咧

P3372 【模板】线段树 1

hehe123313 @ 2023-12-22 16:26:44

#include<iostream>

using namespace std;
const int N = 4 * 1E5 + 10;
struct tree {
    int l;
    int r;
    int sum;
    int add;
} tr[N];
int n, m;
int a[N];

void pushup(int u) {
    tr[u].sum = tr[2 * u].sum + tr[2 * u + 1].sum;
}

void build(int u, int l, int r) {
    if (l == r)
        tr[u] = {l, r, a[l], 0};
    else {
        tr[u] = {l, r};
        tr[u].add = 0;
        int mid = l + r >> 1;
        build(2 * u, l, mid);
        build(2 * u + 1, mid + 1, r);
        pushup(u);
    }
}

void pushdown(int u) {
    if (tr[u].add = 0) {
        tr[2 * u].sum += tr[u].add * (tr[2 * u].r - tr[2 * u].l + 1);
        tr[2 * u + 1].sum += tr[u].add * (tr[2 * u + 1].r - tr[2 * u + 1].l + 1);
        tr[2 * u].add += tr[u].add;
        tr[2 * u + 1].add += tr[u].add;
        tr[u].add = 0;
    }
}

int query(int u, int l, int r) {
    int sum = 0;
    if (tr[u].l >= l && tr[u].r <= r)
        return tr[u].sum;
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid)
        sum = query(2 * u, l, r);
    if (r > mid)
        sum += query(2 * u + 1, mid + 1, r);
    return sum;
}

void update(int u, int l, int r, int k) {
    if (tr[u].l >= l && tr[u].r <= r) {
        tr[u].sum += k * (tr[u].r - tr[u].l + 1);
        tr[u].add += k;
        return;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    pushdown(u);
    if (l <= mid)
        update(2 * u, l, r, k);
    if (r > mid)
        update(2 * u + 1, l, r, k);
    pushup(u);
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    while (m--) {
        int x, l, r, k;
        cin >> x >> l >> r;
        if (x == 1) {
            cin >> k;
            update(1, l, r, k);
        }
        if (x == 2) {
            cout << query(1, l, r) << endl;
        }
    }
    return 0;
}

by hehe123313 @ 2023-12-22 16:27:05

20一直是16


by lsj2009 @ 2023-12-22 16:40:31

@hehe123313

if (tr[u].add = 0)

改成

if (tr[u].add != 0)

即可。


by L_zaa_L @ 2023-12-22 16:42:35

#include<iostream>
#define int long long//3
using namespace std;
const int N = 4 * 1E5 + 10;
struct tree {
    int l;
    int r;
    int sum;
    int add;
} tr[N];
int n, m;
int a[N];

void pushup(int u) {
    tr[u].sum = tr[2 * u].sum + tr[2 * u + 1].sum;
}

void build(int u, int l, int r) {
    if (l == r)
        tr[u] = {l, r, a[l], 0};
    else {
        tr[u] = {l, r,0,0};//1
        tr[u].add = 0;
        int mid = (l + r) >> 1;
        build(2 * u, l, mid);
        build(2 * u + 1, mid + 1, r);
        pushup(u);
    }
}

void pushdown(int u) {
    if (tr[u].add != 0) {//2
        tr[2 * u].sum += tr[u].add * (tr[2 * u].r - tr[2 * u].l + 1);
        tr[2 * u + 1].sum += tr[u].add * (tr[2 * u + 1].r - tr[2 * u + 1].l + 1);
        tr[2 * u].add += tr[u].add;
        tr[2 * u + 1].add += tr[u].add;
        tr[u].add = 0;
    }
}

int query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r)
        return tr[u].sum;
    pushdown(u);
    int sum = 0;
    int mid = (tr[u].l + tr[u].r) >> 1;
    if (l <= mid)
        sum = query(2 * u, l, r);
    if (r > mid)
        sum += query(2 * u + 1, l, r);//1
    return sum;
}

void update(int u, int l, int r, int k) {
    if (tr[u].l >= l && tr[u].r <= r) {
        tr[u].sum += k * (tr[u].r - tr[u].l + 1);
        tr[u].add += k;
        return;
    }
    int mid = (tr[u].l + tr[u].r) >> 1;
    pushdown(u);
    if (l <= mid)
        update(2 * u, l, r, k);
    if (r > mid)
        update(2 * u + 1, l, r, k);
    pushup(u);
}

signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    while (m--) {
        int x, l, r, k;
        cin >> x >> l >> r;
        if (x == 1) {
            cin >> k;
            update(1, l, r, k);
        }
        if (x == 2) {
            cout << query(1, l, r) << endl;
        }
    }
    return 0;
}

我帮你改了,你自己看看,主要问题有三个:

  1. 没开long long。
  2. lazy标记判断打错了。
  3. 查询时改了查询区间。

by L_zaa_L @ 2023-12-22 16:42:44

@hehe123313


by hehe123313 @ 2023-12-22 20:13:26

@zaa okok谢谢佬


|