結果

問題 No.789 範囲の合計
ユーザー toyuzukotoyuzuko
提出日時 2020-05-17 13:35:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 819 ms / 1,000 ms
コード長 1,132 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 12,068 KB
実行使用メモリ 49,676 KB
最終ジャッジ日時 2023-10-25 10:16:09
合計ジャッジ時間 10,037 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,232 KB
testcase_01 AC 27 ms
10,228 KB
testcase_02 AC 764 ms
42,612 KB
testcase_03 AC 507 ms
19,996 KB
testcase_04 AC 753 ms
42,240 KB
testcase_05 AC 716 ms
42,128 KB
testcase_06 AC 754 ms
42,344 KB
testcase_07 AC 530 ms
20,468 KB
testcase_08 AC 618 ms
31,920 KB
testcase_09 AC 611 ms
31,436 KB
testcase_10 AC 819 ms
49,676 KB
testcase_11 AC 690 ms
41,984 KB
testcase_12 AC 690 ms
41,984 KB
testcase_13 AC 26 ms
10,228 KB
testcase_14 AC 27 ms
10,228 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(sorted(V), 2)}
res = 0
bit = BinaryIndexedTree(2 * 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