結果

問題 No.1234 典型RMQ
ユーザー 👑 tamatotamato
提出日時 2020-09-19 14:13:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 480 ms / 2,000 ms
コード長 3,461 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 100,884 KB
最終ジャッジ日時 2023-08-08 18:56:49
合計ジャッジ時間 12,946 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,760 KB
testcase_01 AC 71 ms
71,688 KB
testcase_02 AC 72 ms
71,644 KB
testcase_03 AC 72 ms
71,768 KB
testcase_04 AC 73 ms
71,556 KB
testcase_05 AC 71 ms
71,612 KB
testcase_06 AC 457 ms
95,980 KB
testcase_07 AC 412 ms
81,628 KB
testcase_08 AC 468 ms
100,884 KB
testcase_09 AC 445 ms
87,700 KB
testcase_10 AC 468 ms
98,640 KB
testcase_11 AC 465 ms
95,792 KB
testcase_12 AC 444 ms
86,932 KB
testcase_13 AC 420 ms
81,660 KB
testcase_14 AC 440 ms
86,848 KB
testcase_15 AC 446 ms
85,604 KB
testcase_16 AC 464 ms
98,624 KB
testcase_17 AC 446 ms
87,132 KB
testcase_18 AC 398 ms
81,980 KB
testcase_19 AC 464 ms
100,668 KB
testcase_20 AC 272 ms
100,116 KB
testcase_21 AC 457 ms
95,584 KB
testcase_22 AC 480 ms
100,744 KB
testcase_23 AC 446 ms
100,644 KB
testcase_24 AC 478 ms
100,500 KB
testcase_25 AC 435 ms
100,560 KB
testcase_26 AC 449 ms
100,836 KB
testcase_27 AC 72 ms
71,652 KB
testcase_28 AC 73 ms
71,764 KB
testcase_29 AC 71 ms
71,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    def op(a, b):
        return min(a, b)

    e = 1 << 60

    def mapping(a, x):
        return a + x

    def composition(x, y):
        return x + y

    id = 0

    class LazySegTree:
        # Range update query
        def __init__(self, A, op=op, e=e, mapping=mapping, composition=composition, id=id, initialize=True):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.op = op
            self.e = e
            self.mapping = mapping
            self.composition = composition
            self.id = id
            if initialize:
                self.data = [self.e] * self.N0 + A + [self.e] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = op(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.e] * (self.N0 * 2)
            self.lazy = [id] * (self.N0 * 2)

        def _ascend(self, i):
            for _ in range(i.bit_length() - 1):
                i >>= 1
                self.data[i] = self.op(self.data[i * 2], self.data[i * 2 + 1])

        def _descend(self, idx):
            lv = idx.bit_length()
            for j in range(lv - 1, 0, -1):
                i = idx >> j
                x = self.lazy[i]
                if x == self.id:
                    continue
                self.lazy[i * 2] = self.composition(self.lazy[i * 2], x)
                self.lazy[i * 2 + 1] = self.composition(self.lazy[i * 2 + 1], x)
                self.lazy[i] = self.id
                self.data[i * 2] = self.mapping(self.data[i * 2], x)
                self.data[i * 2 + 1] = self.mapping(self.data[i * 2 + 1], x)

        # open interval [l, r)
        def apply(self, l, r, x):
            l += self.N0 - 1
            r += self.N0 - 1
            self._descend(l // (l & -l))
            self._descend(r // (r & -r) - 1)
            l_ori = l
            r_ori = r
            while l < r:
                if l & 1:
                    self.data[l] = self.mapping(self.data[l], x)
                    self.lazy[l] = self.composition(self.lazy[l], x)
                    l += 1
                if r & 1:
                    r -= 1
                    self.data[r] = self.mapping(self.data[r], x)
                    self.lazy[r] = self.composition(self.lazy[r], x)
                l >>= 1
                r >>= 1
            self._ascend(l_ori // (l_ori & -l_ori))
            self._ascend(r_ori // (r_ori & -r_ori) - 1)

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            self._descend(l // (l & -l))
            self._descend(r // (r & -r) - 1)
            ret = self.e
            while l < r:
                if l & 1:
                    ret = self.op(ret, self.data[l])
                    l += 1
                if r & 1:
                    ret = self.op(self.data[r - 1], ret)
                    r -= 1
                l >>= 1
                r >>= 1
            return ret

    N = int(input())
    A = list(map(int, input().split()))
    ST = LazySegTree(A)
    for q in range(int(input())):
        k, l, r, c = map(int, input().split())
        if k == 1:
            ST.apply(l, r+1, c)
        else:
            print(ST.query(l, r+1))


if __name__ == '__main__':
    main()
0