結果

問題 No.877 Range ReLU Query
ユーザー 👑 NachiaNachia
提出日時 2020-09-27 14:44:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 678 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 2,533 ms
コンパイル使用メモリ 179,724 KB
実行使用メモリ 54,604 KB
最終ジャッジ日時 2024-04-25 22:25:30
合計ジャッジ時間 9,383 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 639 ms
46,920 KB
testcase_12 AC 564 ms
46,108 KB
testcase_13 AC 430 ms
32,296 KB
testcase_14 AC 448 ms
30,356 KB
testcase_15 AC 676 ms
53,752 KB
testcase_16 AC 654 ms
52,844 KB
testcase_17 AC 678 ms
53,476 KB
testcase_18 AC 677 ms
53,796 KB
testcase_19 AC 426 ms
54,604 KB
testcase_20 AC 596 ms
54,604 KB
権限があれば一括ダウンロードができます

ソースコード

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