結果

問題 No.789 範囲の合計
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-15 22:09:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 796 ms / 1,000 ms
コード長 1,484 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 11,992 KB
実行使用メモリ 49,744 KB
最終ジャッジ日時 2023-10-20 06:49:34
合計ジャッジ時間 7,746 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,260 KB
testcase_01 AC 28 ms
10,260 KB
testcase_02 AC 714 ms
42,676 KB
testcase_03 AC 295 ms
18,492 KB
testcase_04 AC 678 ms
42,312 KB
testcase_05 AC 621 ms
42,208 KB
testcase_06 AC 647 ms
42,408 KB
testcase_07 AC 277 ms
18,884 KB
testcase_08 AC 473 ms
30,876 KB
testcase_09 AC 451 ms
30,992 KB
testcase_10 AC 796 ms
49,744 KB
testcase_11 AC 634 ms
42,048 KB
testcase_12 AC 623 ms
42,048 KB
testcase_13 AC 29 ms
10,260 KB
testcase_14 AC 28 ms
10,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


n = int(input())
A = set()
A.add(0)
A.add(10 ** 9)
query = []
for _ in range(n):
    k, x, y = map(int, input().split())
    if k == 0:
        A.add(x)
    else:
        A.add(x)
        A.add(y)
    query.append((k, x, y))

sz = len(A)
atoi = {a: i for i, a in enumerate(sorted(A))}
bit = fenwick_tree(sz)

ans = 0
for k, x, y in query:
    if k == 0:
        i = atoi[x]
        bit.add(i, y)
    else:
        l = atoi[x]
        r = atoi[y] + 1
        ans += bit.sum(l, r)
print(ans)
0