結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-11-25 02:59:09 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 525 ms / 1,000 ms |
| コード長 | 1,111 bytes |
| コンパイル時間 | 242 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 28,288 KB |
| 最終ジャッジ日時 | 2024-10-01 14:38:26 |
| 合計ジャッジ時間 | 7,355 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
コンパイルメッセージ
Syntax OK
ソースコード
class BinaryIndexTree
def initialize(size, init: 0)
@values = Array.new(size, init)
@size = size
end
# @param idx [Integer]
# @param x [Numeric]
def add(idx, x)
raise 'Out of range reference' if @size <= idx
idx += 1
while idx <= @size
@values[idx - 1] += x
idx += idx & -idx
end
end
def sum(l, r)
_sum(r) - _sum(l)
end
private
def _sum(idx)
res = 0
while idx > 0
res += @values[idx - 1]
idx -= idx & -idx
end
res
end
end
N = gets.to_i
Q = N.times.map { gets.split.map(&:to_i) }
memo = Hash.new
values = []
Q.select { |t, _, _| t == 0 }.each do |t, x, y|
values << x
end
values << Float::INFINITY
values.uniq!
values.sort!
values.each.with_index(1) do |v, idx|
memo[v] = idx
end
bit = BinaryIndexTree.new(N + 2)
ans = 0
N.times do |i|
if Q[i][0] == 0
_, x, y = Q[i]
idx = memo[x]
bit.add(idx, y)
else
_, lv, rv = Q[i]
l = values.bsearch_index { |v| v >= lv }
r = values.bsearch_index { |v| v > rv }
l += 1
ans += bit.sum(0, r + 1) - bit.sum(0, l)
end
end
puts ans
siman