結果

問題 No.789 範囲の合計
ユーザー UekiUeki
提出日時 2019-08-01 23:20:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 966 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 106,368 KB
最終ジャッジ日時 2023-09-18 18:06:38
合計ジャッジ時間 4,248 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
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)
        ans += range_sum(xl, xr+1)
print(ans)
0