結果
問題 | No.877 Range ReLU Query |
ユーザー |
![]() |
提出日時 | 2022-11-09 06:18:49 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 1,150 ms / 2,000 ms |
コード長 | 1,114 bytes |
コンパイル時間 | 125 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 44,288 KB |
最終ジャッジ日時 | 2024-07-22 12:52:20 |
合計ジャッジ時間 | 13,876 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
コンパイルメッセージ
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, Q = gets.split.map(&:to_i) A = gets.split.map.with_index(1) { |v, i| [i, v.to_i] } A.sort_by! { |_, a| -a } queries = Q.times.map { |i| [i] + gets.split.map(&:to_i) } queries.sort_by! { |_, _, _, _, x| -x } ans = Array.new(Q, 0) cnt = 0 bit_sum = BinaryIndexTree.new(N + 2) bit_cnt = BinaryIndexTree.new(N + 2) queries.each do |i, _, l, r, x| while A.size >= 1 && A[0][-1] >= x idx, a = A.shift bit_sum.add(idx, a) bit_cnt.add(idx, 1) end cnt = bit_cnt.sum(l, r + 1) sum = bit_sum.sum(l, r + 1) ans[i] = sum - cnt * x # pp [:cnt, cnt, :l, l, :r, r] end puts ans