void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=new int[](n); foreach(i; 0..n) rd(a[i]); int[int] compress(int[] arr){ int[int] ret; sort(arr); foreach(elem; arr){ if(elem in ret) ret[elem]++; else ret[elem]=ret.length.to!(int); } return ret; } auto map=compress(a.dup); auto b=new int[](n); foreach(i; 0..n) b[i]=map[a[i]]; auto freq=new int[](n); foreach(elem; b) freq[elem]+=1; auto lb=new long[](n), ub=new long[](n); // lb[i]:=sum(freq[0, i)), ub[i]:=sum(freq(i, n)) foreach(i; 1..n) lb[i]=lb[i-1]+freq[i-1]; foreach_reverse(i; 1..n) ub[i-1]=ub[i]+freq[i]; struct Pair{int val, idx;} auto arr=new Pair[](n); foreach(i; 0..n) arr[i]=Pair(b[i], i); sort!"a.val==b.val ? a.idx>b.idx : a.val>b.val"(arr); auto tree=new SquareRootDecomposition(n); long invNum=0; // writeln(arr); foreach(e; arr){ int i=e.idx, v=e.val; invNum+=tree.sum(0, i); tree.add(i, 1L); // writeln(invNum); } // writeln(invNum); foreach(e; b){ writeln(invNum); invNum-=lb[e]; invNum+=ub[e]; } } /* 25143 => 5 51432 => 5-1+3=7 14325 => 7-4+0=3 43251 => 3-0+4=7 32514 => 7-3+1=5 */ class SquareRootDecomposition{ import std.algorithm; int D=1; long[] val, buc; this(int n){ while(D*D