結果

問題 No.877 Range ReLU Query
ユーザー neterukunneterukun
提出日時 2019-09-06 22:40:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,004 ms / 2,000 ms
コード長 1,935 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 82,916 KB
実行使用メモリ 110,980 KB
最終ジャッジ日時 2024-04-25 22:04:51
合計ジャッジ時間 11,035 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,000 KB
testcase_01 AC 69 ms
73,028 KB
testcase_02 AC 65 ms
72,532 KB
testcase_03 AC 80 ms
76,572 KB
testcase_04 AC 43 ms
60,580 KB
testcase_05 AC 56 ms
67,548 KB
testcase_06 AC 59 ms
68,476 KB
testcase_07 AC 53 ms
66,600 KB
testcase_08 AC 76 ms
76,168 KB
testcase_09 AC 43 ms
60,448 KB
testcase_10 AC 68 ms
71,808 KB
testcase_11 AC 892 ms
110,032 KB
testcase_12 AC 806 ms
110,420 KB
testcase_13 AC 664 ms
99,560 KB
testcase_14 AC 708 ms
100,476 KB
testcase_15 AC 1,004 ms
110,256 KB
testcase_16 AC 897 ms
108,512 KB
testcase_17 AC 944 ms
110,980 KB
testcase_18 AC 910 ms
108,276 KB
testcase_19 AC 870 ms
110,176 KB
testcase_20 AC 930 ms
110,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from operator import itemgetter 


class SegmentTree():
    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する
    update: i番目をvalに変更する
    get_sum: 区間[begin, end)の和を求める
    """
    def __init__(self, n):
        self.n = n
        self.size = 1
        while self.size < n:
            self.size *= 2
        self.node = [0] * (2*self.size - 1)

    def update(self, i, val):
        i += (self.size - 1)
        self.node[i] = val
        while i > 0:
            i = (i - 1) // 2
            self.node[i] = self.node[2*i + 1] + self.node[2*i + 2]

    def get_sum(self, begin, end):
        begin += (self.size - 1)
        end += (self.size - 1)
        s = 0
        while begin < end:
            if (end - 1) & 1:
                end -= 1
                s = s + self.node[end]
            if (begin - 1) & 1:
                s = s + self.node[begin]
                begin += 1
            begin = (begin - 1) // 2
            end = (end - 1) // 2
        return s


n, q = map(int, input().split())
a = list(map(int, input().split()))
info = [[i] + list(map(int, input().split())) for i in range(q)]

ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

st = SegmentTree(n)
st_cnt = SegmentTree(n)

a = sorted(zip(a, range(len(a))))
info = sorted(info, key = itemgetter(4))

minus = 0
ind = 0
ans = []
for i in range(q):
    j, _, l, r, val = info[i]
    if minus < val:
        minus = val
        while True:
            if ind == n:
                break
            if a[ind][0] <= minus:
                st.update(a[ind][1], a[ind][0])
                st_cnt.update(a[ind][1], 1)
                ind += 1
            else:
                break
    ans.append((j, (ruiseki[r] - ruiseki[l-1]) - st.get_sum(l-1, r) - minus * (r-l+1-st_cnt.get_sum(l-1, r))))
ans = sorted(ans)
for i in range(q):
    print(ans[i][1])
0