結果

問題 No.877 Range ReLU Query
ユーザー nagissnagiss
提出日時 2019-09-06 22:23:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 592 ms / 2,000 ms
コード長 1,819 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 106,096 KB
最終ジャッジ日時 2024-04-25 22:03:15
合計ジャッジ時間 7,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,464 KB
testcase_01 AC 57 ms
67,408 KB
testcase_02 AC 55 ms
66,936 KB
testcase_03 AC 65 ms
70,200 KB
testcase_04 AC 44 ms
60,800 KB
testcase_05 AC 51 ms
63,612 KB
testcase_06 AC 51 ms
63,828 KB
testcase_07 AC 50 ms
62,620 KB
testcase_08 AC 63 ms
70,888 KB
testcase_09 AC 45 ms
60,424 KB
testcase_10 AC 56 ms
65,640 KB
testcase_11 AC 557 ms
106,080 KB
testcase_12 AC 486 ms
98,608 KB
testcase_13 AC 417 ms
95,520 KB
testcase_14 AC 441 ms
96,208 KB
testcase_15 AC 583 ms
106,044 KB
testcase_16 AC 546 ms
101,996 KB
testcase_17 AC 569 ms
102,848 KB
testcase_18 AC 566 ms
102,620 KB
testcase_19 AC 526 ms
106,096 KB
testcase_20 AC 592 ms
104,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)

    def __iter__(self):
        psum = 0
        for i in range(self.size):
            csum = self.sum(i+1)
            yield csum - psum
            psum = csum
        raise StopIteration()

    def __str__(self):  # O(nlogn)
        return str(list(self))

    def sum(self, i):
        # [0, i) の要素の総和を返す
        #if not (0 <= i <= self.size): raise ValueError("error!")
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        #if not (0 <= i < self.size): raise ValueError("error!")
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __getitem__(self, key):
        if not (0 <= key < self.size): raise IndexError("error!")
        return self.sum(key+1) - self.sum(key)

    def __setitem__(self, key, value):
        # 足し算と引き算にはaddを使うべき
        if not (0 <= key < self.size): raise IndexError("error!")
        self.add(key, value - self[key])

from operator import itemgetter
N, Q = map(int, input().split())
A = list(map(int, input().split()))
bit = Bit(N)
bit2 = Bit(N)
for i, a in enumerate(A):
    bit.add(i, a)
Ans = [-1] * Q
ILRX = []
A_ = sorted(enumerate(A), key=itemgetter(1))
for i in range(Q):
    q, l, r, x = map(int, input().split())
    ILRX.append((i, l-1, r, x))
ILRX.sort(key=itemgetter(3))
idx_a_ = 0
for i, l, r, x in ILRX:
    while idx_a_ < N:
        idx_a, a = A_[idx_a_]
        if a < x:
            bit.add(idx_a, -a)
            bit2.add(idx_a, 1)
            idx_a_ += 1
        else:
            break
    Ans[i] = bit.sum(r)-bit.sum(l) - (r-l-bit2.sum(r)+bit2.sum(l))*x

print("\n".join(map(str, Ans)))
0