#include "bits/stdc++.h" using namespace std; #define DEBUG(x) cout<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define fi first #define se second #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep1(i,n) for(ll i=1;i<=(ll)(n);i++) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define rrep1(i,n) for(ll i=(ll)(n);i>0;i--) #define REP(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) #define in(x, a, b) (a <= x && x < b) #define all(v) v.begin(),v.end() #define UNIQUE(v) v.erase(unique(all(v)), v.end()) template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } const ll inf = 1000000001; const ll INF = 2e18; const ll MOD = 1000000007; const double pi = 3.14159265358979323846; #define Sp(p) cout< 0) { s += bit[i - 1]; i -= i & -i; } return s; } //iは0-index void add(int i, ll x) { i++; while (i <= n) { bit[i - 1] += x; i += i & -i; } } // bitに入ってる中でx番目に小さい数を求める // 1番小さい数はx = 1; // bitに3を入れる→add(3, 1), 3を出す→add(3, -1) // return == nの場合x個も入っていない ll min_xth(int x) { int left = -1, right = n; while (left + 1 < right) { int mid = (left + right) / 2; int temp = sum(mid + 1); if (temp < x) { left = mid; } else { right = mid; } } return right; } // 0,1,...,n - 1を並び替えた順列をバブルソートで // 順番通りにするための交換回数 ll bubble(vi p) { int n = p.size(); ll ans = 0; for (int j = 0; j < n; j++) { ans += (j - sum(p[j] + 1)); add(p[j], 1); } return ans; } }; int main() { int n; cin >> n; vi a(n); rep(i, n) { cin >> a[i]; } vi b = a; sort(all(b)); map mp; int cnt = 0; rep(i, n) { if (mp[b[i]] == 0) { mp[b[i]] = ++cnt; } } vi low(cnt), high(cnt); int i = 0; while (i < n) { low[mp[b[i]] - 1] = i; int j = i; while (j < n && b[j] == b[i]) { j++; } high[mp[b[i]] - 1] = n - j; i = j; } rep(i, n) { a[i] = mp[a[i]] - 1; } Bit bit(cnt); ll res = bit.bubble(a); rep(i, n) { cout << res << endl; res = res - low[a[i]] + high[a[i]];; } }