結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,220 KB
testcase_01 AC 29 ms
10,216 KB
testcase_02 AC 689 ms
42,664 KB
testcase_03 AC 390 ms
19,984 KB
testcase_04 AC 671 ms
42,292 KB
testcase_05 AC 615 ms
42,180 KB
testcase_06 AC 647 ms
42,396 KB
testcase_07 AC 398 ms
20,456 KB
testcase_08 AC 509 ms
31,972 KB
testcase_09 AC 481 ms
31,488 KB
testcase_10 AC 778 ms
49,724 KB
testcase_11 AC 618 ms
42,036 KB
testcase_12 AC 603 ms
42,036 KB
testcase_13 AC 30 ms
10,216 KB
testcase_14 AC 29 ms
10,216 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

import sys
input = sys.stdin.readline

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