結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー |
![]() |
提出日時 | 2023-06-27 16:02:28 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 2,727 ms / 3,000 ms |
コード長 | 1,205 bytes |
コンパイル時間 | 242 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 94,056 KB |
最終ジャッジ日時 | 2024-07-04 09:02:27 |
合計ジャッジ時間 | 17,656 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 13 |
コンパイルメッセージ
Syntax OK
ソースコード
class Arraydef inversion_numbern = sizebit = BinaryIndexTree.new(n + 1)cnt = 0n.times do |i|cnt += i - bit.sum(0, self[i] + 1)bit.add(self[i], 1)endcntendendclass 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 & -idxendresendendN = gets.to_iA = N.times.map { gets.to_i }B = A.uniq.sortmemo = Hash.newB.each.with_index(1) do |b, i|memo[b] = iendC = A.map { |a| memo[a] }counter = C.tallyRUI = [0]C.uniq.sort.each do |c|cnt = counter[c]RUI << RUI.last + cntend# pp Cans = []cnt = C.inversion_numberans << cnt0.upto(N - 2) do |i|c = C[i]small_num = RUI[c] - counter[c]big_num = N - small_num - counter[c]cnt += (big_num - small_num)ans << cntendputs ans