結果

問題 No.1234 典型RMQ
ユーザー MisterMister
提出日時 2020-09-18 21:30:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 4,140 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 96,824 KB
実行使用メモリ 8,112 KB
最終ジャッジ日時 2023-08-08 18:32:56
合計ジャッジ時間 5,630 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 137 ms
7,780 KB
testcase_07 AC 105 ms
4,380 KB
testcase_08 AC 144 ms
7,720 KB
testcase_09 AC 125 ms
5,328 KB
testcase_10 AC 140 ms
7,772 KB
testcase_11 AC 134 ms
7,828 KB
testcase_12 AC 123 ms
5,500 KB
testcase_13 AC 105 ms
4,380 KB
testcase_14 AC 124 ms
5,636 KB
testcase_15 AC 120 ms
5,456 KB
testcase_16 AC 139 ms
7,788 KB
testcase_17 AC 124 ms
5,548 KB
testcase_18 AC 93 ms
4,376 KB
testcase_19 AC 143 ms
7,716 KB
testcase_20 AC 85 ms
7,984 KB
testcase_21 AC 136 ms
7,772 KB
testcase_22 AC 109 ms
8,112 KB
testcase_23 AC 110 ms
8,108 KB
testcase_24 AC 109 ms
8,044 KB
testcase_25 AC 109 ms
8,052 KB
testcase_26 AC 109 ms
8,036 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>

template <class T, class E>
struct LazySegmentTree {
    using DMerger = std::function<T(T, T)>;
    using OMerger = std::function<E(E, E)>;
    using Applier = std::function<T(T, E, int)>;

    int length;

    T d_unit;
    E o_unit;

    std::vector<T> dat;
    std::vector<E> ope;

    DMerger dmerge;
    OMerger omerge;
    Applier app;

    explicit LazySegmentTree(int n,
                             T d_unit, E o_unit,
                             DMerger dmerge,
                             OMerger omerge,
                             Applier app)
        : length(1),
          d_unit(d_unit),
          o_unit(o_unit),
          dmerge(dmerge),
          omerge(omerge),
          app(app) {
        while (length < n) length <<= 1;

        dat.assign(length * 2, d_unit);
        ope.assign(length * 2, o_unit);
    }

    template <class Container>
    explicit LazySegmentTree(const Container& elems,
                             T d_unit, E o_unit,
                             DMerger dmerge,
                             OMerger omerge,
                             Applier app)
        : length(1),
          d_unit(d_unit),
          o_unit(o_unit),
          dmerge(dmerge),
          omerge(omerge),
          app(app) {
        int n = elems.size();
        while (length < n) length <<= 1;

        dat.assign(length * 2, d_unit);
        ope.assign(length * 2, o_unit);

        std::copy(elems.begin(), elems.end(), dat.begin() + length);

        for (int nidx = length - 1; nidx >= 1; --nidx) {
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = dmerge(vl, vr);
        }
    }

    void propagate(int nidx, int len) {
        if (ope[nidx] == o_unit) return;

        // propagate
        if (len > 1) {
            ope[nidx * 2 + 0] = omerge(ope[nidx * 2 + 0], ope[nidx]);
            ope[nidx * 2 + 1] = omerge(ope[nidx * 2 + 1], ope[nidx]);
        }

        // update data
        dat[nidx] = app(dat[nidx], ope[nidx], len);
        ope[nidx] = o_unit;
    }

    void update(int ql, int qr, E e, int nidx, int nl, int nr) {
        propagate(nidx, nr - nl);

        if (nr <= ql || qr <= nl) return;
        if (ql <= nl && nr <= qr) {
            ope[nidx] = omerge(ope[nidx], e);
            propagate(nidx, nr - nl);
            return;
        }

        int nm = (nl + nr) / 2;
        update(ql, qr, e, nidx * 2 + 0, nl, nm);
        update(ql, qr, e, nidx * 2 + 1, nm, nr);

        // update data
        dat[nidx] = dmerge(dat[nidx * 2 + 0], dat[nidx * 2 + 1]);
    }

    void update(int ql, int qr, E e) { return update(ql, qr, e, 1, 0, length); }

    T fold(int ql, int qr, int nidx, int nl, int nr) {
        propagate(nidx, nr - nl);

        if (nr <= ql || qr <= nl) return d_unit;
        if (ql <= nl && nr <= qr) return dat[nidx];

        int nm = (nl + nr) / 2;
        T vl = fold(ql, qr, nidx * 2 + 0, nl, nm);
        T vr = fold(ql, qr, nidx * 2 + 1, nm, nr);
        return dmerge(vl, vr);
    }

    T fold(int ql, int qr) { return fold(ql, qr, 1, 0, length); }

    T get(int idx) { return fold(idx, idx + 1); }
    T fold_all() { return fold(0, length); }
};

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n;
    std::cin >> n;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;

    LazySegmentTree<lint, lint>
        seg(
            xs, INF, 0,
            [](auto a, auto b) { return std::min(a, b); },
            [](auto e, auto f) { return e + f; },
            [](auto a, auto e, int) { return a + e; });

    int q;
    std::cin >> q;
    while (q--) {
        int k, l, r;
        lint c;
        std::cin >> k >> l >> r >> c;
        --l;

        switch (k) {
            case 1: {
                seg.update(l, r, c);
                break;
            }
            case 2: {
                std::cout << seg.fold(l, r) << "\n";
                break;
            }
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0