結果

問題 No.877 Range ReLU Query
ユーザー betrue12betrue12
提出日時 2019-09-06 22:18:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 506 ms / 2,000 ms
コード長 1,926 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 184,616 KB
実行使用メモリ 20,048 KB
最終ジャッジ日時 2024-04-25 22:02:39
合計ジャッジ時間 8,158 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 4 ms
6,812 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 486 ms
18,764 KB
testcase_12 AC 414 ms
17,224 KB
testcase_13 AC 332 ms
13,012 KB
testcase_14 AC 348 ms
15,084 KB
testcase_15 AC 506 ms
19,272 KB
testcase_16 AC 477 ms
19,344 KB
testcase_17 AC 486 ms
19,280 KB
testcase_18 AC 471 ms
19,420 KB
testcase_19 AC 413 ms
19,536 KB
testcase_20 AC 468 ms
20,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, Q;
    cin >> N >> Q;
    vector<vector<int>> ev;
    for(int i=0; i<N; i++){
        int a;
        cin >> a;
        ev.push_back({a, 0, i});
    }
    for(int i=0; i<Q; i++){
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        ev.push_back({x, 1, l-1, r-1, i});
    }
    sort(ev.rbegin(), ev.rend());
    vector<int64_t> ans(Q);

    Segtree<int64_t> stval(N, [](int64_t a, int64_t b){ return a+b; }, 0);
    Segtree<int64_t> stnum(N, [](int64_t a, int64_t b){ return a+b; }, 0);
    for(auto& v : ev){
        if(v[1]){
            int x = v[0], l = v[2], r = v[3], i = v[4];
             ans[i] = stval.between(l, r) - x * stnum.between(l, r);
        }else{
            int a = v[0], i = v[2];
            stval.update(i, a);
            stnum.update(i, 1);
        }
    }
    for(int64_t a : ans) cout << a << endl;
    return 0;
}
0