結果

問題 No.789 範囲の合計
ユーザー rlangevinrlangevin
提出日時 2023-03-28 16:39:42
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,770 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 134,500 KB
最終ジャッジ日時 2023-10-20 10:47:41
合計ジャッジ時間 5,745 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,460 KB
testcase_01 AC 44 ms
55,460 KB
testcase_02 MLE -
testcase_03 AC 288 ms
102,316 KB
testcase_04 AC 416 ms
124,512 KB
testcase_05 MLE -
testcase_06 MLE -
testcase_07 AC 270 ms
100,668 KB
testcase_08 AC 334 ms
111,836 KB
testcase_09 AC 326 ms
110,128 KB
testcase_10 MLE -
testcase_11 AC 408 ms
124,820 KB
testcase_12 AC 397 ms
124,844 KB
testcase_13 AC 44 ms
55,476 KB
testcase_14 AC 43 ms
55,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Fenwick_Tree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n

    def add(self, p, x):
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s
    
    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x

from bisect import *
from copy import deepcopy
def compress(lst):
    """
    B: lstを座圧したリスト
    D: 元の値からindexを取得する辞書
    vals: indexから元の値を取得するリスト
    """
    B = []
    D = dict()
    vals = deepcopy(lst)
    vals = list(set(vals))
    vals.sort()
    for i in range(len(lst)):
        ind = bisect_left(vals, lst[i])
        B.append(ind)
    for i in range(len(B)):
        D[lst[i]] = B[i]
    return D, vals


N = int(input())
Q = []
data = []
for i in range(N):
    Q.append(list(map(int, input().split())))
    if Q[i][0] == 0:
        data.append(Q[i][1])
    else:
        data.append(Q[i][1])
        data.append(Q[i][2])
        
D, vals = compress(data)
T = Fenwick_Tree(len(vals))
ans = 0
for q, x, y in Q:
    if q == 0:
        x = D[x]
        T.add(x, y)
    else:
        x, y = D[x], D[y]
        ans += T.sum(x, y + 1)
        
print(ans)
    
0