結果
| 問題 |
No.742 にゃんにゃんにゃん 猫の挨拶
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-05-03 13:03:50 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 146 ms / 2,500 ms |
| コード長 | 798 bytes |
| コンパイル時間 | 549 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 12,672 KB |
| 最終ジャッジ日時 | 2024-07-02 13:21:55 |
| 合計ジャッジ時間 | 3,299 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
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
class Array
def inversion_number
n = size
bit = BinaryIndexTree.new(n + 1)
cnt = 0
n.times do |i|
cnt += i - bit.sum(0, self[i])
bit.add(self[i], 1)
end
cnt
end
end
N = gets.to_i
M = N.times.map { gets.to_i }
puts M.inversion_number
siman