結果

問題 No.1234 典型RMQ
ユーザー NatsubiSoganNatsubiSogan
提出日時 2020-12-21 02:58:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 990 ms / 2,000 ms
コード長 4,764 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 99,968 KB
最終ジャッジ日時 2024-11-09 02:39:17
合計ジャッジ時間 20,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
52,992 KB
testcase_01 AC 44 ms
53,504 KB
testcase_02 AC 44 ms
53,376 KB
testcase_03 AC 45 ms
53,376 KB
testcase_04 AC 44 ms
53,376 KB
testcase_05 AC 44 ms
53,120 KB
testcase_06 AC 934 ms
95,588 KB
testcase_07 AC 794 ms
85,576 KB
testcase_08 AC 990 ms
99,328 KB
testcase_09 AC 899 ms
92,544 KB
testcase_10 AC 964 ms
96,136 KB
testcase_11 AC 929 ms
96,884 KB
testcase_12 AC 935 ms
92,192 KB
testcase_13 AC 801 ms
86,332 KB
testcase_14 AC 892 ms
91,456 KB
testcase_15 AC 869 ms
90,292 KB
testcase_16 AC 972 ms
96,976 KB
testcase_17 AC 915 ms
91,644 KB
testcase_18 AC 752 ms
83,740 KB
testcase_19 AC 977 ms
99,328 KB
testcase_20 AC 498 ms
94,848 KB
testcase_21 AC 931 ms
95,668 KB
testcase_22 AC 787 ms
97,920 KB
testcase_23 AC 782 ms
99,968 KB
testcase_24 AC 772 ms
99,968 KB
testcase_25 AC 789 ms
97,920 KB
testcase_26 AC 761 ms
97,664 KB
testcase_27 AC 43 ms
53,376 KB
testcase_28 AC 44 ms
53,120 KB
testcase_29 AC 44 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegmentTree:
    def __init__(self, lis, op, ele, mapp, comp, id):
        self.lis = lis
        self.n = len(lis)
        self.op = op
        self.ele = ele
        self.mapp = mapp
        self.comp = comp
        self.id = id
        self.log = (self.n - 1).bit_length()
        self.size = 1 << self.log
        self.data = [ele] * (2 * self.size)
        self.lazy = [id] * self.size
        self._build(lis)
    def update(self, k):
        self.data[k] = self.op(self.data[2 * k], self.data[2 * k + 1])
    def _build(self, lis):
        for i, l in enumerate(lis, self.size):
            self.data[i] = l
        for i in range(1, self.size)[::-1]:
            self.update(i)
    def set(self, p, x):
        p += self.size
        for i in range(1, self.log + 1)[::-1]:
            self.push(p >> i)
        self.data[p] = x
        for i in range(1, self.log + 1):
            self.update(p >> i)
    def get(self, p):
        p += self.size
        for i in range(1, self.log + 1):
            self.push(p >> i)
        return self.data[p]
    def apply(self, p, f):
        p += self.size
        for i in range(1, self.log + 1)[::-1]:
            self.push(p >> i)
        self.data[p] = self.mapp(f, self.data[p])
        for i in range(1, self.log + 1):
            self.update(p >> i)
    def range_apply(self, l, r, f):
        if l == r: return 
        l += self.size
        r += self.size
        for i in range(1, self.log + 1)[::-1]:
            if ((l >> i) << i) != l:
                self.push(l >> i)
            if ((r >> i) << i) != r:
                self.push((r - 1) >> i)
        l2, r2 = l, r
        while l2 < r2:
            if l2 & 1:
                self.all_apply(l2, f)
                l2 += 1
            if r2 & 1:
                r2 -= 1
                self.all_apply(r2, f)
            l2 >>= 1
            r2 >>= 1
        for i in range(1, self.log + 1):
            if ((l >> i) << i) != l:
                self.update(l >> i)
            if ((r >> i) << i) != r:
                self.update((r - 1) >> i)
    def all_apply(self, k, f):
        self.data[k] = self.mapp(f, self.data[k])
        if k < self.size:
            self.lazy[k] = self.comp(f, self.lazy[k])
    def push(self, k):
        self.all_apply(2 * k, self.lazy[k])
        self.all_apply(2 * k + 1, self.lazy[k])
        self.lazy[k] = self.id
    def prod(self, l, r):
        if l == r: return self.ele
        l += self.size
        r += self.size
        for i in range(1, self.log + 1)[::-1]:
            if ((l >> i) << i) != l:
                self.push(l >> i)
            if ((r >> i) << i) != r:
                self.push(r >> i)
        sml = smr = self.ele
        while l < r:
            if l & 1:
                sml = self.op(sml, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.data[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)
    def all_prod(self):
        return self.data[1]
    def max_right(self, l, g):
        if l == self.n: return self.n
        l += self.size
        for i in range(1, self.log + 1)[::-1]:
            self.push(l >> i)
        sm = self.ele
        while True:
            while l % 2 == 0:
                l >>= 1
            if not g(self.op(sm, self.data[l])):
                while l < self.size:
                    self.push(l)
                    l <<= 1
                    if g(self.op(sm, self.data[l])):
                        sm = self.op(sm, self.data[l])
                        l += 1
                return l - self.size
            sm = self.op(sm, self.data[l])
            l += 1
            if (l & -l) == l: return self.n
    def min_left(self, r, g):
        if r == 0: return 0
        r += self.size
        for i in range(1, self.log + 1)[::-1]:
            self.push((r - 1) >> i)
        sm = self.ele
        while True:
            r -= 1
            while r > 1 and r % 2:
                r >>= 1
            if not g(self.op(self.data[r], sm)):
                while r < self.size:
                    self.push(r)
                    r = 2 * r + 1
                    if g(self.op(self.data[r], sm)):
                        sm = self.op(self.data[r], sm)
                        r -= 1
                return r + 1 - self.size
            sm = self.op(self.data[r], sm)
            if (r & -r) == r: return 0
n = int(input())
l = list(map(int, input().split()))
ele = float("inf")
op = min
def mapp(p, x): return p + x
def comp(p, q): return p + q
LST = LazySegmentTree(l, op, ele, mapp, comp, 0)
for _ in range(int(input())):
    k, l, r, c = map(int, input().split())
    if k == 1:LST.range_apply(l - 1, r, c)
    else:print(LST.prod(l - 1, r))
0