結果

問題 No.877 Range ReLU Query
ユーザー t98slidert98slider
提出日時 2023-04-15 15:31:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,863 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 182,436 KB
実行使用メモリ 23,504 KB
最終ジャッジ日時 2024-04-19 09:41:40
合計ジャッジ時間 5,150 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 155 ms
21,152 KB
testcase_12 AC 131 ms
19,476 KB
testcase_13 AC 108 ms
16,364 KB
testcase_14 AC 114 ms
16,532 KB
testcase_15 AC 171 ms
23,020 KB
testcase_16 AC 164 ms
22,060 KB
testcase_17 AC 170 ms
22,556 KB
testcase_18 AC 167 ms
22,440 KB
testcase_19 AC 162 ms
23,504 KB
testcase_20 AC 167 ms
23,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> struct fenwick_tree {
    using U = T;

    public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q, l, r, x;
    cin >> N >> Q;
    vector<int> a(N), ca;
    vector<tuple<int,int,int>> query(Q);
    vector<ll> ans(Q);
    for(auto &&v : a) cin >> v;
    ca = a;
    ca.reserve(N + Q);
    for(int i = 0; i < Q; i++){
        cin >> l >> l >> r >> x;
        query[i] = make_tuple(--l, r, x);
        ca.emplace_back(x);
    }
    sort(ca.begin(), ca.end());
    ca.erase(unique(ca.begin(), ca.end()), ca.end());
    int m = ca.size();
    vector<vector<int>> tb(m), tb2(m);
    for(int i = 0; i < N; i++){
        tb[lower_bound(ca.begin(), ca.end(), a[i]) - ca.begin()].emplace_back(i);
    }
    for(int i = 0; i < Q; i++){
        tb2[lower_bound(ca.begin(), ca.end(), get<2>(query[i])) - ca.begin()].emplace_back(i);
    }
    fenwick_tree<ll> fw1(N), fw2(N);
    for(int i = m - 1; i >= 0; i--){
        for(auto &&p : tb[i]){
            fw1.add(p, a[p]);
            fw2.add(p, 1);
        }
        for(auto &&p : tb2[i]){
            tie(l, r, x) = query[p];
            ans[p] = fw1.sum(l, r) - fw2.sum(l, r) * x;
        }
    }
    for(auto &&v : ans) cout << v << '\n';
}
0