結果
問題 | No.1115 二つの数列 / Two Sequences |
ユーザー |
![]() |
提出日時 | 2022-03-22 13:34:14 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 364 ms / 2,000 ms |
コード長 | 940 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 7,552 KB |
実行使用メモリ | 31,232 KB |
最終ジャッジ日時 | 2024-10-10 04:49:52 |
合計ジャッジ時間 | 10,461 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 35 |
コンパイルメッセージ
Syntax OK
ソースコード
class BinaryIndexTreedef initialize(size, init: 0)@values = Array.new(size, init)@size = sizeend# @param idx [Integer]# @param x [Numeric]def add(idx, x)raise 'Out of range reference' if @size <= idxidx += 1while idx <= @size@values[idx - 1] += xidx += idx & -idxendenddef sum(l, r)_sum(r) - _sum(l)endprivatedef _sum(idx)res = 0while idx > 0res += @values[idx - 1]idx -= idx & -idxendresendendclass Arraydef inversion_numbern = sizebit = BinaryIndexTree.new(n + 1)cnt = 0n.times do |i|cnt += i - bit.sum(0, self[i])bit.add(self[i], 1)endcntendendN = gets.to_iA = gets.split.map(&:to_i)B = gets.split.map(&:to_i)idx_table = Hash.newB.each_with_index do |b, idx|idx_table[b] = idx + 1endnums = A.map { |a| idx_table[a] }puts nums.inversion_number