結果

問題 No.789 範囲の合計
ユーザー stngstng
提出日時 2022-06-25 18:34:55
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 349 ms / 1,000 ms
コード長 897 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 115,012 KB
最終ジャッジ日時 2023-08-09 11:19:11
合計ジャッジ時間 5,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,360 KB
testcase_01 AC 70 ms
71,188 KB
testcase_02 AC 349 ms
109,428 KB
testcase_03 AC 265 ms
92,704 KB
testcase_04 AC 322 ms
110,940 KB
testcase_05 AC 288 ms
106,164 KB
testcase_06 AC 310 ms
109,244 KB
testcase_07 AC 245 ms
92,744 KB
testcase_08 AC 285 ms
115,012 KB
testcase_09 AC 279 ms
111,136 KB
testcase_10 AC 347 ms
100,296 KB
testcase_11 AC 300 ms
111,200 KB
testcase_12 AC 279 ms
111,248 KB
testcase_13 AC 69 ms
71,428 KB
testcase_14 AC 67 ms
71,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

n = int(input())
qry = []
idx = []
for i in range(n):
    q = [int(i) for i in input().split()]
    qry.append(q)
    if q[0] == 0:
        idx.append(q[1])

asrt = sorted(set(idx))
adt = { v: i for i, v in enumerate(asrt) }

ki = Bit(len(adt)+1)
ans = 0
#print(bisect.bisect_left(asrt,5)+1)
#print(asrt)
for i in range(n):
    q,x,y = qry[i]
    if q == 0:
        ki.add(adt[x]+1,y)
    else:
        l = bisect.bisect_left(asrt,x)
        r = bisect.bisect_right(asrt,y)
        ans += ki.sum(r) - ki.sum(l)
        #print(ans,l,r)

print(ans)
0