結果

問題 No.877 Range ReLU Query
ユーザー kyunakyuna
提出日時 2019-10-06 21:03:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 402 ms / 2,000 ms
コード長 2,144 bytes
コンパイル時間 1,263 ms
コンパイル使用メモリ 85,976 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-25 22:16:55
合計ジャッジ時間 6,172 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 4 ms
6,944 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,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 390 ms
10,368 KB
testcase_12 AC 338 ms
10,112 KB
testcase_13 AC 269 ms
7,680 KB
testcase_14 AC 281 ms
7,552 KB
testcase_15 AC 402 ms
10,624 KB
testcase_16 AC 379 ms
10,368 KB
testcase_17 AC 390 ms
10,240 KB
testcase_18 AC 382 ms
10,240 KB
testcase_19 AC 353 ms
10,752 KB
testcase_20 AC 374 ms
10,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename Monoid>
struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid M1;
    int sz;
    vector<Monoid> dat;
    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1), sz(1) {
        while (sz < n) sz <<= 1;
        dat.assign(sz * 2, M1);
    }
    void set(int k, const Monoid &x) { dat[k + sz] = x; }
    void build() {
        for (int k = sz - 1; k > 0; k--) {
            dat[k] = f(dat[2 * k], dat[2 * k + 1]);
        }
    }
    void update(int k, const Monoid &x) {
        dat[k += sz] = x;
        while (k >>= 1) dat[k] = f(dat[2 * k], dat[2 * k + 1]);
    }
    Monoid get(int a, int b) {  // [a, b)
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = f(L, dat[a++]);
            if (b & 1) R = f(dat[--b], R);
        }
        return f(L, R);
    }
    Monoid operator[](const int &k) const { return dat[k + sz]; }
    friend ostream& operator<<(ostream& os, SegmentTree<Monoid> &seg) {
        for (int i = 0; i < seg.sz; i++) os << seg[i] << " ";
        return os;
    }
};

int main() {
    int n, q; cin >> n >> q;
    vector<int> a(n);
    for (int &ai: a) cin >> ai;
    vector<int> l(q), r(q), x(q);
    for (int i = 0; i < q; i++) {
        int _; cin >> _ >> l[i] >> r[i] >> x[i]; l[i]--;
    }
    vector<int> ord(n + q);
    iota(ord.begin(), ord.end(), 0);
    auto comp = [&](int s, int t) {
        #define g(i) (i < n ? a[i] : x[i - n])
        return g(s) > g(t);
    };
    sort(ord.begin(), ord.end(), comp);
    using ll = long long;
    auto f = [](ll a, ll b) { return a + b; };
    SegmentTree<ll> sum(n, f, 0), num(n, f, 0);
    vector<ll> ans(q);
    for (int i: ord) {
        if (i < n) {
            sum.update(i, a[i]);
            num.update(i, 1);
        } else {
            i -= n;
            ans[i] = sum.get(l[i], r[i]) - num.get(l[i], r[i]) * x[i];
        }
    }
    for (int i = 0; i < q; i++) cout << ans[i] << endl;
    return 0;
}
0