結果

問題 No.789 範囲の合計
ユーザー toyuzukotoyuzuko
提出日時 2020-05-17 13:30:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,115 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 12,116 KB
実行使用メモリ 46,852 KB
最終ジャッジ日時 2023-10-25 10:09:30
合計ジャッジ時間 8,284 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 31 ms
10,308 KB
testcase_02 RE -
testcase_03 AC 535 ms
19,396 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 540 ms
19,652 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 30 ms
10,308 KB
testcase_14 AC 30 ms
10,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree():  #1-indexed
    def __init__(self, n):
        self.n = n
        self.tree = [0 for _ in range(n + 1)]

    def sum(self, idx):
        res = 0
        while idx:
            res += self.tree[idx]
            idx -= idx & -idx
        return res

    def add(self, idx, x):
        while idx <= self.n:
            self.tree[idx] += x
            idx += idx & -idx

    def bisect_left(self, x):
        if x <= 0: return 0
        res, tmp = 0, 2**((self.n).bit_length() - 1)
        while tmp:
            if res + tmp <= self.n and self.tree[res + tmp] < x:
                x -= self.tree[res + tmp]
                res += tmp
            tmp >>= 1
        return res + 1

N = int(input())
Q = []
V = set()

for i in range(N):
    q, x, y = map(int, input().split())
    if q == 0:
        V.add(x)
    else:
        V.add(x)
        V.add(y)
    Q.append((q, x, y))

comp = {v : k for k, v in enumerate(V, 1)}
res = 0
bit = BinaryIndexedTree(N)

for q, x, y in Q:
    if q == 0:
        bit.add(comp[x], y)
    else:
        res += bit.sum(comp[y]) - bit.sum(comp[x] - 1)

print(res)
0