結果

問題 No.1234 典型RMQ
ユーザー tomatoma
提出日時 2020-09-18 22:38:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 4,066 bytes
コンパイル時間 2,605 ms
コンパイル使用メモリ 204,608 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-26 11:15:54
合計ジャッジ時間 10,051 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 307 ms
7,936 KB
testcase_07 AC 244 ms
5,376 KB
testcase_08 AC 317 ms
8,064 KB
testcase_09 AC 273 ms
5,760 KB
testcase_10 AC 314 ms
7,936 KB
testcase_11 AC 292 ms
7,936 KB
testcase_12 AC 264 ms
5,632 KB
testcase_13 AC 232 ms
5,376 KB
testcase_14 AC 269 ms
5,760 KB
testcase_15 AC 265 ms
5,504 KB
testcase_16 AC 309 ms
8,192 KB
testcase_17 AC 278 ms
5,632 KB
testcase_18 AC 238 ms
5,376 KB
testcase_19 AC 332 ms
8,064 KB
testcase_20 AC 215 ms
8,192 KB
testcase_21 AC 302 ms
8,064 KB
testcase_22 AC 260 ms
8,064 KB
testcase_23 AC 253 ms
8,064 KB
testcase_24 AC 244 ms
8,064 KB
testcase_25 AC 236 ms
8,064 KB
testcase_26 AC 237 ms
8,320 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"

using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pll = pair<ll, ll>;
using tp3 = tuple<ll, ll, ll>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
template<typename Monoid, typename OperatorMonoid = Monoid>
class LazySegmentTree {
private:
    using F = function<Monoid(Monoid, Monoid)>;
    using G = function<Monoid(Monoid, OperatorMonoid, int)>;
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

    int sz; // 対応する配列の幅
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    const F f; // 2区間マージ演算(data-data-ボトムアップマージ)
    const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算)
    const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算)
    const Monoid M1;          // モノイド単位元 (data単位元)
    const OperatorMonoid OM0; // 作用素単位元 (lazy単位元)

    void propagate(int idx, int len) {
        // 幅lenのlazy[idx]が存在するとき、値を下に流す
        if (lazy[idx] != OM0) {
            if (idx < sz) {
                lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]);
                lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]);
            }
            data[idx] = g(data[idx], lazy[idx], len);
            lazy[idx] = OM0;
        }
    }
    Monoid update_impl(int a, int b, const OperatorMonoid& val, int idx, int l, int r) {
        propagate(idx, r - l);
        if (r <= a || b <= l)return data[idx];
        else if (a <= l && r <= b) {
            lazy[idx] = h(lazy[idx], val);
            propagate(idx, r - l);
            return data[idx];
        }
        else return data[idx] = f(
            update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1),
            update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r)
        );
    }
    Monoid query_impl(int a, int b, int idx, int l, int r) {
        propagate(idx, r - l);
        if (r <= a || b <= l)return M1;
        else if (a <= l && r <= b)return data[idx];
        else return f(
            query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1),
            query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r)
        );
    }

public:
    // init忘れに注意
    LazySegmentTree(int n, const F f, const G g, const H h,
        const Monoid& M1, const OperatorMonoid OM0)
        :f(f), g(g), h(h), M1(M1), OM0(OM0) {
        sz = 1;
        while (sz < n)sz <<= 1;
        data.assign(2 * sz, M1);
        lazy.assign(2 * sz, OM0);
    }
    void build(const vector<Monoid>& vals) {
        rep(idx, vals.size())data[idx + sz] = vals[idx];
        for (int idx = sz - 1; idx > 0; idx--) {
            data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
        }
    }
    Monoid update(int a, int b, const OperatorMonoid& val) {
        return update_impl(a, b, val, 1, 0, sz);
    }
    Monoid query(int a, int b) {
        return query_impl(a, b, 1, 0, sz);
    }
    Monoid operator[](const int& idx) {
        return query(idx, idx + 1);
    }
};
// ============ template finished ============

int main()
{
    int N;
    cin >> N;
    vector<ll> a(N);
    rep(i, N)cin >> a[i];

    auto f = [](ll a, ll b) {return min(a, b); };
    auto g = [](ll data, ll query, int len) {return data + query; };
    auto h = [](ll lazy, ll query) {return lazy + query; };
    LazySegmentTree<ll, ll> lst(N, f, g, h, INFL, 0);
    lst.build(a);

    int Q;
    cin >> Q;
    rep(_, Q) {
        int com, l, r, c;
        cin >> com >> l >> r >> c;
        l--;
        if (com == 1) {
            lst.update(l, r, c);
        }
        else {
            cout << lst.query(l, r) << endl;
        }
    }
    return 0;
}
0