This page is still under construction.

Parts of this page are still being built. What you see may change.

Haircut

Time limit1sMemory limit512 MB

Summary
For each threshold j from 0 to N-1, clip every value above j down to j and count the resulting inversions.
Level

Hard8 of 10

Topics
Sorting, Prefix sum, Math, Implementation
Solved
No attempts yet

Problem

Tired of his stubborn cowlick, Farmer John decides to get a haircut. He has NN (1≤N≤1051\le N\le 10^5) strands of hair arranged in a line, and strand ii is initially AiA_i micrometers long (0≤Ai≤N0\le A_i\le N). Ideally, he wants his hair to be monotonically increasing in length, 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 of j=0,1,…,N−1j=0,1,\ldots,N-1, FJ would like to know the badness of his hair if all strands with length greater than jj are decreased to length exactly jj.

(Fun fact: the average human head does indeed have about 10510^5 hairs!)

Input

The first line contains NN.

The second line contains A1,A2,…,AN.A_1,A_2,\ldots,A_N.

Output

For each of j=0,1,…,N−1j=0,1,\ldots,N-1, output the badness of FJ's hair on a new line.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

Hint

The fourth line of output describes the number of inversions when FJ's hairs are decreased to length 3. Then A=[3,2,3,3,0]A=[3,2,3,3,0] has five inversions: A1>A2, A1>A5, A2>A5, A3>A5,A_1>A_2,\,A_1>A_5,\,A_2>A_5,\,A_3>A_5, and A4>A5A_4>A_5.

Examples1

  1. Example 1

    Input
    5
    5 2 3 3 0
    
    Expected output
    0
    4
    4
    5
    7