結果

問題 No.1234 典型RMQ
ユーザー face4face4
提出日時 2020-09-19 17:58:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 2,852 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 79,624 KB
実行使用メモリ 8,876 KB
最終ジャッジ日時 2023-08-08 18:59:37
合計ジャッジ時間 8,152 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 273 ms
8,376 KB
testcase_07 AC 230 ms
4,376 KB
testcase_08 AC 284 ms
8,600 KB
testcase_09 AC 259 ms
6,080 KB
testcase_10 AC 280 ms
8,284 KB
testcase_11 AC 268 ms
8,076 KB
testcase_12 AC 253 ms
5,648 KB
testcase_13 AC 232 ms
4,376 KB
testcase_14 AC 255 ms
5,652 KB
testcase_15 AC 247 ms
5,944 KB
testcase_16 AC 278 ms
8,276 KB
testcase_17 AC 254 ms
5,636 KB
testcase_18 AC 218 ms
4,380 KB
testcase_19 AC 285 ms
8,548 KB
testcase_20 AC 213 ms
8,664 KB
testcase_21 AC 272 ms
8,240 KB
testcase_22 AC 233 ms
8,876 KB
testcase_23 AC 235 ms
8,552 KB
testcase_24 AC 235 ms
8,612 KB
testcase_25 AC 233 ms
8,588 KB
testcase_26 AC 235 ms
8,704 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<climits>
using namespace std;
typedef long long ll;

// F : T x T -> T
// G : T x E -> T
// H : E x E -> E
// R : E x int(length of the range) -> E
template<typename T, typename E, typename F, typename G, typename H, typename R>
struct SegL{
private:
    vector<T> node;
    vector<E> lazy;
    int n;
    T defVal;
    E defOp;
    F f;    // merge elements(T)
    G g;    // update element(T) 
    H h;    // merge operators(E)
    R r;    // operator depends on length

    void init(int siz, T defVal, E defOp){
        n = 1;
        while(n < siz)  n *= 2;
        node.resize(2*n-1, defVal);
        lazy.resize(2*n-1, defOp);
    }

public:
    SegL(int siz, T defVal, E defOp, F f, G g, H h, R r) : defVal(defVal), defOp(defOp), f(f), g(g), h(h), r(r){
        init(siz, defVal, defOp);
    }
    SegL(vector<T> v, T defVal, E defOp, F f, G g, H h, R r) : defVal(defVal), defOp(defOp), f(f), g(g), h(h), r(r){
        init(v.size(), defVal, defOp);
        for(int i = 0; i < v.size(); i++)   node[n-1+i] = v[i];
        for(int i = n-2; i >= 0; i--)   node[i] = f(node[2*i+1], node[2*i+2]);
    }

    void eval(int k, int len){
        if(lazy[k] == defOp)    return;
        if(k < n-1){
            lazy[2*k+1] = h(lazy[2*k+1], lazy[k]);
            lazy[2*k+2] = h(lazy[2*k+2], lazy[k]);
        }
        node[k] = g(node[k], r(lazy[k], len));
        lazy[k] = defOp;
    }

    void update(int a, int b, E x, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        eval(k, r-l);
        if(b <= l || r <= a){
            return;
        }else if(a <= l && r <= b){
            lazy[k] = h(lazy[k], x);
            eval(k, r-l);
        }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] = f(node[2*k+1], node[2*k+2]);
        }
    }

    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        eval(k, r-l);
        if(b <= l || r <= a)    return defVal;
        if(a <= l && r <= b)    return node[k];
        T lx = query(a, b, 2*k+1, l, (l+r)/2);
        T rx = query(a, b, 2*k+2, (l+r)/2, r);
        return f(lx, rx);
    }
};

int main(){
    int n;
    cin >> n;
    vector<ll> v(n);
    for(int i = 0; i < n; i++)  cin >> v[i];
    auto f = [](ll a, ll b)->ll{return min(a,b);};
    auto g = [](ll a, ll b)->ll{return a+b;};
    auto h = [](ll a, ll b)->ll{return a+b;};
    auto r = [](ll a, ll b)->ll{return a;};
    SegL<ll, ll, decltype(f), decltype(g), decltype(h), decltype(r)> seg(v, 1ll<<62, 0, f, g, h, r);
    int q;
    cin >> q;
    while(q--){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        b--;
        if(a == 1){
            seg.update(b, c, d);
        }else{
            cout << seg.query(b, c) << endl;
        }
    }
    return 0;
}
0