結果

問題 No.365 ジェンガソート
ユーザー siman
提出日時 2020-10-01 18:28:35
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 710 bytes
コンパイル時間 34 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-07-07 01:29:17
合計ジャッジ時間 8,499 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 WA * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 = gets.to_i
A = gets.split.map(&:to_i)
M = A.max
bit = BinaryIndexTree.new(M + 2)

ans = 0

A.each do |a|
  if bit.sum(a + 1, M + 1) > 0
    ans += 1
  end

  bit.add(a, 1)
end

puts ans
0