結果

問題 No.877 Range ReLU Query
ユーザー rokahikou1rokahikou1
提出日時 2021-02-16 21:56:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 4,788 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 217,788 KB
実行使用メモリ 15,492 KB
最終ジャッジ日時 2024-04-25 22:28:40
合計ジャッジ時間 6,653 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,948 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 251 ms
14,940 KB
testcase_12 AC 219 ms
13,800 KB
testcase_13 AC 174 ms
9,476 KB
testcase_14 AC 187 ms
11,448 KB
testcase_15 AC 258 ms
14,796 KB
testcase_16 AC 245 ms
15,204 KB
testcase_17 AC 250 ms
13,972 KB
testcase_18 AC 255 ms
15,492 KB
testcase_19 AC 246 ms
14,620 KB
testcase_20 AC 261 ms
14,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define ALL(v) (v).begin(), (v).end()
#define LLA(v) (v).rbegin(), (v).rend()
#define SZ(v) (v).size()
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    read(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    read(__VA_ARGS__)
#define DOUBLE(...)                                                            \
    double __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    read(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define VEC(type, name, size)                                                  \
    vector<type> name(size);                                                   \
    read(name)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
template <class T> inline vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> inline auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}

void read() {}
template <class T> inline void read(T &a) { cin >> a; }
template <class T, class S> inline void read(pair<T, S> &p) {
    read(p.first), read(p.second);
}
template <class T> inline void read(vector<T> &v) {
    for(auto &a : v)
        read(a);
}
template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
    read(head), read(tail...);
}
template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); }
template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); }

/* SegmentTree(0-indexed) */
template <class Monoid> struct SegmentTree {
    using F = function<Monoid(Monoid, Monoid)>;
    const F f;
    const Monoid e;
    int n;

    vector<Monoid> dat;
    SegmentTree(int _n, const F _f, const Monoid &_e) : f(_f), e(_e) {
        n = 1;
        while(n < _n)
            n *= 2;
        dat.assign(2 * n, e);
    }

    SegmentTree(vector<Monoid> v, const F _f, const Monoid &_e) : f(_f), e(_e) {
        int sz = v.size();
        n = 1;
        while(n < sz)
            n *= 2;
        dat.resize(2 * n);
        for(int i = 0; i < sz; i++)
            set(i, v[i]);
        build();
    }

    void set(int k, Monoid a) { dat[k + n] = a; }

    void build() {
        for(int i = n - 1; i > 0; i--) {
            dat[i] = f(dat[2 * i], dat[2 * i + 1]);
        }
    }

    void update(int k, const Monoid &a) {
        k += n;
        dat[k] = a;
        while(k >>= 1) {
            dat[k] = f(dat[k * 2], dat[k * 2 + 1]);
        }
    }

    Monoid at(int a) { return query(a, a + 1); }

    // query for [a,b)
    Monoid query(int a, int b) {
        Monoid vl = e, vr = e;
        a += n, b += n;
        for(; a < b; a >>= 1, b >>= 1) {
            if(a & 1)
                vl = f(vl, dat[a++]);
            if(b & 1)
                vr = f(dat[--b], vr);
        }
        return f(vl, vr);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    INT(n, q);
    VEC(ll, a, n);
    using S = pll;
    auto op = [](S a, S b) -> S {
        return S{a.first + b.first, a.second + b.second};
    };
    S e = {0, 0};
    SegmentTree<S> segtree(n, op, e);
    using A = tuple<ll, int, int>; // [value,query,index]
    vector<A> vec;
    rep(i, n) { vec.emplace_back(a[i], 0, i); }
    using Q = tuple<int, int, ll>; // [l,r,value]
    vector<Q> query(q);
    rep(i, q) {
        LL(_, l, r, x);
        l--, r--;
        query[i] = {l, r, x};
        vec.emplace_back(x, 1, i);
    }
    sort(ALL(vec), greater{});
    vector<ll> res(q);
    for(auto [val, qq, idx] : vec) {
        if(qq == 0) {
            segtree.update(idx, S{val, 1});
        } else {
            auto [l, r, x] = query[idx];
            auto [v, num] = segtree.query(l, r + 1);
            res[idx] = v - x * num;
        }
    }
    rep(i, q) { cout << res[i] << endl; }
}
0