// yukicoder: No.694 square1001 and Permutation 3 // 2019.7.20 bal4u #include #include typedef long long ll; #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } void out(ll n) { // 非負整数の表示(出力) int i; char b[50]; if (!n) pc('0'); else { i = 0; while (n) b[i++] = n % 10 + '0', n /= 10; while (i--) pc(b[i]); } pc('\n'); } #define MAX 500005 // Bit木 library int bit[MAX]; int nbit; int sum(int i) { int s = 0; while (i > 0) s += bit[i], i -= i & -i; return s; } void add(int i) { while (i <= nbit) bit[i]++, i += i & -i; } typedef struct { int a, id; } T; T t[MAX]; int a[MAX]; int N; int cmp(const void *a, const void *b) { return ((T *)a)->a - ((T *)b)->a; } void compact(int n, int *a) { int i, v; for (i = 0; i < n; i++) t[i].a = a[i], t[i].id = i; qsort(t, n, sizeof(T), cmp); v = 0; a[t[0].id] = v; for (i = 1; i < n; i++) { if (t[i].a != t[i-1].a) v++; a[t[i].id] = v; } } int main() { int i; ll ans; N = in(); for (i = 0; i < N; i++) a[i] = in(); compact(N, a); ans = 0, nbit = N+1; for (i = 0; i < N; i++) { ans += i - sum(a[i]+1); add(a[i]+1); } for (i = 0; i < N; i++) { out(ans); ans -= sum(a[i]); ans += N - sum(a[i]+1); } return 0; }