线段树洛谷全RE代码求调,本地不会RE但与输出结果不符

P3372 【模板】线段树 1

lwx20211103 @ 2022-11-13 09:13:03

#include <bits/stdc++.h>
#define int long long
using namespace std;

struct tree
{
    int l, r, prev, add;
} t[444444];

int nums[444444];

inline int read()
{
    char c;
    int x = 0, f = 1;
    c = getchar();
    if (c == '-')
    {
        f = -1;
    }

    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

void build(int i, int l, int r)
{
    t[i].l = l, t[i].r = r;
    if (l == r)
    {
        t[i].prev = nums[l];
        return;
    }
    int mid = l + r >> 1;
    build(i << 1, l, mid);
    build(i << 1 | 1, mid + 1, r);
    t[i].prev = t[i << 1].prev + t[i << 1 | 1].prev;
}

void mark(int i)
{
    if (t[i].add)
    {
        t[i << 1].prev += t[i].add * (t[i << 1].r - t[i << 1].l + 1);
        t[i << 1 | 1].prev += t[i].add * (t[i << 1 | 1].r - t[i << 1 | 1].l + 1);
        t[i << 1].add += t[i].add;
        t[i << 1 |1].add += t[i].add;
        t[i].add = 0;
    }
}

void change(int i, int x, int y, int j)
{
    if (x <= t[i].l && y >= t[i].r)
    {
        t[i].prev += j * (t[i].r - t[i].l + 1);
        t[i].add += j;
        return;
    }
    mark(i);
    int mid = t[i].l + t[i].r  >> 1;
    if (x <= mid)
    {
        change(i << 1, x, y, j);
    }
    if (y > mid)
    {
        change(i << 1 | 1, x, y, j);
    }
    t[i].prev = t[i << 1].prev + t[i << 1 | 1].prev;
}

int ask(int i, int x, int y)
{
    if (x <= t[i].l && y >= t[i].r)
    {
        return t[i].prev;
    }
    mark(i);
    int mid = t[i].l + t[i].r >> 1;
    int ans = 0;
    if (x <= mid)
    {
        ans += ask(i << 1, x, y);
    }
    if (y > mid)
    {
        ans += ask(i << 1 | 1, x, y);
    }
    return ans;
}

signed main()
{
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    int n, m;
    n = read(), m = read();
    int i;
    for (i = 1; i <= n; i++)
    {
        nums[i] = read();
    }
    build(1, 1, n);
    for (i = 1; i <= m; i++)
    {
        int x, y, k;
        int op;
        op = read();
        if (op & 1)
        {
            x = read(), y = read(), k = read();
            change(1, x, y, k);
        }
        else
        {
            x = read(), y = read();
            cout << ask(1, x, y) << "\n";
        }
    }
    return 0;
}

by Micnation_AFO @ 2022-11-13 09:18:35

@lwx20211103 lg 没注释 freopen


by Micnation_AFO @ 2022-11-13 09:21:59

@lwx20211103 然后快读貌似锅了


by XOOR @ 2022-11-13 09:28:09

qs


by XKJie @ 2022-11-13 09:49:48

qs


by lwx20211103 @ 2022-11-13 10:15:18

谢谢各位的帮助,AC了,此帖结


|