結果

問題 No.877 Range ReLU Query
ユーザー tktk_snsntktk_snsn
提出日時 2020-09-28 23:55:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 789 ms / 2,000 ms
コード長 2,519 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 115,664 KB
最終ジャッジ日時 2024-04-25 22:26:03
合計ジャッジ時間 9,380 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,620 KB
testcase_01 AC 63 ms
72,424 KB
testcase_02 AC 58 ms
68,572 KB
testcase_03 AC 68 ms
74,220 KB
testcase_04 AC 44 ms
63,100 KB
testcase_05 AC 48 ms
64,020 KB
testcase_06 AC 50 ms
65,952 KB
testcase_07 AC 50 ms
65,164 KB
testcase_08 AC 74 ms
76,448 KB
testcase_09 AC 44 ms
62,168 KB
testcase_10 AC 56 ms
67,404 KB
testcase_11 AC 696 ms
111,160 KB
testcase_12 AC 631 ms
107,908 KB
testcase_13 AC 535 ms
101,880 KB
testcase_14 AC 532 ms
102,028 KB
testcase_15 AC 775 ms
114,516 KB
testcase_16 AC 728 ms
113,136 KB
testcase_17 AC 762 ms
114,392 KB
testcase_18 AC 758 ms
113,984 KB
testcase_19 AC 744 ms
112,296 KB
testcase_20 AC 789 ms
115,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from operator import add
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class SegmentTree:
    def __init__(self, N, arr=None, op=None, unit=None):
        """
        INPUT
        N: 配列のサイズ
        segment tree: 1-indexedで構築
        original array: 0-indexed
                 1
           2           3
         4    5     6     7
        8 9 10 11 12 13 14 15
        ---------------------
        0 1 2  3  4  5  6  7
        """
        self.N = N
        self.unit = unit
        self.op = op
        self.tree = [unit] * (2 * N + 1)
        if arr is not None:
            self._build(arr)

    def _build(self, arr):
        op = self.op
        N = self.N
        for i, a in enumerate(arr, N):
            self.tree[i] = a
        for i in reversed(range(1, N)):
            self.tree[i] = op(self.tree[i << 1], self.tree[i << 1 | 1])

    def show(self):
        for i in range(self.N):
            print(self.tree[i+self.N], end=" ")
        print()

    def update(self, i, x):
        """
        arr[i]の値をxに変更する
        i: 0-indexed
        """
        i += self.N
        op = self.op
        self.tree[i] = x
        while i > 1:
            i >>= 1
            self.tree[i] = op(self.tree[i << 1], self.tree[i << 1 | 1])

    def query(self, L, R):
        """
        L,R: 0-indexed
        半開区間[L,R)の累積演算した値を返す
        """
        op = self.op
        res = self.unit
        L += self.N
        R += self.N
        while L < R:
            if L & 1:
                res = op(res, self.tree[L])
                L += 1
            if R & 1:
                R -= 1
                res = op(res, self.tree[R])
            L >>= 1
            R >>= 1
        return res


def op(a, b):
    return a[0]+b[0], a[1]+b[1]


N, Q = map(int, input().split())
seg = SegmentTree(N, [(0, 0)]*N,  op=op, unit=(0, 0))
ans = [-1] * Q

A = [(a, i) for i, a in enumerate(map(int, input().split()))]
query = []
for i in range(Q):
    _, l, r, x = map(int, input().split())
    query.append((x, l, r, i))

A.sort()
query.sort()

while query and A:
    if A[-1][0] > query[-1][0]:
        a, i = A.pop()
        seg.update(i, (a, 1))
    else:
        x, l, r, i = query.pop()
        val, sz = seg.query(l - 1, r)
        ans[i] = val - x * sz

while A:
    a, i = A.pop()
    seg.update(i, (a, 1))

while query:
    x, l, r, i = query.pop()
    val, sz = seg.query(l - 1, r)
    ans[i] = val - x * sz

print(*ans, sep="\n")
0