結果

問題 No.1234 典型RMQ
ユーザー satashunsatashun
提出日時 2020-09-18 21:27:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 4,634 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 202,848 KB
実行使用メモリ 8,092 KB
最終ジャッジ日時 2023-08-08 18:32:31
合計ジャッジ時間 8,994 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 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 236 ms
7,748 KB
testcase_07 AC 193 ms
4,380 KB
testcase_08 AC 247 ms
7,748 KB
testcase_09 AC 214 ms
5,376 KB
testcase_10 AC 241 ms
7,756 KB
testcase_11 AC 226 ms
7,404 KB
testcase_12 AC 219 ms
5,368 KB
testcase_13 AC 195 ms
4,376 KB
testcase_14 AC 214 ms
5,572 KB
testcase_15 AC 212 ms
5,548 KB
testcase_16 AC 243 ms
7,740 KB
testcase_17 AC 215 ms
5,436 KB
testcase_18 AC 181 ms
4,380 KB
testcase_19 AC 235 ms
7,760 KB
testcase_20 AC 190 ms
8,092 KB
testcase_21 AC 232 ms
7,764 KB
testcase_22 AC 212 ms
8,084 KB
testcase_23 AC 214 ms
7,760 KB
testcase_24 AC 211 ms
7,788 KB
testcase_25 AC 215 ms
7,728 KB
testcase_26 AC 203 ms
8,088 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

// index of root = 1
// T1 : array value type
// T2 : action type

template <class U>
struct segtree {
    using T1 = typename U::T1;
    using T2 = typename U::T2;
    int sz, H;

    V<T1> a;
    V<T2> b;

    segtree() { sz = H = -1; }
    segtree(int n) {
        sz = 1, H = 0;
        while (sz < n) {
            sz *= 2, ++H;
        }
        a.assign(sz * 2, U::id1());
        b.assign(sz * 2, U::id2());
    }

    segtree(const V<T1>& vec) {
        int n = vec.size();
        sz = 1, H = 0;
        while (sz < n) {
            sz *= 2, ++H;
        }

        a.assign(sz * 2, U::id1());
        b.assign(sz * 2, U::id2());

        for (int i = 0; i < n; ++i) {
            a[sz + i] = vec[i];
        }
        for (int i = sz - 1; i >= 1; --i) {
            a[i] = U::op11(a[i << 1 | 0], a[i << 1 | 1]);
        }
    }

    void up(int i) {
        while (i >>= 1) {
            T1 xl = U::op21(b[i << 1 | 0], a[i << 1 | 0]);
            T1 xr = U::op21(b[i << 1 | 1], a[i << 1 | 1]);
            a[i] = U::op11(xl, xr);
        }
    }

    void propagate(int p) {
        for (int h = H; h > 0; --h) {
            int i = p >> h;
            a[i] = U::op21(b[i], a[i]);
            b[i << 1 | 0] = U::op22(b[i], b[i << 1 | 0]);
            b[i << 1 | 1] = U::op22(b[i], b[i << 1 | 1]);
            b[i] = U::id2();
        }
    }

    // action on [p, q)
    void apply(int p, int q, T2 x) {
        if (p == q) return;
        p += sz, q += sz;
        propagate(p);
        propagate(q - 1);

        for (int l = p, r = q; l < r; l >>= 1, r >>= 1) {
            if (l & 1) b[l] = U::op22(x, b[l]), ++l;
            if (r & 1) --r, b[r] = U::op22(x, b[r]);
        }
        up(p);
        up(q - 1);
    }

    T1 get(int l, int r) {
        if (l == r) return U::id1();
        l += sz, r += sz;
        propagate(l);
        propagate(r - 1);

        T1 lval = U::id1(), rval = U::id1();

        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) lval = U::op11(lval, U::op21(b[l], a[l])), ++l;
            if (r & 1) --r, rval = U::op11(U::op21(b[r], a[r]), rval);
        }
        return U::op11(lval, rval);
    }
};

// for https://atcoder.jp/contests/tenka1-2016-qualb/tasks/tenka1_2016_qualB_d
// range addition and range minimum query needed, initialization must be cared
// (set all to 0, while id1 = INF)

// modify only U for use

constexpr ll INF = TEN(18);

struct U {
    using T1 = ll;
    using T2 = ll;
    static T1 id1() { return INF; }
    static T2 id2() { return 0; }
    static T1 op11(const T1& a, const T1& b) { return min(a, b); }
    static T1 op21(const T2& b, const T1& a) { return b + a; }
    static T2 op22(const T2& a, const T2& b) { return a + b; }
};

int main() {
    int N;
    cin >> N;
    V<ll> A(N);
    rep(i, N) cin >> A[i];
    segtree<U> seg(A);
    int Q;
    cin >> Q;
    while (Q--) {
        int k, l, r, c;
        cin >> k >> l >> r >> c;
        --l;
        if (k == 1) {
            seg.apply(l, r, c);
        } else {
            auto a = seg.get(l, r);
            cout << a << '\n';
        }
    }
    return 0;
}
0