結果

問題 No.877 Range ReLU Query
ユーザー ferinferin
提出日時 2019-09-06 21:59:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,745 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 176,880 KB
実行使用メモリ 63,340 KB
最終ジャッジ日時 2023-09-06 23:50:40
合計ジャッジ時間 7,904 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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

    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, x);
        // cout << "ret=" << ret << " val=" << val << endl;
        ret -= val.second;
        ret -= (r-l+1-val.first) * x;
        cout << ret << endl;
    }

    return 0;
}
0