結果

問題 No.365 ジェンガソート
ユーザー simansiman
提出日時 2020-10-01 18:28:35
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 710 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 11,580 KB
実行使用メモリ 22,556 KB
最終ジャッジ日時 2023-09-21 07:02:14
合計ジャッジ時間 9,241 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,240 KB
testcase_01 AC 83 ms
15,332 KB
testcase_02 AC 82 ms
15,152 KB
testcase_03 AC 81 ms
14,988 KB
testcase_04 AC 85 ms
15,088 KB
testcase_05 AC 82 ms
15,116 KB
testcase_06 AC 81 ms
15,156 KB
testcase_07 AC 82 ms
15,312 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 82 ms
15,028 KB
testcase_15 AC 83 ms
14,984 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 97 ms
15,532 KB
testcase_19 AC 308 ms
21,692 KB
testcase_20 AC 158 ms
17,092 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 323 ms
22,556 KB
testcase_37 AC 323 ms
22,332 KB
testcase_38 WA -
testcase_39 AC 321 ms
22,480 KB
testcase_40 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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