結果

問題 No.1234 典型RMQ
ユーザー ぷらぷら
提出日時 2021-03-28 20:41:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 170,496 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-26 11:51:18
合計ジャッジ時間 9,770 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 312 ms
8,064 KB
testcase_07 AC 241 ms
5,376 KB
testcase_08 AC 336 ms
8,192 KB
testcase_09 AC 288 ms
5,760 KB
testcase_10 AC 329 ms
7,936 KB
testcase_11 AC 310 ms
7,936 KB
testcase_12 AC 281 ms
5,888 KB
testcase_13 AC 248 ms
5,376 KB
testcase_14 AC 278 ms
5,760 KB
testcase_15 AC 274 ms
5,632 KB
testcase_16 AC 326 ms
7,936 KB
testcase_17 AC 285 ms
5,760 KB
testcase_18 AC 227 ms
5,376 KB
testcase_19 AC 331 ms
8,064 KB
testcase_20 AC 249 ms
8,192 KB
testcase_21 AC 315 ms
7,936 KB
testcase_22 AC 269 ms
8,192 KB
testcase_23 AC 270 ms
8,192 KB
testcase_24 AC 267 ms
8,064 KB
testcase_25 AC 268 ms
8,192 KB
testcase_26 AC 267 ms
8,064 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;

long long inf = 1001001001001001001;

template <typename T>
struct LazySegmentTree{
    int n;
    vector<T> dat;
    vector<T>lazy;
    LazySegmentTree() {}
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2-1,0);
        lazy.resize(n*2-1,0);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == 0) return;
        if(k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+2] += lazy[k];
        }
        dat[k] += lazy[k];
        lazy[k] = 0;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2+1, l, (l+r)/2);
            update(a, b, x, k*2+2, (l+r)/2, r);
            dat[k] = min(dat[k*2+1],dat[k*2+2]);
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return inf;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k*2+1, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+2, (l+r)/2, r);
            return min(vl,vr);
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 0, 0, n);
    }
};

int main() {
    int N;
    cin >> N;
    vector<long long>a(N);
    LazySegmentTree<long long>tree(N);
    for(int i = 0; i < N; i++) {
        cin >> a[i];
        tree.update(i,i+1,a[i]);
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int k,l,r;
        long long c;
        cin >> k >> l >> r >> c;
        if(k == 1) {
            tree.update(l-1,r,c);
        }
        else {
            cout << tree.query(l-1,r) << endl;
        }
    }
}
0