結果

問題 No.1234 典型RMQ
ユーザー titiatitia
提出日時 2020-09-18 22:27:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 366 ms / 2,000 ms
コード長 2,985 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 94,592 KB
最終ジャッジ日時 2024-04-26 11:15:01
合計ジャッジ時間 9,460 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,608 KB
testcase_01 AC 41 ms
52,480 KB
testcase_02 AC 43 ms
52,608 KB
testcase_03 AC 39 ms
52,480 KB
testcase_04 AC 40 ms
52,480 KB
testcase_05 AC 40 ms
52,224 KB
testcase_06 AC 340 ms
89,472 KB
testcase_07 AC 303 ms
78,896 KB
testcase_08 AC 358 ms
94,080 KB
testcase_09 AC 343 ms
84,024 KB
testcase_10 AC 355 ms
92,544 KB
testcase_11 AC 354 ms
89,088 KB
testcase_12 AC 331 ms
83,020 KB
testcase_13 AC 303 ms
78,948 KB
testcase_14 AC 334 ms
83,196 KB
testcase_15 AC 326 ms
81,868 KB
testcase_16 AC 360 ms
92,032 KB
testcase_17 AC 334 ms
83,344 KB
testcase_18 AC 284 ms
78,020 KB
testcase_19 AC 366 ms
93,952 KB
testcase_20 AC 276 ms
93,312 KB
testcase_21 AC 366 ms
89,216 KB
testcase_22 AC 334 ms
94,592 KB
testcase_23 AC 331 ms
94,208 KB
testcase_24 AC 328 ms
94,208 KB
testcase_25 AC 329 ms
94,080 KB
testcase_26 AC 335 ms
94,080 KB
testcase_27 AC 42 ms
52,480 KB
testcase_28 AC 41 ms
52,352 KB
testcase_29 AC 41 ms
52,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

# 非再帰遅延伝搬セグ木(範囲加算, 範囲最小値出力)
# 高々N点を更新

seg_el=1<<(N.bit_length()) # Segment treeの台の要素数
SEG=[1<<60]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

for i in range(N): # Aを対応する箇所へupdate
    SEG[i+seg_el]=A[i]

for i in range(seg_el-1,0,-1): # 親の部分もupdate
    SEG[i]=min(SEG[i*2],SEG[i*2+1])


LAZY=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

def indexes(L,R): # 遅延伝搬すべきノードのリストを下から上の順に返す. (つまり, updateやgetvaluesで見るノードより上にあるノードたち)
    INDLIST=[]

    R-=1
    
    L>>=1
    R>>=1

    while L!=R:
        if L>R:
            INDLIST.append(L)
            L>>=1
        else:
            INDLIST.append(R)
            R>>=1

    while L!=0:
        INDLIST.append(L)
        L>>=1

    return INDLIST
        

def adds(l,r,x): # 区間[l,r)を +x 更新
        
    L=l+seg_el
    R=r+seg_el

    L//=(L & (-L))
    R//=(R & (-R))

    UPIND=indexes(L,R)
    
    for ind in UPIND[::-1]: # 遅延伝搬. 上から更新していく. (ただし, 今回はうまくやるとこの部分を省ける. addはクエリの順番によらないので. addではなく, updateの場合必要)
        if LAZY[ind]!=0:
            plus_lazy=LAZY[ind]
            
            SEG[ind<<1]+=plus_lazy
            SEG[1+(ind<<1)]+=plus_lazy
            
            LAZY[ind<<1]+=plus_lazy
            LAZY[1+(ind<<1)]+=plus_lazy
            
            LAZY[ind]=0
    
    while L!=R:
        if L > R:
            SEG[L]+=x
            LAZY[L]+=x
            L+=1
            L//=(L & (-L))

        else:
            R-=1
            SEG[R]+=x
            LAZY[R]+=x
            R//=(R & (-R))

    for ind in UPIND:
        SEG[ind]=min(SEG[ind<<1],SEG[1+(ind<<1)]) # 最初の遅延伝搬を省いた場合, ここを変える
        
def getvalues(l,r): # 区間[l,r)に関するminを調べる

    L=l+seg_el
    R=r+seg_el

    L//=(L & (-L))
    R//=(R & (-R))

    UPIND=indexes(L,R)
    
    for ind in UPIND[::-1]: # 遅延伝搬
        if LAZY[ind]!=0:
            plus_lazy=LAZY[ind]
            
            SEG[ind<<1]+=plus_lazy
            SEG[1+(ind<<1)]+=plus_lazy
            
            LAZY[ind<<1]+=plus_lazy
            LAZY[1+(ind<<1)]+=plus_lazy
            
            LAZY[ind]=0
            
    ANS=1<<60

    while L!=R:
        if L > R:
            ANS=min(ANS , SEG[L])
            L+=1
            L//=(L & (-L))

        else:
            R-=1
            ANS=min(ANS , SEG[R])
            R//=(R & (-R))

    return ANS

Q=int(input())
for queries in range(Q):
    k,l,r,c=map(int,input().split())
    if k==1:
        adds(l-1,r,c)
    else:
        print(getvalues(l-1,r))
    
0