結果

問題 No.789 範囲の合計
ユーザー toyuzukotoyuzuko
提出日時 2020-05-17 13:31:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,123 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 11,972 KB
実行使用メモリ 48,412 KB
最終ジャッジ日時 2023-10-25 10:11:40
合計ジャッジ時間 9,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 29 ms
10,248 KB
testcase_02 WA -
testcase_03 AC 558 ms
20,840 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 557 ms
21,280 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 26 ms
10,248 KB
testcase_14 AC 25 ms
10,248 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, 2)}
res = 0
bit = BinaryIndexedTree(3 * N + 1)

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