結果

問題 No.1234 典型RMQ
ユーザー rokahikou1rokahikou1
提出日時 2020-10-21 17:59:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,514 bytes
コンパイル時間 1,780 ms
コンパイル使用メモリ 176,156 KB
実行使用メモリ 8,348 KB
最終ジャッジ日時 2023-09-28 14:17:39
合計ジャッジ時間 10,443 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 237 ms
7,732 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

// LazySegmentTree(0-indexed)
template <class Monoid, class OperatorMonoid = Monoid> class LazySegTree {
  private:
    using F = function<Monoid(Monoid, Monoid)>;
    using G = function<Monoid(Monoid, OperatorMonoid)>;
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
    using L = function<OperatorMonoid(OperatorMonoid, int)>;

    int n;
    const F f;
    const G g;
    const H h;
    const L reflect_len;
    const Monoid e;
    const OperatorMonoid o_e;
    vector<Monoid> node;
    vector<OperatorMonoid> lazy;

  public:
    LazySegTree(int sz, const F _f, const G _g, const H _h, const L _l,
                const Monoid &_e, const OperatorMonoid &_o_e)
        : f(_f), g(_g), h(_h), reflect_len(_l), e(_e), o_e(_o_e) {
        n = 1;
        while(n < sz)
            n *= 2;
        node.resize(2 * n - 1, e);
        lazy.resize(2 * n - 1, o_e);
    }

    void set(int k, const Monoid &x) { node[k + n - 1] = x; }

    void build() {
        for(int i = n - 2; i >= 0; i--) {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    // k番目のノードについて遅延評価
    void eval(int k, int l, int r) {
        // 遅延配列を見て空でなかったら値を伝播
        if(lazy[k] != o_e) {
            node[k] = g(node[k], reflect_len(lazy[k], r - l));
            // 伝播
            if(r - l > 1) {
                lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
                lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]);
            }
        }

        // 伝播の終了
        lazy[k] = o_e;
    }

    Monoid at(int k) { return query(k, k + 1); }

    // [a,b)区間加算
    void update(int a, int b, OperatorMonoid x, int k = 0, int l = 0,
                int r = -1) {
        if(r < 0)
            r = n;
        // まず評価
        eval(k, l, r);
        // 範囲外なら何もしない
        if(b <= l || r <= a)
            return;

        // 完全に被覆 遅延配列に値を入れ評価
        if(a <= l && r <= b) {
            lazy[k] = h(lazy[k], x);
            eval(k, l, r);
        }
        // そうでないとき
        // 子コードの値を再帰的に計算して計算済みの値をもってくる
        else {
            update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            node[k] = f(node[2 * k + 1], node[2 * k + 2]);
        }
    }

    // [a,b)区間取得
    Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if(r < 0)
            r = n;
        // まず評価
        eval(k, l, r);
        if(b <= l || r <= a)
            return e;
        if(a <= l && r <= b)
            return node[k];
        Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
};

template <class T, class U> struct RAMQ : LazySegTree<T, U> {
    using Segtree = LazySegTree<T, U>;
    static auto f(T x1, T x2) { return min(x1, x2); }
    static auto g(T x, U a) { return x + a; }
    static auto h(U a, U b) { return a + b; }
    static auto l(U a, int len) { return a * len; }

    RAMQ(int n, const T &e = LINF, const U &o_e = 0)
        : Segtree(n, f, g, h, l, e, o_e) {}
};

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];
    int Q;
    cin >> Q;
    RAMQ<ll, ll> ramq(N);
    rep(i, N) ramq.set(i, A[i]);
    ramq.build();
    while(Q--) {
        int k, l, r, c;
        cin >> k >> l >> r >> c;
        l--;
        if(k == 2) {
            cout << ramq.query(l, r) << endl;
        } else {
            ramq.update(l, r, c);
        }
    }
}
0