結果

問題 No.2065 Sum of Min
ユーザー titiatitia
提出日時 2022-09-10 01:16:29
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
TLE  
実行時間 -
コード長 2,120 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 11,224 KB
実行使用メモリ 80,636 KB
最終ジャッジ日時 2023-08-17 07:16:26
合計ジャッジ時間 45,128 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,368 KB
testcase_01 AC 17 ms
8,504 KB
testcase_02 AC 17 ms
8,464 KB
testcase_03 AC 17 ms
8,416 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 1,837 ms
73,720 KB
testcase_08 TLE -
testcase_09 AC 1,988 ms
77,348 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
権限があれば一括ダウンロードができます

ソースコード

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