結果

問題 No.877 Range ReLU Query
ユーザー TAISA_TAISA_
提出日時 2019-09-06 22:40:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 3,070 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 182,656 KB
実行使用メモリ 15,388 KB
最終ジャッジ日時 2024-04-25 22:04:32
合計ジャッジ時間 5,999 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 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 267 ms
13,448 KB
testcase_12 AC 232 ms
13,652 KB
testcase_13 AC 182 ms
11,188 KB
testcase_14 AC 193 ms
11,544 KB
testcase_15 AC 263 ms
14,172 KB
testcase_16 AC 252 ms
14,112 KB
testcase_17 AC 250 ms
14,288 KB
testcase_18 AC 251 ms
13,936 KB
testcase_19 AC 221 ms
15,264 KB
testcase_20 AC 243 ms
15,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
    for (int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
    for (auto &e : t) {
        fill_v(e, v);
    }
};

struct Segtree {
    int n;
    vector<ll> dat;
    Segtree(int n_) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n, 0);
    }
    void upd(int k, int x) {
        k += n;
        dat[k] = x;
        k >>= 1;
        while (k > 0) {
            dat[k] = dat[k << 1] + dat[k << 1 | 1];
            k >>= 1;
        }
    }
    ll get(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a) {
            return 0;
        }
        if (a <= l && r <= b) {
            return dat[k];
        }
        return get(a, b, 2 * k, l, (l + r) / 2) +
               get(a, b, 2 * k + 1, (l + r) / 2, r);
    }
    ll get(int a, int b) {
        if (a >= b) {
            return 0;
        }
        return get(a, b, 1, 0, n);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    Segtree seg(n + 1), seg2(n + 1);
    vector<ll> a(n);
    vector<P> v;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        seg.upd(i, a[i]);
        v.push_back(P(a[i], i));
    }
    sort(all(v));
    struct st {
        ll x, l, r, id;
        bool operator<(const st &to) { return x < to.x; }
    };
    vector<st> qs;
    for (int i = 0; i < q; i++) {
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        --l;
        qs.push_back({x, l, r, i});
    }
    sort(all(qs));
    int id = 0;
    vector<ll> res(q);
    for (int i = 0; i < q; i++) {
        ll x = qs[i].x, l = qs[i].l, r = qs[i].r;
        while (id < n && v[id].first < x) {
            seg.upd(v[id].second, 0);
            seg2.upd(v[id].second, 1);
            id++;
        }
        res[qs[i].id] = seg.get(l, r);
        ll co = seg2.get(l, r);
        res[qs[i].id] -= (r - l - co) * x;
    }
    for (int i = 0; i < q; i++) {
        cout << res[i] << endl;
    }
}
0