結果

問題 No.1234 典型RMQ
ユーザー milanis48663220milanis48663220
提出日時 2020-09-18 21:49:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 2,855 bytes
コンパイル時間 1,050 ms
コンパイル使用メモリ 94,388 KB
実行使用メモリ 8,932 KB
最終ジャッジ日時 2023-08-08 18:38:22
合計ジャッジ時間 7,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 230 ms
8,532 KB
testcase_07 AC 198 ms
4,376 KB
testcase_08 AC 246 ms
8,620 KB
testcase_09 AC 222 ms
5,916 KB
testcase_10 AC 233 ms
8,480 KB
testcase_11 AC 227 ms
8,288 KB
testcase_12 AC 218 ms
5,652 KB
testcase_13 AC 199 ms
4,380 KB
testcase_14 AC 221 ms
5,856 KB
testcase_15 AC 212 ms
5,716 KB
testcase_16 AC 234 ms
8,496 KB
testcase_17 AC 218 ms
6,024 KB
testcase_18 AC 187 ms
4,376 KB
testcase_19 AC 237 ms
8,568 KB
testcase_20 AC 159 ms
8,808 KB
testcase_21 AC 230 ms
8,228 KB
testcase_22 AC 186 ms
8,908 KB
testcase_23 AC 187 ms
8,756 KB
testcase_24 AC 187 ms
8,852 KB
testcase_25 AC 186 ms
8,932 KB
testcase_26 AC 188 ms
8,820 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

template <typename T>
struct LazySegmentTree {
    int n;
    T unit;
    vector<T> node, lazy;
    vector<bool> lazyFlag;

    T calc(T a, T b){
        T ans;
        ans = min(a, b); //適宜変える
        return ans;
    }

    T lazyEvaluateNode(T node, T lazy){
        return node+lazy; //適宜変える
    }

    T clearLazy(){
        return 0; //適宜変える
    }

    LazySegmentTree(vector<T> v, T UNIT) {
        int sz = (int)v.size();
        unit = UNIT;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1, false);

        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = calc(node[i*2+1], node[i*2+2]);
    }

    void lazyEvaluate(int k, int l, int r) {
        if(lazyFlag[k]) {
            node[k] = lazyEvaluateNode(node[k], lazy[k]);
            if(r - l > 1) {
                lazy[k*2+1] = lazyEvaluateNode(lazy[k*2+1], lazy[k]);
                lazy[k*2+2] = lazyEvaluateNode(lazy[k*2+2], lazy[k]);
                lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
            }
            lazyFlag[k] = false;
            lazy[k] = clearLazy();
        }
    }

    //[a, b)
    //区間[a, b]への更新に対してはupdate(a, b+1, x)と呼ぶ
    void update(int a, int b, T x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] += x;
            lazyFlag[k] = true;
            lazyEvaluate(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] = calc(node[2*k+1], node[2*k+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return unit;
        if(a <= l && r <= b) return node[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return calc(vl, vr);
    }
};

const ll INF = 1e17;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    LazySegmentTree<ll> sgt(a, INF);
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int k, l, r; ll c;
        cin >> k >> l >> r >> c; l--; r--;
        if(k == 1){
            sgt.update(l, r+1, c);
        }else{
            cout << sgt.query(l, r+1) << endl;
        }
    }
}
0