W=[0]*10**6 def bit(a,n) r=0 i=n-1 while i>=0 idx=a[i];(r+=W[idx];idx&=idx-1)while idx>0 idx=a[i];(W[idx]+=1;idx+=idx&-idx)while idxa[j-1] : i<=>j} x=bit(z,z.size) b=a.sort n.times{|i| p x x-=((0...n).bsearch{|j|a[i]<=b[j]}||n)-n+((0...n).bsearch{|j|a[i]