結果

問題 No.877 Range ReLU Query
ユーザー simansiman
提出日時 2022-11-09 06:18:49
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,001 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 11,308 KB
実行使用メモリ 44,348 KB
最終ジャッジ日時 2023-09-29 18:48:21
合計ジャッジ時間 13,646 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,060 KB
testcase_01 AC 85 ms
15,112 KB
testcase_02 AC 82 ms
15,280 KB
testcase_03 AC 83 ms
15,360 KB
testcase_04 AC 78 ms
15,116 KB
testcase_05 AC 79 ms
15,032 KB
testcase_06 AC 80 ms
15,144 KB
testcase_07 AC 79 ms
15,040 KB
testcase_08 AC 83 ms
15,336 KB
testcase_09 AC 78 ms
15,252 KB
testcase_10 AC 79 ms
15,112 KB
testcase_11 AC 956 ms
40,096 KB
testcase_12 AC 838 ms
39,400 KB
testcase_13 AC 689 ms
32,336 KB
testcase_14 AC 702 ms
34,420 KB
testcase_15 AC 1,001 ms
40,016 KB
testcase_16 AC 928 ms
43,680 KB
testcase_17 AC 971 ms
44,348 KB
testcase_18 AC 946 ms
43,952 KB
testcase_19 AC 902 ms
40,724 KB
testcase_20 AC 945 ms
40,768 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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
0