线段树0分求助!!!(无lazy标记)

P3372 【模板】线段树 1

HanZig @ 2023-08-07 15:47:26

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
ll a[MAXN];
struct SegmentTree
{
    #define ls (id << 1)
    #define rs (id << 1 | 1)
    struct Segment
    {
        int  l, r;
        ll val;
    }seg[MAXN << 2];
    void merge(int id)
    {
        seg[id].val = seg[ls].val + seg[rs].val;
    }
    void build(int id, int l, int r)
    {
        if (l == r)
        {
            seg[id].val = a[l];
            return;
        }
        int mid = (l + r) / 2;
        build(ls, l, mid), build(rs, mid + 1, r);
        merge(id);
    }
    ll query(int id, int l, int r)
    {
        if (seg[id].l > r || seg[id].r < l)
        {
            return 0;
        }
        if (seg[id].l >= l && seg[id].r <= r)
        {
            return seg[id].val;
        }
        return query(ls, l, r) + query(rs, l, r);
    }
    void change(int id, int l, int r, int k)
    {
        if (seg[id].l > r || seg[id].r < l)
        {
            return;
        }
        if (seg[id].l >= l && seg[id].r <= r)
        {
            seg[id].val += k;
        }
        change(ls, l, r, k), change(rs, l, r, k);
    }
}SGT;
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    SGT.build(1, 1, n);
    while (m--)
    {
        int op;
        cin >> op;
        if (op == 1)
        {
            int x, y, k;
            cin >> x >> y >> k;
            SGT.change(1, x, y, k);
        }
        else
        {
            int l, r;
            cin >> l >> r;
            cout << SGT.query(1, l, r) << endl;
        }
    }
    return 0;
}

by Argvchs @ 2023-08-07 15:49:14

@HanZig 无lazy标记是假的


by HanZig @ 2023-08-07 15:49:36

输出全是0(cry……)


by Argvchs @ 2023-08-07 15:49:44

@HanZig 必须要lazy标记或者标记永久化


by HanZig @ 2023-08-07 15:51:06

可是我没学过lazy标记……


by HonkaiStarRail @ 2023-08-07 15:53:58

@HanZig 现在去学


by HanZig @ 2023-08-07 15:59:23

ok


by HanZig @ 2023-08-07 16:02:49

五年级的蒟蒻xxs麻烦各位大佬给一些教程,谢谢


by HanZig @ 2023-08-09 00:16:54

lazy标记学完啦!谢谢各位大佬!

评测记录

此帖结


|