結果
問題 | No.789 範囲の合計 |
ユーザー | convexineq |
提出日時 | 2021-03-19 03:10:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 373 ms / 1,000 ms |
コード長 | 1,737 bytes |
コンパイル時間 | 271 ms |
コンパイル使用メモリ | 87,084 KB |
実行使用メモリ | 101,664 KB |
最終ジャッジ日時 | 2023-08-10 23:42:52 |
合計ジャッジ時間 | 5,185 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 72 ms
71,456 KB |
testcase_01 | AC | 71 ms
70,976 KB |
testcase_02 | AC | 373 ms
98,076 KB |
testcase_03 | AC | 252 ms
88,516 KB |
testcase_04 | AC | 358 ms
99,432 KB |
testcase_05 | AC | 312 ms
97,964 KB |
testcase_06 | AC | 314 ms
97,872 KB |
testcase_07 | AC | 253 ms
88,472 KB |
testcase_08 | AC | 299 ms
101,664 KB |
testcase_09 | AC | 285 ms
98,824 KB |
testcase_10 | AC | 366 ms
93,408 KB |
testcase_11 | AC | 313 ms
98,788 KB |
testcase_12 | AC | 302 ms
98,688 KB |
testcase_13 | AC | 71 ms
71,172 KB |
testcase_14 | AC | 71 ms
71,156 KB |
ソースコード
class segment_tree: __slots__ = ["op_M", "e_M","N","N0","dat"] def __init__(self, N, operator_M, e_M): self.op_M = operator_M self.e_M = e_M self.N = N self.N0 = 1<<(N-1).bit_length() self.dat = [self.e_M]*(2*self.N0) # 長さNの配列 initial で初期化 def build(self, initial): assert self.N == len(initial) self.dat[self.N0:self.N0+len(initial)] = initial[:] for k in range(self.N0-1,0,-1): self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1]) # a_k の値を x に更新 def update(self,k,x): k += self.N0 self.dat[k] = x k >>= 1 while k: self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1]) k >>= 1 # 区間[L,R]をopでまとめる def query(self,L,R): L += self.N0; R += self.N0 + 1 sl = sr = self.e_M while L < R: if R & 1: R -= 1 sr = self.op_M(self.dat[R],sr) if L & 1: sl = self.op_M(sl,self.dat[L]) L += 1 L >>= 1; R >>= 1 return self.op_M(sl,sr) def get(self, k): #k番目の値を取得。query[k,k]と同じ return self.dat[k+self.N0] n = int(input()) query = [list(map(int,input().split())) for _ in range(n)] s = set() for i,a,b in query: if i==0: s.add(a) s.add(b) s = sorted(s) from operator import add from bisect import bisect_left,bisect_right ans = 0 seg = segment_tree(len(s), add, 0) for i,a,b in query: if i==0: j = bisect_left(s,a) seg.update(j,seg.get(j)+b) else: ans += seg.query(bisect_left(s,a),bisect_right(s,b)-1) print(ans)