結果

問題 No.877 Range ReLU Query
ユーザー betrue12betrue12
提出日時 2019-09-06 22:18:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 1,926 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 185,004 KB
実行使用メモリ 19,464 KB
最終ジャッジ日時 2023-08-08 04:19:48
合計ジャッジ時間 8,217 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 426 ms
17,472 KB
testcase_12 AC 366 ms
17,768 KB
testcase_13 AC 290 ms
12,824 KB
testcase_14 AC 312 ms
13,608 KB
testcase_15 AC 438 ms
19,464 KB
testcase_16 AC 422 ms
19,336 KB
testcase_17 AC 439 ms
19,192 KB
testcase_18 AC 428 ms
19,104 KB
testcase_19 AC 375 ms
18,912 KB
testcase_20 AC 433 ms
18,912 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