結果

問題 No.877 Range ReLU Query
ユーザー 👑 Nachia
提出日時 2020-09-27 14:44:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 181,920 KB
実行使用メモリ 54,352 KB
最終ジャッジ日時 2024-11-08 10:31:35
合計ジャッジ時間 10,161 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)


struct RQ {
private:
    struct Node { vector<LL> x, s; };
    int N;
    vector<Node> V;

public:

    RQ(vector<LL> A) {
        N = 1; while (N < A.size()) N *= 2;
        V.assign(N * 2, { {},{} });
        rep(i, A.size()) V[N + i] = { {A[i]}, {A[i],0} };
        for (int i = N - 1; i >= 1; i--) {
            for (LL a : V[i * 2].x) V[i].x.push_back(a);
            for (LL a : V[i * 2 + 1].x) V[i].x.push_back(a);
            sort(V[i].x.begin(), V[i].x.end());
            V[i].s.resize(V[i].x.size() + 1);
            for (int j = V[i].x.size() - 1; j >= 0; j--)
                V[i].s[j] = V[i].s[j + 1] + V[i].x[j];
        }
    }

    LL sum(int l, int r, LL x, int a = 0, int b = 0, int i = -1) {
        if (i == -1) { a = 0; b = N; i = 1; }
        if (r <= a || b <= l) return 0;
        if (l <= a && b <= r) {
            int xl = 0, xr = V[i].x.size() + 1;
            while (xl + 1 < xr) {
                int xm = (xl + xr) / 2;
                if (V[i].x[xm - 1] < x) xl = xm; else xr = xm;
            }
            if (xl == V[i].x.size()) return 0;
            LL ans = V[i].s[xl];
            ans -= (V[i].x.size() - xl) * x;
            return ans;
        }
        LL q1 = sum(l, r, x, a, (a + b) / 2, i * 2);
        LL q2 = sum(l, r, x, (a + b) / 2, b, i * 2 + 1);
        return q1 + q2;
    }
};


int main() {
    int N, Q; cin >> N >> Q;
    vector<LL> A(N); rep(i, N) { cin >> A[i]; }
    RQ G(A);

    while (Q--) {
        int c; cin >> c;
        if (c == 1) {
            int l, r, x; cin >> l >> r >> x; l--;
            cout << G.sum(l, r, x) << endl;
        }
    }

    return 0;
}
0