結果

問題 No.789 範囲の合計
ユーザー rlangevinrlangevin
提出日時 2023-03-28 16:54:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,622 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 11,960 KB
実行使用メモリ 33,984 KB
最終ジャッジ日時 2023-10-20 11:00:44
合計ジャッジ時間 12,346 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,360 KB
testcase_01 AC 33 ms
10,360 KB
testcase_02 TLE -
testcase_03 AC 731 ms
18,216 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 716 ms
18,808 KB
testcase_08 AC 918 ms
25,644 KB
testcase_09 AC 891 ms
25,308 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 31 ms
10,360 KB
testcase_14 AC 32 ms
10,360 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 = []
    vals = deepcopy(lst)
    vals = list(set(vals))
    vals.sort()
    for i in range(len(lst)):
        ind = bisect_left(vals, lst[i])
        B.append(ind)
    return B, vals


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