結果

問題 No.1234 典型RMQ
ユーザー c-yanc-yan
提出日時 2021-02-07 03:17:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,571 ms / 2,000 ms
コード長 2,758 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 96,384 KB
最終ジャッジ日時 2024-04-26 11:47:34
合計ジャッジ時間 27,081 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,224 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 AC 40 ms
52,480 KB
testcase_03 AC 41 ms
52,608 KB
testcase_04 AC 40 ms
52,608 KB
testcase_05 AC 38 ms
52,480 KB
testcase_06 AC 1,156 ms
91,548 KB
testcase_07 AC 892 ms
87,476 KB
testcase_08 AC 1,219 ms
96,272 KB
testcase_09 AC 1,262 ms
89,344 KB
testcase_10 AC 1,244 ms
93,568 KB
testcase_11 AC 1,082 ms
92,544 KB
testcase_12 AC 980 ms
89,728 KB
testcase_13 AC 831 ms
87,680 KB
testcase_14 AC 1,303 ms
90,256 KB
testcase_15 AC 1,131 ms
90,376 KB
testcase_16 AC 1,112 ms
93,312 KB
testcase_17 AC 937 ms
89,984 KB
testcase_18 AC 879 ms
87,536 KB
testcase_19 AC 1,571 ms
96,176 KB
testcase_20 AC 1,042 ms
94,464 KB
testcase_21 AC 987 ms
92,288 KB
testcase_22 AC 1,424 ms
96,256 KB
testcase_23 AC 1,279 ms
96,128 KB
testcase_24 AC 1,482 ms
96,036 KB
testcase_25 AC 1,236 ms
96,000 KB
testcase_26 AC 1,165 ms
96,384 KB
testcase_27 AC 40 ms
52,864 KB
testcase_28 AC 40 ms
52,864 KB
testcase_29 AC 39 ms
52,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin, setrecursionlimit


class SegmentTree:
    DEFAULT_VALUE = 10 ** 18
    DEFAULT_LAZY = 0
    op = min

    def __init__(self, size):
        self._size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._values = [SegmentTree.DEFAULT_VALUE] * (t * 2 - 1)
        self._lazy = [SegmentTree.DEFAULT_LAZY] * (t * 2 - 1)

    def build(self, iterable):
        op = SegmentTree.op
        values = self._values
        values[self._offset:self._offset + self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            values[i] = SegmentTree.op(values[i * 2 + 1], values[i * 2 + 2])

    def _iter_segmen_indexes(self, start, stop):
        l = start + self._offset
        r = stop + self._offset
        while l < r:
            if l & 1 == 0:
                yield l
            if r & 1 == 0:
                yield r - 1
            l = l // 2
            r = (r - 1) // 2

    def _propagate_to(self, index, value):
        self._lazy[index] += value
        self._values[index] += value

    def _propagate_at(self, index):
        if index != 0:
            self._propagate_at((index - 1) // 2)
        lazy = self._lazy[index]
        if lazy == SegmentTree.DEFAULT_LAZY:
            return
        self._propagate_to(index * 2 + 1, lazy)
        self._propagate_to(index * 2 + 2, lazy)
        self._lazy[index] = SegmentTree.DEFAULT_LAZY

    def _propagate(self, start, stop):
        for i in self._iter_segmen_indexes(start, stop):
            if i != 0:
                self._propagate_at((i - 1) // 2)

    def _apply_to(self, index, value):
        op = SegmentTree.op
        values = self._values
        self._lazy[index] += value
        values[index] += value
        while index >= 1:
            index = (index - 1) // 2
            values[index] = op(values[index * 2 + 1], values[index * 2 + 2])

    def apply(self, start, stop, value):
        self._propagate(start, stop)
        for i in self._iter_segmen_indexes(start, stop):
            self._apply_to(i, value)

    def query(self, start, stop):
        op = SegmentTree.op
        values = self._values
        self._propagate(start, stop)
        result = SegmentTree.DEFAULT_VALUE
        for i in self._iter_segmen_indexes(start, stop):
            result = op(result, values[i])
        return result


readline = stdin.readline
setrecursionlimit(10 ** 6)

N = int(readline())
a = list(map(int, readline().split()))
Q = int(readline())

st = SegmentTree(N)
st.build(a)
result = []
for _ in range(Q):
    k, l, r, c = map(int, readline().split())
    if k == 1:
        st.apply(l - 1, r, c)
    elif k == 2:
        result.append(st.query(l - 1, r))
print(*result, sep='\n')
0