結果

問題 No.877 Range ReLU Query
ユーザー ferinferin
提出日時 2019-09-06 22:05:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 615 ms / 2,000 ms
コード長 4,069 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 175,848 KB
実行使用メモリ 63,384 KB
最終ジャッジ日時 2024-04-25 22:00:30
合計ジャッジ時間 8,527 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 595 ms
52,356 KB
testcase_12 AC 518 ms
51,340 KB
testcase_13 AC 403 ms
38,332 KB
testcase_14 AC 415 ms
35,324 KB
testcase_15 AC 615 ms
62,104 KB
testcase_16 AC 605 ms
60,672 KB
testcase_17 AC 614 ms
61,868 KB
testcase_18 AC 593 ms
62,176 KB
testcase_19 AC 317 ms
63,384 KB
testcase_20 AC 474 ms
63,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

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); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

struct segTreeRangeFreq {
    int n;
    vector<vector<ll>> dat;
    vector<vector<ll>> rui;
    // 初期化 O(NlogN)
    segTreeRangeFreq() {}
    segTreeRangeFreq(vector<vector<ll>> v) {
        n = 1; while(n < (ll)v.size()) n *= 2;
        dat.resize(2*n-1);
        rui.resize(2*n-1);
        REP(i, v.size()) {
            dat[i+n-1] = rui[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; --i) {
            dat[i].resize(dat[i*2+1].size() + dat[i*2+2].size());
            merge(ALL(dat[i*2+1]), ALL(dat[i*2+2]), dat[i].begin());
            if(dat[i].size() == 0) continue;
            rui[i].resize(dat[i].size());
            rui[i][0] = dat[i][0];
            FOR(j, 1, rui[i].size()) {
                rui[i][j] = rui[i][j-1] + dat[i][j];
            }
        }
    }
    // [a, b) のx以下の要素の和を返す O(log^2N)
    PII query(int a, int b, ll x, int k, int l, int r) {
        if(b <= l || r <= a) return PII(0, 0);
        if(a <= l && r <= b) {
            ll itr = upper_bound(ALL(dat[k]), x) - dat[k].begin();
            return PII(itr, itr-1<0?0:rui[k][itr-1]);
        }
        PII vl = query(a, b, x, k*2+1, l, (l+r)/2);
        PII vr = query(a, b, x, k*2+2, (l+r)/2, r);
        return PII({vl.first+vr.first, vl.second+vr.second});
    }
    PII query(int a, int b, ll x) { return query(a, b, x, 0, 0, n); }
};

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    vector<vector<ll>> v(n, vector<ll>(1));
    REP(i, n) v[i][0] = a[i];

    // cout << v << endl;

    segTreeRangeFreq seg(v);

    // cout << "---------------------" << endl;
    // int cnt = 0;
    // for(int i=1; i<=seg.n; i*=2) {
    //     REP(j, i) {
    //         cout << "(" << seg.dat[cnt] << "," << seg.rui[cnt] << ") ";
    //         cnt++;
    //     }
    //     cout << endl;
    // }
    // cout << "---------------------" << endl;

    FOR(i, 1, n) a[i] += a[i-1];
    while(q--) {
        ll t, l, r, x;
        cin >> t >> l >> r >> x;
        l--, r--;

        ll ret = a[r] - (l==0?0:a[l-1]);
        PII val = seg.query(l, r+1, x);
        // cout << "ret=" << ret << " val=" << val << endl;
        ret -= val.second;
        ret -= (r-l+1-val.first) * x;
        cout << ret << endl;
    }

    return 0;
}
0