結果

問題 No.789 範囲の合計
ユーザー UekiUeki
提出日時 2019-08-01 23:21:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 966 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 10,812 KB
実行使用メモリ 31,856 KB
最終ジャッジ日時 2023-09-18 18:06:51
合計ジャッジ時間 3,755 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,292 KB
testcase_01 AC 16 ms
7,928 KB
testcase_02 AC 754 ms
31,856 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 #

import bisect
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N = int(input())
ans = 0
nums = set()
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)

nums = list(sorted(nums))
size = len(nums)
L = [0]*(size+1)


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

    return ret


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


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


for q, l, r in query:
    if q == 0:
        xl = bisect.bisect_left(nums, l)
        add(xl, r)
    else:
        xl = bisect.bisect_left(nums, l)
        xr = bisect.bisect_left(nums, r+1)
        ans += range_sum(xl, xr)
print(ans)
0