线段树18分求调

P4513 小白逛公园

NanYanNanChui @ 2024-03-14 14:55:28

又是线段树学废的一天(x

#include <bits/stdc++.h>

#define int long long

template<class Info>
struct SegmentTree {
    int n;
    std::vector<Info> info;

    SegmentTree() : n(0) {}

    SegmentTree(int n_, Info v_ = Info()) {
        init(n_, v_);
    }

    template<class T>
    SegmentTree(std::vector<T> init_) {
        init(init_);
    }

    void init(int n_, Info v_ = Info()) {
        init(std::vector(n_, v_));
    }

    template<class T>
    void init(std::vector<T> init_) {
        n = init_.size();
        info.assign(4 << std::__lg(n), Info());
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init_[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }

    void pull(int p) {
        info[p] = info[2 * p] + info[2 * p + 1];
    }

    void modify(int p, int l, int r, int x, const Info &v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }

    void modify(int p, const Info &v) {
        modify(1, 0, n, p, v);
    }

    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
    }

    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }

    template<class F>
    int findFirst(int p, int l, int r, int x, int y, F pred) {
        if (l >= y || r <= x || !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        int res = findFirst(2 * p, l, m, x, y, pred);
        if (res == -1) {
            res = findFirst(2 * p + 1, m, r, x, y, pred);
        }
        return res;
    }

    template<class F>
    int findFirst(int l, int r, F pred) {
        return findFirst(1, 0, n, l, r, pred);
    }

    template<class F>
    int findLast(int p, int l, int r, int x, int y, F pred) {
        if (l >= y || r <= x || !pred(info[p])) {
            return -1;
        }
        if (r - l == 1) {
            return l;
        }
        int m = (l + r) / 2;
        int res = findLast(2 * p + 1, m, r, x, y, pred);
        if (res == -1) {
            res = findLast(2 * p, l, m, x, y, pred);
        }
        return res;
    }

    template<class F>
    int findLast(int l, int r, F pred) {
        return findLast(1, 0, n, l, r, pred);
    }
};
constexpr int inf = 1e18;
struct Info {
    int sum = -inf, maxl = -inf, maxr = -inf, max = -inf;
};

Info operator+(Info l, Info r) {
    Info now{};
    now.max = std::max(l.maxr, r.maxl);
    now.max += std::max(0ll, std::min(l.maxr, r.maxl));
    now.max = std::max({l.max, r.max, now.max});

    now.maxl = std::max(l.maxl, r.maxl + l.sum);
    now.maxr = std::max(r.maxr, l.maxr + r.sum);
    now.sum = l.sum + r.sum;
    return now;
}

void Fuck() {
    int n, q;
    std::cin >> n >> q;

    std::vector<Info> a(n);
    for (int i = 0; i < n; ++i) {
        int x;
        std::cin >> x;
        a[i] = {x, x, x, x};
    }

    SegmentTree<Info> seg(a);
    while (q--) {
        int op, x, y;
        std::cin >> op >> x >> y;
        if (op == 1) {
            int l = std::min(x, y), r = std::max(x, y);
            std::cout << seg.rangeQuery(l - 1, r).max << '\n';
        } else {
            seg.modify(x - 1, {y, y, y, y});
        }
    }
}

int32_t main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);

    int _ = 1;
    //std::cin >> _;

    while (_--) {
        Fuck();
    }

    std::cerr << "Time:" << 1000 * ((double) clock()) / (double) CLOCKS_PER_SEC << "ms\n";

    return 0;
}

by NanYanNanChui @ 2024-03-15 01:14:32

找到问题了,inf设置成1e18爆精度了,改成1e9就过了(有点怪,还在找原因


by jianhe @ 2024-11-07 15:58:58

/bx 一样的错误


|