#P6278. [USACO20OPEN] Haircut G

[USACO20OPEN] Haircut G

Description

Farmer John is tired of trying to straighten his unruly hair, so he decides to get a haircut. He has a row of NN strands of hair, and the ii-th strand initially has length AiA_i micrometers (0AiN0 \le A_i \le N). Ideally, he wants his hair lengths to be monotonically non-decreasing, so he defines the “badness” of his hair as the number of inversions: pairs (i,j)(i, j) such that i<ji < j and Ai>AjA_i > A_j.
For each j=0,1,,N1j = 0, 1, \ldots, N - 1, Farmer John wants to know the badness of his hair if all strands with length greater than jj are reduced to length jj.


(Fun fact: Humans really do have about 10510^5 hairs on average!)

Input Format

The first line contains NN.
The second line contains A1,A2,,ANA_1, A_2, \ldots, A_N.

Output Format

For each j=0,1,,N1j = 0, 1, \ldots, N - 1, output on a separate line the badness of Farmer John’s hair.


Note that the integer values in this problem may require 64-bit integer storage (for example, “long long” in C/C++).

5
5 2 3 3 0
0
4
4
5
7

Hint

Sample Explanation:

The fourth line of the output describes the number of inversions when Farmer John’s hair lengths are reduced to 33.
A=[3,2,3,3,0]A = [3, 2, 3, 3, 0] has five inversions: A1>A2A_1 > A_2, A1>A5A_1 > A_5, A2>A5A_2 > A_5, A3>A5A_3 > A_5, and A4>A5A_4 > A_5.


For 100%100\% of the testdata, 1N1051 \le N \le 10^5.

There are 1313 test points in total. Test point 11 is the sample, and the others satisfy:

Test point 22 satisfies N100N \le 100.
Test points 353 \sim 5 satisfy N5000N \le 5000.
Test points 6136 \sim 13 have no additional constraints.


Problem author: Dhruv Rohatgi.

Translated by ChatGPT 5