結果

問題 No.877 Range ReLU Query
ユーザー veqccveqcc
提出日時 2019-09-09 20:07:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 120,052 KB
実行使用メモリ 15,156 KB
最終ジャッジ日時 2024-04-25 22:14:37
合計ジャッジ時間 4,177 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 106 ms
14,272 KB
testcase_12 AC 97 ms
13,576 KB
testcase_13 AC 73 ms
10,368 KB
testcase_14 AC 77 ms
10,368 KB
testcase_15 AC 114 ms
14,952 KB
testcase_16 AC 111 ms
14,644 KB
testcase_17 AC 113 ms
14,744 KB
testcase_18 AC 116 ms
14,696 KB
testcase_19 AC 102 ms
15,112 KB
testcase_20 AC 116 ms
15,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

template <typename T>
class SegmentTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T t;
    vector <T> dat;
public:
    SegmentTree(F f, T t) : f(f), t(t) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, t);
    }
    void build(const vector <T> &v) {
        auto n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; i++) dat[n + i] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
    T query(int a, int b) {
        T vl = t, vr = t;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

typedef pair <ll, ll> P;

struct Query {
    int l, r;
    ll x;
};

struct Val {
    ll val;
    int type, idx;
    bool operator>(const Val &a) const {
        return val > a.val;
    }
};

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q, c;
    cin >> n >> q;

    vector <ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector <Query> Q(q);
    for (int i = 0; i < q; i++) cin >> c >> Q[i].l >> Q[i].r >> Q[i].x;

    vector <Val> v(n + q);
    for (int i = 0; i < n; i++) v[i] = {a[i], 0, i};
    for (int i = 0; i < q; i++) v[n + i] = {Q[i].x, 1, i};

    sort(v.begin(), v.end(), greater<Val>());

    vector <ll> ans(q);

    auto f = [](P a, P b) {
        return P(a.first + b.first, a.second + b.second);
    };

    SegmentTree<P> seg(f, P(0, 0));
    seg.build(vector<P>(n, P(0, 0)));

    for (int i = 0; i < n + q; i++) {
        if (v[i].type == 0) {
            seg.set_val(v[i].idx, {v[i].val, 1});
        } else {
            int idx = v[i].idx;
            P ret = seg.query(Q[idx].l - 1, Q[idx].r);
            ans[idx] = ret.first - v[i].val * ret.second;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }

    return 0;
}
0