結果

問題 No.877 Range ReLU Query
ユーザー titia
提出日時 2019-09-06 23:00:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 892 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,720 KB
最終ジャッジ日時 2024-11-08 10:10:54
合計ジャッジ時間 10,240 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

# BIT(BIT-indexed tree)
LEN=N

BIT=[0]*(LEN+1)# 1-indexedなtree

def update(v,w):# index vにwを加える
    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v))# 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue(v):# [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v))# 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS

BIT2=[0]*(LEN+1)# 1-indexedなtree

def update2(v,w):# index vにwを加える
    while v<=LEN:
        BIT2[v]+=w
        v+=(v&(-v))# 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue2(v):# [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT2[v]
        v-=(v&(-v))# 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS


ALL=[(x,i) for i,x in enumerate(A)]

for ind,(_,l,r,x) in enumerate(Query):
    ALL.append((x,l,r,ind))

ALL.sort(reverse=True)

ANS=[0]*Q

for a in ALL:
    if len(a)==2:
        update(a[1]+1,a[0])
        update2(a[1]+1,1)
    else:
        ANS[a[3]]=getvalue(a[2])-getvalue(a[1]-1)-a[0]*(getvalue2(a[2])-getvalue2(a[1]-1))

for ans in ANS:
    print(ans)
        
0