#include #include #include #include #include #include #include #include #include #include #pragma GCC optimize("O3") #pragma comment(linker, "STACK:36777216") using namespace std; using i64 = int64_t; constexpr i64 MOD = 1e9 + 7; using vi = vector; using vvi = vector; using vvvi = vector; i64 mergeCount(vi &a) { i64 cnt = 0; int n = int(a.size()); if (n > 1) { vi b(a.begin(), a.begin() + n / 2); vi c(a.begin() + n / 2, a.end()); cnt += mergeCount(b); cnt += mergeCount(c); for (int i = 0, j = 0, k = 0; i < n; i++) { if (k == c.size()) a[i] = b[j++]; else if (j == b.size()) a[i] = c[k++]; else if (b[j] <= c[k]) a[i] = b[j++]; else { a[i] = c[k++]; cnt += n / 2 - j; } } } return cnt; } class RangeCount { const int ST_SIZE = (1 << 20) - 1; int n; vi data; vvi segtree; void init(int k, int l, int r) { if (r - l == 1) { segtree[k].push_back(data[l]); } else { int lch = k * 2 + 1; int rch = k * 2 + 2; init(lch, l, (l + r) / 2); init(rch, (l + r) / 2, r); segtree[k].resize(r - l); merge(segtree[lch].begin(), segtree[lch].end(), segtree[rch].begin(), segtree[rch].end(), segtree[k].begin()); } } // number of x in [i, j) int query(int i, int j, i64 x, int k, int l, int r) { if (j <= l || r <= i) { return 0; } if (i <= l && r <= j) { return int(upper_bound(segtree[k].begin(), segtree[k].end(), x) - segtree[k].begin()); } int lc = query(i, j, x, k * 2 + 1, l, (l + r) / 2); int rc = query(i, j, x, k * 2 + 2, (l + r) / 2, r); return lc + rc; } public: RangeCount(vi v) { n = int(v.size()); data = vi(v); segtree = vvi(ST_SIZE); init(0, 0, n); } int exact(int i, int j, i64 x) { return query(i, j, x, 0, 0, n) - query(i, j, x - 1, 0, 0, n); } int le(int i, int j, i64 x) { return query(i, j, x, 0, 0, n); } int lt(int i, int j, i64 x) { return query(i, j, x - 1, 0, 0, n); } int ge(int i, int j, i64 x) { return query(i, j, 1e18, 0, 0, n) - query(i, j, x - 1, 0, 0, n); } int gt(int i, int j, i64 x) { return query(i, j, 1e18, 0, 0, n) - query(i, j, x, 0, 0, n); } }; int main() { int n; cin >> n; vi as; for (int i = 0; i < n; i++) { int a; cin >> a; as.push_back(a); } vi bs(as); RangeCount cnt(as); i64 ini = mergeCount(bs); for (int i = 0; i < n; i++) { cout << ini << endl; ini += cnt.gt(0, n, as[i]) - cnt.lt(0, n, as[i]); } }