結果

問題 No.877 Range ReLU Query
ユーザー tktk_snsn
提出日時 2020-09-28 23:55:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 807 ms / 2,000 ms
コード長 2,519 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 115,840 KB
最終ジャッジ日時 2024-11-08 10:32:08
合計ジャッジ時間 9,640 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

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