求助:wa8910,已开long long,但输出负数

P3372 【模板】线段树 1

JGYPJ @ 2024-10-19 18:21:46

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

class st
{
private:
    struct node
    {
        int l, r;
        long long val, add; /* data */
    };
    vector<node> t; // l,r,val,add
    int n;
    void build(int p, int l, int r, vector<long long> &a)
    {
        // cout << p << " " << l << " " << r << endl;
        if (l == r)
        {
            t[p] = {l, r, a[l], 0};
            // cout << p << " " << l << " " << r << " " << a[p] << endl;
            return;
        }
        int mid = (l + r) >> 1;
        build(p * 2, l, mid, a);
        build(p * 2 + 1, mid + 1, r, a);
        t[p] = {l, r, t[p * 2].val + t[p * 2 + 1].val, 0};
        // cout << p << " " << l << " " << r << " " << t[p].val << endl;
    }
    void pushdown(int p)
    {
        if (t[p].l == t[p].r)
            return;
        auto &[l, r, val, add] = t[p];
        t[p * 2].add += add, t[p * 2 + 1].add += add;
        t[p * 2].val += 1LL * (t[p * 2].r - t[p * 2].l + 1) * add;
        t[p * 2 + 1].val += 1LL * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1) * add;
        add = 0;
    }

public: // error:"add" is a tag for p's sons!!!!!!!!!!!!!!!
        //~~~~~~~~~~~!!!!!!!!!!!!!!!!!!!
    st(int _n, vector<long long> &a) : n(_n)
    {
        t.resize(4 * (n + 10));
        build(1, 1, n, a);
    }
    void add(int p, int l, int r, long long va)
    {
        pushdown(p);
        auto &[ll, rr, V, ad] = t[p];
        if (ll >= l && rr <= r)
        {
            ad += va;
            // pushdown(p);
            V += 1LL * (rr - ll + 1) * va;
            return;
        }
        int mid = (ll + rr) >> 1;
        if (l <= mid)
            add(p * 2, l, r, va);
        if (r > mid)
            add(p * 2 + 1, l, r, va);
        // pushdown(p * 2);
        // pushdown(p * 2 + 1);
        t[p].val = t[p * 2].val + t[p * 2 + 1].val;
    }
    long long ask(int p, int l, int r)
    {
        int ans = 0;
        pushdown(p);
        auto [ll, rr, v, ad] = t[p];
        if (ll >= l && rr <= r)
        {
            return v;
        }
        int mid = (ll + rr) >> 1;
        if (l <= mid)
            ans = ask(p * 2, l, r);
        if (r > mid)
            ans += ask(p * 2 + 1, l, r);
        // pushdown(p * 2);
        // pushdown(p * 2 + 1);
        // t[p].val = t[p * 2].val + t[p * 2 + 1].val;
        return ans;
    }
    void debg()
    {
        // cout << t.size() << endl;

        // for (int i = 0; i < t.size(); i++)
        // {
        //     cout << t[i].val << endl;
        // }
        // add(1, 1, 5, 1);
        // add(1, 1, 4, 1);
        // add(1, 2, 3, 1);
        // for (int i = 1; i <= n;i++)
        //     cout << ask(1, 1, i) << " ";
        //     exit(0);
        // for (int i = 1; i <= n; i++)
        //     cout << ask(1, i, i) << " ";
        // puts("----------------------");
        // exit(0);
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    vector<long long> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    st tree(n, a);
    // tree.debg();
    // return 0;
    // for (int i = 1; i <= n; i++)
    //     cout << tree.ask(1, i, i) << endl;
    // return 0;
    for (int i = 1; i <= m; i++)
    {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1)
        {
            long long k;
            cin >> k;
            tree.add(1, x, y, k);
        }
        else
        {
            cout << tree.ask(1, x, y) << endl;
        }
        // tree.debg();
    }
    return 0;
}

by eternal_silence @ 2024-10-20 11:15:35

ask函数里面的ans的类型是int,应该改为long long,不然会爆


by eternal_silence @ 2024-10-20 11:19:05

@JGYPJ


by JGYPJ @ 2024-10-24 14:10:51

@eternal_silence 过了,感谢大佬((ヾ(。ゝω・)ノ


|