結果

問題 No.877 Range ReLU Query
ユーザー convexineqconvexineq
提出日時 2019-09-06 23:23:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 559 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 104,636 KB
最終ジャッジ日時 2024-04-25 22:07:15
合計ジャッジ時間 7,047 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,348 KB
testcase_01 AC 58 ms
71,060 KB
testcase_02 AC 62 ms
71,760 KB
testcase_03 AC 64 ms
71,680 KB
testcase_04 AC 42 ms
62,344 KB
testcase_05 AC 54 ms
66,932 KB
testcase_06 AC 55 ms
68,592 KB
testcase_07 AC 56 ms
65,148 KB
testcase_08 AC 64 ms
72,012 KB
testcase_09 AC 45 ms
61,140 KB
testcase_10 AC 59 ms
71,004 KB
testcase_11 AC 498 ms
102,116 KB
testcase_12 AC 478 ms
99,792 KB
testcase_13 AC 380 ms
93,916 KB
testcase_14 AC 408 ms
99,060 KB
testcase_15 AC 510 ms
101,036 KB
testcase_16 AC 557 ms
104,608 KB
testcase_17 AC 559 ms
104,040 KB
testcase_18 AC 517 ms
104,636 KB
testcase_19 AC 501 ms
101,676 KB
testcase_20 AC 523 ms
102,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!
"""
セグメント木(一般化)
"""
from operator import add
class segment_tree:
    """
    N: 処理する区間の長さ
    """
    def __init__(self, N):
        #演算子および単位元を定義する。
        # max, min, add,ラムダ式,関数定義...
        self.op = add
        self.UNIT = 0
        
        
        self.N0 = 2**(N-1).bit_length()
        self.tree = [self.UNIT]*(2*self.N0)
        
    # a_k の値を x に更新
    def update(self, k,x):
        k += self.N0-1
        self.tree[k] = x
        while k >= 0:
            k = (k - 1) // 2
            self.tree[k] = self.op(self.tree[2*k+1], self.tree[2*k+2])
    # 区間[l,r]をopでまとめる
    def query(self, l,r):
        L = l + self.N0; R = r + self.N0 + 1 
        s = self.UNIT
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(s, self.tree[R-1])
            if L & 1:
                s = self.op(s, self.tree[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s
    def get(self, k): #k番目の値を取得。query[k,k]と同じ
        return self.tree[k+self.N0-1]

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

n,q = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]
ilrx = [[int(i) for i in readline().split()]+[i] for i in range(q)]


seg = segment_tree(n)
num = segment_tree(n)

from operator import itemgetter
ilrx.sort(key = itemgetter(3), reverse=True)

ans = [0]*q
idx = 0

z = sorted(range(n), key=lambda i: a[i], reverse=True)
for i,l,r,x,j in ilrx:
    l -= 1
    r -= 1
    while idx < n and a[z[idx]]>x:
        seg.update(z[idx],a[z[idx]])
        num.update(z[idx],1)
        idx += 1
    ans[j] = seg.query(l,r)-x*num.query(l,r)

print(*ans,sep="\n")




0