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] + 1) bit.add(self[i], 1) end cnt end end 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 = N.times.map { gets.to_i } B = A.uniq.sort memo = Hash.new B.each.with_index(1) do |b, i| memo[b] = i end C = A.map { |a| memo[a] } counter = C.tally RUI = [0] C.uniq.sort.each do |c| cnt = counter[c] RUI << RUI.last + cnt end # pp C ans = [] cnt = C.inversion_number ans << cnt 0.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 << cnt end puts ans