結果

問題 No.877 Range ReLU Query
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-09-10 10:40:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,786 ms / 2,000 ms
コード長 4,590 bytes
コンパイル時間 2,687 ms
コンパイル使用メモリ 220,880 KB
実行使用メモリ 135,740 KB
最終ジャッジ日時 2023-08-08 04:33:20
合計ジャッジ時間 19,537 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 4 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 6 ms
4,408 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1,630 ms
116,588 KB
testcase_12 AC 1,453 ms
115,060 KB
testcase_13 AC 1,096 ms
77,508 KB
testcase_14 AC 1,073 ms
72,148 KB
testcase_15 AC 1,770 ms
132,828 KB
testcase_16 AC 1,714 ms
130,832 KB
testcase_17 AC 1,786 ms
132,540 KB
testcase_18 AC 1,746 ms
133,176 KB
testcase_19 AC 826 ms
135,740 KB
testcase_20 AC 1,341 ms
135,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
// StaticHealthy2DSegtree
#define def 0
using V = ll;
V comp(V& l, V& r) { return l + r; };
struct SegTree { //[l,r)
    int NV;
    vector<V> val;
    void init(int n) {
        NV = 1;
        while (NV < n) NV *= 2;
        val = vector<V>(NV * 2, def);
    }
    V get(int x, int y, int l, int r, int k) {
        if (r <= x || y <= l) return def; if (x <= l&&r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b);
    }
    V get(int x, int y) { return get(x, y, 0, NV, 1); }
    void update(int i, V v) { i += NV; val[i] = v; while (i>1)i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
    void compupdate(int i, V v) { update(i, comp(v, val[i + NV])); }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};

struct StaticHealthy2DSegTree {
    int NV;
    vector<SegTree> st;
    vector<vector<int>> index;
    
    void init(vector<vector<pair<int,V>>> &v) {
        int n = v.size();
        NV = 1; while (NV < n) NV *= 2;
        index.resize(2 * NV);
        rep(i, 0, n) fore(p, v[i]) index[i + NV].push_back(p.first);
        rrep(i, NV * 2 - 1, 1) {
            sort(index[i].begin(), index[i].end());
            index[i].erase(unique(index[i].begin(), index[i].end()), index[i].end());
            fore(j, index[i]) index[i / 2].push_back(j);
        }
        st.resize(2 * NV);
        rep(i, 1, NV * 2) st[i].init(index[i].size());
        
        rep(i, 0, n) fore(p, v[i]) {
            int j = lower_bound(index[i + NV].begin(), index[i + NV].end(), p.first) - index[i + NV].begin();
            st[i + NV].update(j, p.second);
        }

        rrep(i, NV * 2 - 1, 2) {
            rep(j, 0, index[i].size()) {
                V v = st[i][j];
                
                int k = lower_bound(index[i / 2].begin(), index[i / 2].end(), index[i][j]) - index[i / 2].begin();
                st[i / 2].compupdate(k, v);
            }
        }
    }
    V get(int sx, int tx, int sy, int ty, int k, int l, int r) {
        assert(k < NV * 2);
        assert(l < r);
        if (r <= sx or tx <= l) return def;
        if (sx <= l and r <= tx) {
            int syy = lower_bound(index[k].begin(), index[k].end(), sy) - index[k].begin();
            int tyy = lower_bound(index[k].begin(), index[k].end(), ty) - index[k].begin();
            return st[k].get(syy, tyy);
        }
        int md = (l + r) / 2;
        V le = get(sx, tx, sy, ty, k * 2, l, md);
        V ri = get(sx, tx, sy, ty, k * 2 + 1, md, r);
        return comp(le, ri);
    }
    V get(int sx, int tx, int sy, int ty) {
        return get(sx, tx, sy, ty, 1, 0, NV);
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, Q, A[101010];
StaticHealthy2DSegTree sum, cnt;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> Q;
    rep(i, 0, N) cin >> A[i];

    vector<vector<pair<int, ll>>> v_sum(N), v_cnt(N);
    rep(i, 0, N) {
        v_sum[i].push_back({A[i], 1LL*A[i]});
        v_cnt[i].push_back({A[i], 1LL});
    }
    sum.init(v_sum);
    cnt.init(v_cnt);

    rep(i, 0, Q) {
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        l--;

        ll ans = sum.get(l, r, x, inf) - cnt.get(l, r, x, inf) * x;
        printf("%lld\n", ans);
    }
}



0