結果

問題 No.1234 典型RMQ
ユーザー koi_kotyakoi_kotya
提出日時 2020-09-18 21:53:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 2,000 ms
コード長 2,334 bytes
コンパイル時間 1,820 ms
コンパイル使用メモリ 172,816 KB
実行使用メモリ 8,720 KB
最終ジャッジ日時 2023-08-08 18:39:15
合計ジャッジ時間 9,725 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 315 ms
8,308 KB
testcase_07 AC 256 ms
4,384 KB
testcase_08 AC 329 ms
8,720 KB
testcase_09 AC 296 ms
6,204 KB
testcase_10 AC 327 ms
8,400 KB
testcase_11 AC 307 ms
8,016 KB
testcase_12 AC 288 ms
5,764 KB
testcase_13 AC 260 ms
4,380 KB
testcase_14 AC 292 ms
5,752 KB
testcase_15 AC 278 ms
5,800 KB
testcase_16 AC 318 ms
8,248 KB
testcase_17 AC 286 ms
5,612 KB
testcase_18 AC 240 ms
4,380 KB
testcase_19 AC 329 ms
8,672 KB
testcase_20 AC 222 ms
8,656 KB
testcase_21 AC 311 ms
8,420 KB
testcase_22 AC 251 ms
8,660 KB
testcase_23 AC 249 ms
8,544 KB
testcase_24 AC 253 ms
8,544 KB
testcase_25 AC 251 ms
8,584 KB
testcase_26 AC 250 ms
8,544 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';

struct LazySegmentTree {
private:
    int n;
    vector<ll> node,lazy;
public:
    LazySegmentTree(vector<ll> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1,0);
        for (int i = 0;i < sz;++i) node[i+n-1] = v[i];
        for (int i = n-2;i >= 0;--i) node[i] = min(node[i*2+1],node[i*2+2]);
    }

    void eval(int k,int l,int r) {
        if (lazy[k] != 0) {
            node[k] += lazy[k]/(r-l);
            if (r-l > 1) {
                lazy[2*k+1] += lazy[k]/2;
                lazy[2*k+2] += lazy[k]/2;
            }
            lazy[k] = 0;
        }
    }

    void add(int a,int b,ll x,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        eval(k,l,r);
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            lazy[k] += (r-l)*x;
            eval(k,l,r);
        } else {
            add(a,b,x,2*k+1,l,(l+r)/2);
            add(a,b,x,2*k+2,(l+r)/2,r);
            node[k] = min(node[2*k+1],node[2*k+2]);
        }
    }

    ll getmin(int a,int b,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (b <= l || r <= a) return INT64_MAX;
        eval(k,l,r);
        if (a <= l && r <= b) return node[k];
        ll vl = getmin(a,b,k*2+1,l,(l+r)/2);
        ll vr = getmin(a,b,2*k+2,(l+r)/2,r);
        return min(vl,vr);
    }
};

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0;i < n;++i) cin >> a[i];
    LazySegmentTree seg(a);
    int q;
    cin >> q;
    while (q--) {
        int k,l,r;ll c;
        cin >> k >> l >> r >> c;
        if (k == 1) seg.add(l-1,r,c);
        else cout << seg.getmin(l-1,r) << newl;
    }
}
0