結果

問題 No.1234 典型RMQ
ユーザー O2MTO2MT
提出日時 2021-02-15 10:52:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 678 ms / 2,000 ms
コード長 3,390 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,708 KB
実行使用メモリ 101,344 KB
最終ジャッジ日時 2024-04-26 11:50:26
合計ジャッジ時間 15,657 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,120 KB
testcase_01 AC 41 ms
53,376 KB
testcase_02 AC 41 ms
53,376 KB
testcase_03 AC 40 ms
53,248 KB
testcase_04 AC 40 ms
52,992 KB
testcase_05 AC 43 ms
53,120 KB
testcase_06 AC 662 ms
95,744 KB
testcase_07 AC 607 ms
80,332 KB
testcase_08 AC 678 ms
100,608 KB
testcase_09 AC 637 ms
87,508 KB
testcase_10 AC 662 ms
98,904 KB
testcase_11 AC 647 ms
95,488 KB
testcase_12 AC 625 ms
86,272 KB
testcase_13 AC 616 ms
80,432 KB
testcase_14 AC 623 ms
86,380 KB
testcase_15 AC 626 ms
84,528 KB
testcase_16 AC 663 ms
98,652 KB
testcase_17 AC 650 ms
86,144 KB
testcase_18 AC 577 ms
79,464 KB
testcase_19 AC 650 ms
101,344 KB
testcase_20 AC 403 ms
99,584 KB
testcase_21 AC 646 ms
95,616 KB
testcase_22 AC 617 ms
100,864 KB
testcase_23 AC 629 ms
101,120 KB
testcase_24 AC 618 ms
101,220 KB
testcase_25 AC 604 ms
100,992 KB
testcase_26 AC 610 ms
100,736 KB
testcase_27 AC 41 ms
53,120 KB
testcase_28 AC 42 ms
53,248 KB
testcase_29 AC 42 ms
53,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from operator import add
class Lazysegtree: #tpyneriverさんのdiff#555661をコピぺしています。
    def __init__(self, A, fx, ex, fm, em, fa, initialize = True):
        #fa(operator, idx)の形にしている、data[idx]の長さに依存する場合などのため
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()

        self.fx = fx
        self.fm = fm

        self.ex = ex
        self.em = em
        self.lazy = [em]*(2*self.N0)
        if initialize:
            self.data = [self.ex]*self.N0 + A + [self.ex]*(self.N0 - self.N)
            for i in range(self.N0-1, -1, -1):
                self.data[i] = self.fx(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [self.ex]*(2*self.N0)        
    
    def fa(self, ope, idx):
        return ope + self.data[idx]
    
    def __repr__(self):
        s = 'data'
        l = 'lazy'
        cnt = 1
        for i in range(self.N0.bit_length()):
            s = '\n'.join((s, ' '.join(map(str, self.data[cnt:cnt+(1<<i)]))))
            l = '\n'.join((l, ' '.join(map(str, self.lazy[cnt:cnt+(1<<i)]))))
            cnt += 1<<i
        return '\n'.join((s, l))
    
    def _ascend(self, k):
        k = k >> 1
        c = k.bit_length()
        for j in range(c):
            idx = k >> j
            self.data[idx] = self.fx(self.data[2*idx], self.data[2*idx+1])
            
    def _descend(self, k):
        k = k >> 1
        idx = 1
        c = k.bit_length()
        for j in range(1, c+1):
            idx = k >> (c - j)
            if self.lazy[idx] == self.em:
                continue
            self.data[2*idx] = self.fa(self.lazy[idx], 2*idx) 
            self.data[2*idx+1] = self.fa(self.lazy[idx], 2*idx+1)
            self.lazy[2*idx] = self.fm(self.lazy[idx], self.lazy[2*idx])
            self.lazy[2*idx+1] = self.fm(self.lazy[idx], self.lazy[2*idx+1])
            self.lazy[idx] = self.em
            
            
    def query(self, l, r):
        L = l+self.N0
        R = r+self.N0
        self._descend(L//(L & -L))
        self._descend(R//(R & -R)-1)
        
        sl = self.ex
        sr = self.ex                                                                   

        while L < R:
            if R & 1:
                R -= 1
                sr = self.fx(self.data[R], sr)
            if L & 1:
                sl = self.fx(sl, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return self.fx(sl, sr)
    
    def operate(self, l, r, x):
        L = l+self.N0
        R = r+self.N0

        Li = L//(L & -L)
        Ri = R//(R & -R)
        self._descend(Li)
        self._descend(Ri-1)
        
        while L < R :
            if R & 1:
                R -= 1
                self.data[R] = self.fa(x, R)
                self.lazy[R] = self.fm(x, self.lazy[R])
            if L & 1:
                self.data[L] = self.fa(x, L)
                self.lazy[L] = self.fm(x, self.lazy[L])
                L += 1
            L >>= 1
            R >>= 1
        
        self._ascend(Li)
        self._ascend(Ri-1)

N = int(input())
A = list(map(int,input().split()))
Q = int(input())

INF = 10**18
tree = Lazysegtree(A, min, INF, add, 0, None, initialize = True)

for _ in range(Q):
    k,l,r,c = map(int,input().split())
    if k == 1:
        tree.operate(l-1,r,c)
    else:
        print(tree.query(l-1,r))
0