結果

問題 No.877 Range ReLU Query
ユーザー tachyon777tachyon777
提出日時 2020-05-05 11:58:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 961 ms / 2,000 ms
コード長 2,669 bytes
コンパイル時間 623 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 142,144 KB
最終ジャッジ日時 2024-04-25 22:21:33
合計ジャッジ時間 10,990 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,872 KB
testcase_01 AC 58 ms
70,852 KB
testcase_02 AC 55 ms
69,188 KB
testcase_03 AC 62 ms
74,040 KB
testcase_04 AC 40 ms
60,424 KB
testcase_05 AC 45 ms
63,652 KB
testcase_06 AC 52 ms
66,700 KB
testcase_07 AC 44 ms
62,956 KB
testcase_08 AC 65 ms
73,600 KB
testcase_09 AC 41 ms
61,220 KB
testcase_10 AC 54 ms
68,936 KB
testcase_11 AC 870 ms
129,928 KB
testcase_12 AC 823 ms
126,716 KB
testcase_13 AC 647 ms
115,928 KB
testcase_14 AC 634 ms
113,028 KB
testcase_15 AC 960 ms
141,096 KB
testcase_16 AC 952 ms
140,616 KB
testcase_17 AC 961 ms
142,024 KB
testcase_18 AC 959 ms
140,984 KB
testcase_19 AC 921 ms
141,372 KB
testcase_20 AC 939 ms
142,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder No.877 Range ReLU Query
"""
問題:
xが与えられる。
セグ木の要素aiがあり、区間l<=i<=rに対し、Σi=[l,r]max(ai-x,0)
を出力せよ(つまり、各要素におけるreluの合計)
解法:
ai-x=0となる場合無視すると考えることができる。
よって、各クエリのxについて
常にxより大きいような要素のみをセグ木に埋め込み続ける
これによってただのreturn x+y のsegfuncによって問題を解けるようになる

xについて降順にソートし、各セグ木の要素についても降順にソートしておく。
なお、ai-xであることから、区間クエリ-(x*その区間に含まれる要素数)
を求める必要があるので、セグ木は二次元に持って、
[値,そこまでの要素数]とする必要があるので注意
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,q = map(int,readline().split())
lst1 = list(map(int,readline().split()))
lst2 = []
for i in range(q):
    #comは1のみなので無視できる
    com,l,r,x = map(int,readline().split())
    lst2.append([x,l-1,r-1,i])

ans = [0]*q #lst2[3]がindex

#####segfunc######
def segfunc(x,y):
    return [x[0]+y[0],x[1]+y[1]]

def init(init_val): #渡されたリストでsegを初期化
    #set_val
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]    
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x): #segの要素kをxに変更(セグ木全体の更新)
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q): #区間[p,q)での、segfuncに準じた値を返す
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

ide_ele = [0,0]
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num

lst2.sort(reverse=True)
lst3 = []
for i,j in enumerate(lst1):
    lst3.append([j,i]) #値,index

lst3.sort(reverse=True)

now = 0
for x,l,r,i in lst2:
    while True:
        if now >= len(lst3):
            break
        if lst3[now][0] <= x:
            break
        update(lst3[now][1],[lst3[now][0],1])
        now += 1
    a,b = query(l,r+1) #a:区間内合計,b:区間内要素数
    ans[i] = a - b*x

for i in ans:
    print(i)
    
0