#include #include #include #define int long long using namespace std; typedef pair P; struct BIT { int a[500001]; BIT() { for (int i = 0; i <= 500000; i++) a[i] = 0; } void add(int pos, int w) { for (int i = pos; i <= 500000; i += i & -i) a[i] += w; } int sum(int pos) { int ret = 0; for (int i = pos; i > 0; i -= i & -i) ret += a[i]; return ret; } }; int n; int a[500000]; P valPos[500000]; BIT existFlag; int sorted_a[500000]; signed main() { int i, j; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; valPos[i] = P(a[i], i); existFlag.add(i + 1, 1); } sort(valPos, valPos + n); int prevVal = -1; int ans = 0; for (i = 0; i < n; i++) { if (prevVal != valPos[i].first) { for (j = i; j < n; j++) { if (valPos[i].first != valPos[j].first) break; existFlag.add(valPos[j].second + 1, -1); } prevVal = valPos[i].first; } int hanten_number = existFlag.sum(valPos[i].second); ans += hanten_number; } cout << ans << endl; //差分計算 for (i = 0; i < n; i++) sorted_a[i] = a[i]; sort(sorted_a, sorted_a + n); for (i = 0; i < n - 1; i++) { int minus = lower_bound(sorted_a, sorted_a + n, a[i]) - sorted_a; //a[i]未満の要素の個数 int plus = n - (upper_bound(sorted_a, sorted_a + n, a[i]) - sorted_a); //a[i]を超える要素の個数 ans -= minus; ans += plus; cout << ans << endl; } return 0; }