結果

問題 No.2065 Sum of Min
ユーザー titiatitia
提出日時 2022-09-10 01:16:43
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 938 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 120,268 KB
最終ジャッジ日時 2023-08-17 07:17:17
合計ジャッジ時間 17,539 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,736 KB
testcase_01 AC 76 ms
71,640 KB
testcase_02 AC 76 ms
71,212 KB
testcase_03 AC 75 ms
71,408 KB
testcase_04 AC 643 ms
117,356 KB
testcase_05 AC 585 ms
116,288 KB
testcase_06 AC 558 ms
117,216 KB
testcase_07 AC 322 ms
116,220 KB
testcase_08 AC 566 ms
120,268 KB
testcase_09 AC 823 ms
119,132 KB
testcase_10 AC 818 ms
118,752 KB
testcase_11 AC 826 ms
118,672 KB
testcase_12 AC 920 ms
119,520 KB
testcase_13 AC 898 ms
119,360 KB
testcase_14 AC 903 ms
119,404 KB
testcase_15 AC 917 ms
119,100 KB
testcase_16 AC 902 ms
119,208 KB
testcase_17 AC 904 ms
119,288 KB
testcase_18 AC 938 ms
119,316 KB
testcase_19 AC 922 ms
119,460 KB
testcase_20 AC 912 ms
119,432 KB
testcase_21 AC 915 ms
119,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from operator import itemgetter

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


def seg_function(x,y): # Segment treeで扱うfunction
    return x+y

seg_el=1<<(N.bit_length()) # Segment treeの台の要素数
SEG=[0]*(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]=seg_function(SEG[i*2],SEG[i*2+1])
    
def update(n,x,seg_el): # A[n]をxへ更新
    i=n+seg_el
    SEG[i]=x
    i>>=1 # 子ノードへ
    
    while i!=0:
        SEG[i]=seg_function(SEG[i*2],SEG[i*2+1])
        i>>=1
        
def getvalues(l,r): # 区間[l,r)に関するseg_functionを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=0

    while L<R:
        if L & 1:
            ANS=seg_function(ANS , SEG[L])
            L+=1

        if R & 1:
            R-=1
            ANS=seg_function(ANS , SEG[R])
        L>>=1
        R>>=1

    return ANS

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

def update2(n,x,seg_el): # A[n]をxへ更新
    i=n+seg_el
    SEG2[i]=x
    i>>=1 # 子ノードへ
    
    while i!=0:
        SEG2[i]=seg_function(SEG2[i*2],SEG2[i*2+1])
        i>>=1
        
def getvalues2(l,r): # 区間[l,r)に関するseg_functionを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=0

    while L<R:
        if L & 1:
            ANS=seg_function(ANS , SEG2[L])
            L+=1

        if R & 1:
            R-=1
            ANS=seg_function(ANS , SEG2[R])
        L>>=1
        R>>=1

    return ANS

QU=[list(map(int,input().split())) for i in range(Q)]

X=[]

for i in range(N):
    X.append((A[i],i))

for i in range(Q):
    l,r,x=QU[i]
    X.append((x,1<<30,i,l-1,r))

X.sort(reverse=True)
ANS=[-1]*Q

for xx in X:
    if xx[1]==1<<30:
        ANS[xx[2]]=getvalues(xx[3],xx[4])+getvalues2(xx[3],xx[4])*xx[0]
    else:
        update(xx[1],0,seg_el)
        update2(xx[1],1,seg_el)

print("\n".join(map(str,ANS)))
        
0