結果

問題 No.877 Range ReLU Query
ユーザー t33ft33f
提出日時 2019-09-12 19:16:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 2,029 bytes
コンパイル時間 1,079 ms
コンパイル使用メモリ 85,148 KB
実行使用メモリ 11,340 KB
最終ジャッジ日時 2024-04-25 22:15:49
合計ジャッジ時間 5,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 312 ms
11,132 KB
testcase_12 AC 277 ms
10,812 KB
testcase_13 AC 223 ms
7,884 KB
testcase_14 AC 232 ms
8,276 KB
testcase_15 AC 321 ms
11,340 KB
testcase_16 AC 304 ms
11,012 KB
testcase_17 AC 312 ms
11,200 KB
testcase_18 AC 309 ms
11,236 KB
testcase_19 AC 299 ms
11,276 KB
testcase_20 AC 298 ms
11,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

class SegTree {
    using T = pair<long long, int>;
    static const T op(const T& lhs, const T& rhs) { // binary operator
        return {lhs.first + rhs.first, lhs.second + rhs.second};
    }
    constexpr static T unit = { 0, 0 }; // unit element
    const int N;
    T *data;
    static int calc_size(int sz) {
        int n = 1;
        while (sz > n) n *= 2;
        return n;
    }
public:
    explicit SegTree(int sz) : N(calc_size(sz)) {
        data = new T[2*N-1];
    }
    ~SegTree() { delete[] data; }
    T query(int a, int b, int i = -1, int l = -1, int r = -1) const {
        if (i == -1) { i = 0; l = 0; r = N; }
        // if (r <= a || b <= l) return unit; // need this if a query can be empty
        if (a <= l && r <= b) return data[i];
        int m = (l+r)/2;
        if (b <= m) return query(a, b, 2*i+1, l, m);
        if (m <= a) return query(a, b, 2*i+2, m, r);
        T v1 = query(a, b, 2*i+1, l, m),
          v2 = query(a, b, 2*i+2, m, r);
        return op(v1, v2);
    }
    void update(int i, T v) {
        int x = i + N - 1;
        data[x] = v;
        while (x > 0) {
            x = (x-1)/2;
            data[x] = op(data[2*x+1], data[2*x+2]);
        }
    }
};

struct query { int id, l, r, x; };

int main() {
    vector<query> qs;
    int n, q; cin >> n >> q;
    for (int i = 0; i < n; i++) {
        int a; cin >> a;
        qs.push_back({-1, i, 0, a});
    }
    for (int i = 0; i < q; i++) {
        int j, l, r, x; cin >> j >> l >> r >> x;
        qs.push_back({i, l, r, x});
    }
    sort(qs.begin(), qs.end(), [](query &lhs, query &rhs) {
            return lhs.x > rhs.x;
        });
    SegTree st(n);
    vector<long long> ans(q);
    for (query &q : qs) {
        if (q.id == -1) st.update(q.l, {q.x, 1});
        else {
            auto x = st.query(q.l-1, q.r);
            ans[q.id] = x.first - (long long)x.second * q.x;
        }
    }
    for (long long a : ans) cout << a << endl;
}
0