結果

問題 No.877 Range ReLU Query
ユーザー finefine
提出日時 2019-10-23 00:32:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 3,994 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 182,232 KB
実行使用メモリ 13,412 KB
最終ジャッジ日時 2024-04-25 22:17:26
合計ジャッジ時間 4,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 108 ms
12,536 KB
testcase_12 AC 94 ms
12,160 KB
testcase_13 AC 76 ms
9,396 KB
testcase_14 AC 78 ms
9,296 KB
testcase_15 AC 113 ms
13,284 KB
testcase_16 AC 108 ms
12,984 KB
testcase_17 AC 109 ms
13,116 KB
testcase_18 AC 108 ms
13,004 KB
testcase_19 AC 97 ms
13,260 KB
testcase_20 AC 106 ms
13,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

template <class Monoid>
struct SegmentTree {
    using T = typename Monoid::T;

    int n;
    vector<T> data;

    SegmentTree(int size, T initial_value = Monoid::identity()) {
        n = 1;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, initial_value);
    }

    SegmentTree(const vector<T>& v, T initial_value = Monoid::identity()) {
        int size = v.size();
        n = 1;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, initial_value);

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = Monoid::merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        Monoid::update(data[k], x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = Monoid::merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return Monoid::identity();
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return Monoid::merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = Monoid::identity(), vr = Monoid::identity();
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = Monoid::merge(vl, data[l++ - 1]);
            if (r & 1) vr = Monoid::merge(data[--r - 1], vr);
        }
        return Monoid::merge(vl, vr);
    }
};

// 以下、Monoidの例
template <class U = ll>
struct RangeMax {
    using T = U;
    static T merge(T x, T y) { return max(x, y); }
    static void update(T& target, T x) { target = x; }
    static constexpr T identity() { return T(0); }
};

template <class U = ll>
struct RangeSum {
    using T = U;
    static T merge(T x, T y) { return x + y; }
    static void update(T& target, T x) { target += x; }
    static constexpr T identity() { return T(0); }
};

template <class U = P>
struct RangeReLU {
    using T = U;
    static T merge(T x, T y) { return T(x.first + y.first, x.second + y.second); }
    static void update(T& target, T x) { target = x; }
    static constexpr T identity() { return T(0, 0); }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<P> v(n);
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        v.emplace_back(a, i);
    }
    sort(v.begin(), v.end(), greater<>());

    vector<int> l(q), r(q), x(q);
    vector<int> ids;
    for (int i = 0; i < q; i++) {
        int c;
        cin >> c >> l[i] >> r[i] >> x[i];
        l[i]--;
        ids.push_back(i);
    }

    sort(ids.begin(), ids.end(), [&](const int i1, const int i2){
        return x[i1] > x[i2];
    });

    vector<ll> ans(q, 0);
    SegmentTree< RangeReLU<> > st(n);
    int cur = 0;
    for (int i = 0; i < q; i++) {
        int ii = ids[i];
        while (cur < n && v[cur].first >= x[ii]) {
            st.update(v[cur].second, P(v[cur].first, 1));
            cur++;
        }

        P tar = st.query_fast(l[ii], r[ii]);
        ans[ii] = tar.first - tar.second * x[ii];
    }

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