#include using namespace std; typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define PQ priority_queue #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define FOR(it, v) for(auto it = v.begin(); it != v.end(); ++it) #define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it) template bool setmin(T & a, T b) { if(b < a) { a = b; return true; } else { return false; } } template bool setmax(T & a, T b) { if(b > a) { a = b; return true; } else { return false; } } template bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } } template bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- template class SegTree { private: T * a = NULL; int N = -1, S; function F; T I; bool is_available = false; public: SegTree() { } SegTree(int n, function func, T id) { init(n, func, id); } // [0, n) が使用可能になる、func が演算、id は単位元 void init(int size, function func, T id) { assert(size > 0); N = size; F = func; I = id; S = 1; while(S < size) { S *= 2; } delete[] a; a = new T[S * 2]; inc(i, S * 2) { a[i] = I; } is_available = true; } // [p] の値を返す T operator[](int p) { assert(inID(p, 0, N)); p += S; return a[p]; } // [p] への参照を返す(calc() を呼ぶまで fold は使用不能になる) T & ref(int p) { is_available = false; assert(inID(p, 0, N)); p += S; return a[p]; } // テーブル全体を再計算 void calc() { decID(i, 1, S) { a[i] = F(a[i * 2], a[i * 2 + 1]); } is_available = true; } // [p] に op を適用しテーブルを更新 void apply(int p, function op) { assert(inID(p, 0, N)); p += S; op(a[p]); while(p != 1) { p /= 2; a[p] = F(a[p * 2], a[p * 2 + 1]); } } // [l, r) に f を適用した結果を返す T fold_ID(int l, int r, bool loop = false) { assert(is_available); assert(inII(l, 0, N)); assert(inII(r, 0, N)); if(loop && l >= r) { return F(fold_ID(l, N), fold_ID(0, r)); } assert(l <= r); l += S; r += S; T v = I, w = I; while(l < r) { if(l + 1 == r) { v = F(v, a[l]); break; } if(l % 2 == 1) { v = F(v, a[l]); } if(r % 2 == 1) { w = F(a[r - 1], w); } l = (l + 1) / 2; r = r / 2; } return F(v, w); } // [l, r] に f を適用した結果を返す T fold_II(int l, int r, bool loop = false) { return fold_ID(l, r + 1, loop); } }; #define OP(op) [&](auto a, auto b) { return op; } #define AP(op) [&](auto & a) { op; } // ---- ---- const int M = 500000; int n; pair a_[M]; int ir[M], p[M], s[M + 1]; LL ID(int a, int b) { return s[b] - s[a]; } int main() { scanf("%d", & n); inc(i, n) { scanf("%d", & a_[i].FI); a_[i].SE = i; } sort(a_, a_ + n); int R = 0; inc(i, n) { ir[a_[i].SE] = R; if(i == n - 1) { R++; break; } if(a_[i].FI < a_[i + 1].FI) { R++; } } inc(i, n) { p[ir[i]]++; } inc1(i, n) { s[i] = s[i - 1] + p[i - 1]; } SegTree st(R, OP(a + b), 0); LL v = 0; dec(i, n) { v += st.fold_ID(0, ir[i]); st.apply(ir[i], AP(a++)); } inc(i, n) { printf("%lld\n", v); v -= ID(0, ir[i]); v += ID(ir[i] + 1, R); } return 0; }