/* -*- coding: utf-8 -*- * * 694.cc: No.694 square1001 and Permutation 3 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 500000; /* typedef */ /* global variables */ int as[MAX_N], bs[MAX_N], tmp[MAX_N]; /* subroutines */ int merge_sort(int as[], int tmp[], int l, int r) { // [l, r) int ic = 0; if (l + 1 < r) { int m = (l + r) / 2; ic = merge_sort(as, tmp, l, m) + merge_sort(as, tmp, m, r); int i = l, j = m, k = l; while (i < m && j < r) { if (as[i] <= as[j]) tmp[k++] = as[i++]; else tmp[k++] = as[j++], ic += m - i; } while (i < m) tmp[k++] = as[i++]; while (j < r) tmp[k++] = as[j++]; for (int i = l; i < r; i++) as[i] = tmp[i]; } return ic; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &as[i]), bs[i] = as[i]; int ic = merge_sort(as, tmp, 0, n); //printf("ic=%d\n", ic); //for (int i = 0; i < n * 2; i++) printf("%d ", bs[i]); putchar('\n'); for (int i = 0; i < n; i++) { printf("%d\n", ic); int &bi = bs[i]; int lb = lower_bound(as, as + n, bi) - as; int ub = upper_bound(as, as + n, bi) - as; ic -= lb; ic += (n - ub); } return 0; }