結果

問題 No.1234 典型RMQ
ユーザー t33ft33f
提出日時 2020-09-19 01:26:30
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,854 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 100,992 KB
最終ジャッジ日時 2024-04-27 05:01:27
合計ジャッジ時間 1,568 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:8:31: error: no template named 'numeric_limits'
    8 |     static D unitD() { return numeric_limits<D>::max(); }
      |                               ^
1 error generated.

ソースコード

diff #

#include <iostream>
using namespace std;

class SegTreeLazy {
    // monoid and action
    using D = long long int;
    using A = long long int;
    static D unitD() { return numeric_limits<D>::max(); }
    static A unitA() { return 0; }
    static const D compose(const D& lhs, const D& rhs) {
        return min(lhs, rhs);
    }
    static const D applyA(const A& a, const D& x, int l, int r) {
        return a + x;
    }
    static const A appendA(const A& a, const A& b) {
        return a + b;
    }
    // ---

    void force(int i, int l, int r) {
        if (delay[i] != unitA()) {
            data[i] = applyA(delay[i], data[i], l, r);
            if (r - l > 1) {
                delay[2*i+1] = appendA(delay[i], delay[2*i+1]);
                delay[2*i+2] = appendA(delay[i], delay[2*i+2]);
            }
            delay[i] = unitA();
        }
    }
    const int N;
    D *data;
    A *delay;
    int calc_size(int n) { int ret = 1; while (n > ret) ret *= 2; return ret; }
public:
    explicit SegTreeLazy(int n) : N(calc_size(n)) {   // [0, n-1]
        data = new D[2*N-1]();
        delay = new A[2*N-1]();
    }
    ~SegTreeLazy() { delete[] data; delete[] delay; }
    D query(int a, int b, int i = -1, int l = -1, int r = -1) {
        if (i == -1) { i = 0; l = 0; r = N; }
        if (r <= a || b <= l) return unitD();
        force(i, l, r);
        if (a <= l && r <= b) return data[i];
        if (r - l == 1) return data[i];
        const int m = (l+r)/2;
        if (b <= m) return query(a, b, 2*i+1, l, m);
        if (m <= a) return query(a, b, 2*i+2, m, r);
        D v1 = query(a, b, 2*i+1, l, m), v2 = query(a, b, 2*i+2, m, r);
        return compose(v1, v2);
    }
    void add(int a, int b, A v, int i = -1, int l = -1, int r = -1) {
        if (i == -1) { i = 0; l = 0; r = N; }
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) { delay[i] = appendA(v, delay[i]); force(i, l, r); return; }
        add(a, b, v, 2*i+1, l, (l+r)/2); force(2*i+1, l, (l+r)/2);
        add(a, b, v, 2*i+2, (l+r)/2, r); force(2*i+2, (l+r)/2, r);
        data[i] = compose(data[2*i+1], data[2*i+2]);
    }
    void init(D *a, int n) {
        for (int i = 0; i < n; i++) data[i+N-1] = a[i];
        for (int i = N-2; i >= 0; i--) data[i] = compose(data[2*i+1], data[2*i+2]);
    }
    // D get(int i) { return query(i, i+1); }
    // void update(int i, T v) {
    //     int ov = get(i);
    //     add(i, i+1, v-ov); // BEWARE: assumes add is `+'
    // }
};


int main() {
    int n; cin >> n;
    long long a[n]; for (int i = 0; i < n; i++) cin >> a[i];
    SegTreeLazy st(n+1);
    st.init(a, n);
    int q; cin >> q;
    while (q--) {
        int k, l, r, c; cin >> k >> l >> r >> c;
        if (k == 1) {
            st.add(l-1, r, c);
        } else {
            cout << st.query(l-1, r) << endl;
        }
    }
}
0