結果

問題 No.1234 典型RMQ
ユーザー tokusakuraitokusakurai
提出日時 2020-09-18 21:42:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 3,755 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 201,392 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-26 11:04:09
合計ジャッジ時間 9,458 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 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
7,936 KB
testcase_07 AC 233 ms
5,376 KB
testcase_08 AC 294 ms
8,064 KB
testcase_09 AC 256 ms
5,632 KB
testcase_10 AC 290 ms
7,936 KB
testcase_11 AC 278 ms
7,808 KB
testcase_12 AC 263 ms
5,632 KB
testcase_13 AC 232 ms
5,376 KB
testcase_14 AC 257 ms
5,888 KB
testcase_15 AC 244 ms
5,504 KB
testcase_16 AC 294 ms
8,192 KB
testcase_17 AC 262 ms
5,632 KB
testcase_18 AC 226 ms
5,376 KB
testcase_19 AC 295 ms
8,064 KB
testcase_20 AC 208 ms
8,064 KB
testcase_21 AC 282 ms
7,808 KB
testcase_22 AC 241 ms
8,320 KB
testcase_23 AC 235 ms
8,064 KB
testcase_24 AC 238 ms
8,064 KB
testcase_25 AC 241 ms
8,192 KB
testcase_26 AC 240 ms
8,064 KB
testcase_27 AC 0 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;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename Monoid, typename Operator_Monoid>
struct Starry_Sky_Tree{
    vector<Monoid> seg;
    vector<Operator_Monoid> lazy;
    const Monoid e1;
    const Operator_Monoid e2;
    const int n;

    Monoid f(const Monoid &a, const Monoid &b) const {return min(a, b);}

    Monoid g(const Monoid &a, const Operator_Monoid &b) const {return a+b;}

    Operator_Monoid h(const Operator_Monoid &a, const Operator_Monoid &b) const {return a+b;}
    
    Starry_Sky_Tree(int N, const Monoid &e1, const Operator_Monoid &e2)
        : e1(e1), e2(e2), n(1<<(32-__builtin_clz(N-1))){
        seg.assign(2*n, e1), lazy.assign(2*n, e2);
    }

    void set(int i, const Monoid &x) {seg[n+i] = x;}

    void build(){
        rep3(i, n-1, 1) seg[i] = f(seg[2*i], seg[2*i+1]);
    }

    void init(int N, const Monoid &x){
        rep(i, N) set(i, x);
        build();
    }
    
    void eval(int i, int l, int r){
        if(lazy[i] != e2){
            //seg[i] = g(seg[i], lazy[i]*(r-l));
            seg[i] = g(seg[i], lazy[i]);
            if(r-l > 1){
                lazy[2*i] = h(lazy[2*i] ,lazy[i]);
                lazy[2*i+1] = h(lazy[2*i+1], lazy[i]);
            }
            lazy[i] = e2;
        }
    }
    
    void add(int a, int b, const Operator_Monoid &x, int i, int l, int r){
        eval(i, l, r);
        if(a >= r || b <= l) return;
        if(a <= l && r <= b){
            lazy[i] = h(lazy[i], x);
            eval(i, l, r);
        }
        else{
            add(a, b, x, 2*i, l, (l+r)/2);
            add(a, b, x, 2*i+1, (l+r)/2, r);
            seg[i] = f(seg[2*i], seg[2*i+1]);
        }
    }

    void add(int a, int b, const Operator_Monoid &x) {add(a, b, x, 1, 0, n);}
    
    Monoid query(int a, int b, int i, int l, int r){
        eval(i, l, r);
        if(a >= r || b <= l) return e1;
        if(a <= l && r <= b) return seg[i];
        Monoid vl = query(a, b, 2*i, l, (l+r)/2);
        Monoid vr = query(a, b, 2*i+1, (l+r)/2, r);
        return f(vl, vr);
    }

    Monoid query(int a, int b) {return query(a, b, 1, 0, n);}
    
    void update(int i, int l, int r){
        seg[i] = g(seg[i], lazy[i]);
        if(r-l > 1){
            lazy[2*i] = h(lazy[2*i], lazy[i]);
            lazy[2*i+1] = h(lazy[2*i+1], lazy[i]);
            update(2*i, l, (l+r)/2);
            update(2*i+1, (l+r)/2, r);
        }
        lazy[i] = e2;
    }

    void update() {update(1, 0, n);}
    
    Monoid operator [] (int i) const {return seg[n+i];}
    
    void clear(){
        fill(all(seg), e1), fill(all(lazy), e2);
    }
};

int main(){
    int N;
    cin >> N;
    ll a[N];
    rep(i, N) cin >> a[i];
    Starry_Sky_Tree<ll, ll> seg(N, INF, 0);
    rep(i, N) seg.set(i, a[i]);
    seg.build();
    int Q;
    cin >> Q;
    while(Q--){
        int k, l, r; ll c; cin >> k >> l >> r >> c; l--;
        if(k == 1) seg.add(l, r, c);
        else cout << seg.query(l, r) << endl;
    }
}
0