結果

問題 No.1234 典型RMQ
ユーザー 👑 NachiaNachia
提出日時 2020-09-27 14:27:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 2,997 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 172,956 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-26 11:36:36
合計ジャッジ時間 9,625 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 281 ms
8,448 KB
testcase_07 AC 231 ms
5,376 KB
testcase_08 AC 291 ms
8,736 KB
testcase_09 AC 261 ms
6,144 KB
testcase_10 AC 291 ms
8,716 KB
testcase_11 AC 279 ms
8,320 KB
testcase_12 AC 259 ms
5,888 KB
testcase_13 AC 235 ms
5,376 KB
testcase_14 AC 265 ms
6,144 KB
testcase_15 AC 249 ms
5,760 KB
testcase_16 AC 294 ms
8,576 KB
testcase_17 AC 259 ms
6,272 KB
testcase_18 AC 216 ms
5,376 KB
testcase_19 AC 295 ms
8,704 KB
testcase_20 AC 224 ms
8,832 KB
testcase_21 AC 285 ms
8,412 KB
testcase_22 AC 252 ms
8,832 KB
testcase_23 AC 254 ms
8,960 KB
testcase_24 AC 249 ms
8,832 KB
testcase_25 AC 245 ms
8,948 KB
testcase_26 AC 247 ms
8,832 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

template<
    class S,
    S(*op)(S a, S b),
    S(*e)(),
    class F,
    S(*mapping)(F a, S b),
    F(*composition)(F a, F b),
    F(*id)()
>
struct lazy_segtree {
private:
    struct Node { S v; F r; };
    int N;
    vector<Node> V;

    void spread(int i) {
        V[i].v = mapping(V[i].r, V[i].v);
        if (i < N) {
            V[i * 2].r = composition(V[i].r, V[i * 2].r);
            V[i * 2 + 1].r = composition(V[i].r, V[i * 2 + 1].r);
        }
        V[i].r = id();
    }
public:

    lazy_segtree(int n) {
        N = 1; while (N < n) N *= 2;
        V.assign(N * 2, { e(),id() });
    }
    lazy_segtree(vector<S> A) {
        N = 1; while (N < A.size()) N *= 2;
        V.assign(N * 2, { e(),id() });
        rep(i, A.size()) V[N + i].v = A[i];
        for (int i = N - 1; i >= 1; i--)
            V[i].v = op(V[i * 2].v, V[i * 2 + 1].v);
    }

    void set(int p, S v) {
        p += N;
        for (int d = N; d >= 1; d /= 2) spread(p / d);
        V[p].v = v;
        int z = 1;
        while (p != 1) {
            p /= 2; z *= 2;
            spread(p * 2); spread(p * 2 + 1);
            V[p].v = op(V[p * 2].v, V[p * 2 + 1].v);
        }
    }
    S get(int p) {
        p += N;
        for (int d = N; d >= 1; d /= 2) spread(p / d);
        return V[p].v;
    }

    void apply(int l, int r, F v, int a = 0, int b = 0, int i = -1) {
        if (i == -1) { a = 0; b = N; i = 1; }
        if (r <= a || b <= l) { spread(i); return; }
        else if (l <= a && b <= r) { V[i].r = composition(v, V[i].r); spread(i); return; }
        spread(i);
        apply(l, r, v, a, (a + b) / 2, i * 2);
        apply(l, r, v, (a + b) / 2, b, i * 2 + 1);
        V[i].v = op(V[i * 2].v, V[i * 2 + 1].v);
    }

    S prod(int l, int r, int a = 0, int b = 0, int i = -1) {
        if (i == -1) { a = 0; b = N; i = 1; }
        if (r <= a || b <= l) return e();
        spread(i);
        if (l <= a && b <= r) return V[i].v;
        S q1 = prod(l, r, a, (a + b) / 2, i * 2);
        S q2 = prod(l, r, (a + b) / 2, b, i * 2 + 1);
        q1 = op(q1, q2);
        return q1;
    }
};

const LL INF = 1000000000000000000;
struct S { LL x; };
S op(S a, S b) { return { min(a.x,b.x) }; }
S e() { return { INF }; }
struct F { LL x; };
S mapping(F a, S b) { b.x += a.x; return b; }
F composition(F a, F b) { b.x += a.x; return b; }
F id() { return { 0 }; }

using RQ = lazy_segtree<S, op, e, F, mapping, composition, id>;


int main() {
    int N; cin >> N;
    vector<S> A(N); rep(i, N) { cin >> A[i].x; }
    RQ G(A);

    int Q; cin >> Q;

    while (Q--) {
        int c; cin >> c;
        if (c == 1) {
            int l, r, x; cin >> l >> r >> x; l--;
            G.apply(l, r, { x });
        }
        if (c == 2) {
            int l, r, x; cin >> l >> r >> x; l--;
            cout << G.prod(l, r).x << endl;
        }
    }

    return 0;
}
0