結果

問題 No.877 Range ReLU Query
ユーザー yosupotyosupot
提出日時 2019-09-06 22:51:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,440 ms / 2,000 ms
コード長 5,158 bytes
コンパイル時間 2,692 ms
コンパイル使用メモリ 219,248 KB
実行使用メモリ 113,692 KB
最終ジャッジ日時 2023-08-08 04:23:56
合計ジャッジ時間 17,290 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1,331 ms
98,876 KB
testcase_12 AC 1,150 ms
97,568 KB
testcase_13 AC 839 ms
65,028 KB
testcase_14 AC 872 ms
60,708 KB
testcase_15 AC 1,436 ms
112,248 KB
testcase_16 AC 1,424 ms
110,356 KB
testcase_17 AC 1,440 ms
111,868 KB
testcase_18 AC 1,439 ms
112,480 KB
testcase_19 AC 677 ms
113,692 KB
testcase_20 AC 1,041 ms
113,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

template <class T> struct Fenwick {
    int n;
    V<T> seg;
    Fenwick(int _n = 0) : n(_n), seg(n + 1) {}
    /// i番目の要素にxを追加する
    void add(int i, T x) {
        i++;
        while (i <= n) {
            seg[i] += x;
            i += i & -i;
        }
    }
    /// [0, i)のsum
    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += seg[i];
            i -= i & -i;
        }
        return s;
    }
    /// [a, b)のsum
    T sum(int a, int b) { return sum(b) - sum(a); }
    /// sum[0, idx) >= xなる最小のidx(sum[0, n) < x なら n+1)
    int sum_lower_bound(T x) {
        if (x <= 0) return 0;
        int res = 0, len = 1;
        while (2 * len <= n) len *= 2;
        for (; len >= 1; len /= 2) {
            if (res + len <= n && seg[res + len] < x) {
                res += len;
                x -= seg[res];
            }
        }
        return res + 1;
    }
};


template <class D, class I> struct Fenwick2D {
    using P = pair<I, I>;
    V<P> points;
    VV<I> ys;
    V<Fenwick<D>> fws;
    int lg, sz;
    Fenwick2D(V<P> _points) : points(_points) {
        sort(points.begin(), points.end());
        points.erase(unique(points.begin(), points.end()), points.end());
        int n = int(points.size());
        lg = 1;
        while ((1 << lg) < n) lg++;
        sz = 1 << lg;
        ys = VV<I>(2 * sz);
        for (int i = 0; i < n; i++) ys[sz + i].push_back(points[i].second);
        for (int i = sz - 1; i >= 1; i--) {
            ys[i] = V<I>(ys[2 * i].size() + ys[2 * i + 1].size());
            merge(ys[2 * i].begin(), ys[2 * i].end(), ys[2 * i + 1].begin(),
                  ys[2 * i + 1].end(), ys[i].begin());
        }
        fws = V<Fenwick<D>>(2 * sz);
        for (int i = 1; i < 2 * sz; i++) {
            fws[i] = Fenwick<D>(int(ys[i].size()));
        }
    }

    void add(P p, D x) {
        int k =
            int(lower_bound(points.begin(), points.end(), p) - points.begin());
        k += sz;
        while (k) {
            int yid = lower_bound(ys[k].begin(), ys[k].end(), p.second) -
                      ys[k].begin();
            fws[k].add(yid, x);
            k >>= 1;
        }
    }

    D sum(int a, int b, I lw, I up, int l, int r, int k) {
        if (b <= l || r <= a) return D(0);
        if (a <= l && r <= b) {
            int lid =
                lower_bound(ys[k].begin(), ys[k].end(), lw) - ys[k].begin();
            int uid =
                lower_bound(ys[k].begin(), ys[k].end(), up) - ys[k].begin();
            return fws[k].sum(lid, uid);
        }
        int mid = (l + r) / 2;
        return sum(a, b, lw, up, l, mid, 2 * k) +
               sum(a, b, lw, up, mid, r, 2 * k + 1);
    }

    D sum(P lw, P up) {
        int a = lower_bound(points.begin(), points.end(), lw.first,
                            [&](P p, I x) { return p.first < x; }) -
                points.begin();
        int b = lower_bound(points.begin(), points.end(), up.first,
                            [&](P p, I x) { return p.first < x; }) -
                points.begin();
        return sum(a, b, lw.second, up.second, 0, sz, 1);
    }
};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;
    using P = pair<ll, ll>;
    V<P> a(n);
    for (int i = 0; i < n; i++) {
        a[i].first = i;
        cin >> a[i].second;
    }    
    auto fen2d0 = Fenwick2D<ll, ll>(a);
    auto fen2d1 = Fenwick2D<ll, ll>(a);
    for (auto p: a) {
        fen2d0.add(p, 1);
        fen2d1.add(p, p.second);
    }
    
    for (int ph = 0; ph < q; ph++) {
        int ty, l, r;
        ll x;
        cin >> ty >> l >> r >> x; l--;

        ll y = fen2d0.sum(P(l, x), P(r, TEN(9) + 1));
        ll z = fen2d1.sum(P(l, x), P(r, TEN(9) + 1));
        
        cout << z - y * x << "\n";
    }
    return 0;
}
0