結果

問題 No.789 範囲の合計
ユーザー 👑 rin204rin204
提出日時 2022-09-30 23:00:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 873 ms / 1,000 ms
コード長 1,286 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 47,816 KB
最終ジャッジ日時 2023-08-24 16:34:15
合計ジャッジ時間 9,018 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,304 KB
testcase_01 AC 16 ms
8,336 KB
testcase_02 AC 838 ms
40,796 KB
testcase_03 AC 453 ms
16,508 KB
testcase_04 AC 792 ms
40,412 KB
testcase_05 AC 745 ms
40,152 KB
testcase_06 AC 775 ms
40,488 KB
testcase_07 AC 422 ms
17,132 KB
testcase_08 AC 627 ms
29,424 KB
testcase_09 AC 591 ms
29,116 KB
testcase_10 AC 873 ms
47,816 KB
testcase_11 AC 732 ms
40,084 KB
testcase_12 AC 709 ms
40,040 KB
testcase_13 AC 16 ms
8,340 KB
testcase_14 AC 16 ms
8,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.n0 = 1 << (n.bit_length() - 1)
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
         
    def lower_bound(self, x):
        pos = 0
        plus = self.n0
        while plus > 0:
            if pos + plus <= self.size and self.tree[pos + plus] < x:
                x -= self.tree[pos + plus]
                pos += plus
            plus //= 2
        return pos

n = int(input())
query = []
X = set()
for _ in range(n):
    t, x, y = map(int, input().split())
    X.add(x)
    if t == 1:
        y += 1
        X.add(y)
    query.append((t, x, y))

n = len(X)
lst = sorted(X)
dic = {l:i for i, l in enumerate(lst)}
bit = Bit(n)

ans = 0
for t, x, y in query:
    x = dic[x]
    if t == 0:
        bit.add(x, y)
    else:
        y = dic[y]
        ans += bit.range_sum(x, y)
print(ans)
0