結果

問題 No.1234 典型RMQ
ユーザー koi_kotyakoi_kotya
提出日時 2020-09-18 21:53:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 2,334 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 172,264 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-26 11:07:16
合計ジャッジ時間 10,241 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_06 AC 350 ms
8,704 KB
testcase_07 AC 265 ms
5,376 KB
testcase_08 AC 353 ms
8,960 KB
testcase_09 AC 306 ms
6,144 KB
testcase_10 AC 347 ms
8,576 KB
testcase_11 AC 332 ms
8,320 KB
testcase_12 AC 305 ms
5,888 KB
testcase_13 AC 266 ms
5,376 KB
testcase_14 AC 304 ms
6,016 KB
testcase_15 AC 302 ms
5,760 KB
testcase_16 AC 344 ms
8,576 KB
testcase_17 AC 304 ms
6,016 KB
testcase_18 AC 246 ms
5,376 KB
testcase_19 AC 346 ms
8,704 KB
testcase_20 AC 238 ms
8,960 KB
testcase_21 AC 325 ms
8,448 KB
testcase_22 AC 261 ms
8,832 KB
testcase_23 AC 263 ms
8,832 KB
testcase_24 AC 263 ms
8,832 KB
testcase_25 AC 263 ms
8,832 KB
testcase_26 AC 265 ms
8,960 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;

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