結果

問題 No.789 範囲の合計
ユーザー UekiUeki
提出日時 2019-08-01 22:52:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,297 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 50,624 KB
最終ジャッジ日時 2023-09-18 18:04:55
合計ジャッジ時間 4,838 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
12,580 KB
testcase_01 AC 17 ms
8,344 KB
testcase_02 AC 809 ms
50,624 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class BIT:
    """
    Binary Index Tree

    add :L[i]+=x
    sum: sum(L[:l])
    range_sum : sum(L[l:r])
    """

    def __init__(self, N, init_val=0):
        self.size = N
        self.L = [init_val]*(N+1)

    def sum(self, r):
        """
        calc sum from L[0] to L[r-1]
        """
        ret = 0
        while r > 0:
            ret += self.L[r]
            r -= r & (-r)

        return ret

    def range_sum(self, l, r):
        """
        calc sum from L[l] to L[r-1]
        """
        return self.sum(r)-self.sum(l)

    def add(self, i, x):
        """
        L[i] += x
        """
        while i < self.size:
            self.L[i] += x
            i += i & (-i)


N = int(input())
ans = 0
nums = set()
cnt = 0
query = []
for _ in range(N):
    q, l, r = list(map(int, input().split()))
    query.append([q, l, r])

    if q == 0:
        nums.add(l)
    else:
        nums.add(l)
        nums.add(r+1)

num2Id = {}
Id2Num = {}
for i, n in enumerate(list(sorted(nums))):
    num2Id[n] = i
    Id2Num[i] = n

Bit = BIT(len(nums))

for q, l, r in query:
    if q == 0:
        Bit.add(num2Id[l], r)
    else:
        ans += Bit.range_sum(num2Id[l], num2Id[r+1])
print(ans)
0