Under construction

Still being built. Things may change

Selection Sort Movement Distance

Interview

Time limit5sMemory limit1024 MB

Summary
Given a permutation, run selection sort and report the total distance each value moves across all swaps.
Level

Easy3 of 10

Topics
Array, Simulation
Solved
No attempts yet

Problem

A sequence AA of length NN contains each integer from 1 to NN exactly once. Apply the selection sort algorithm to AA and find the moving distance of each number.

If you are not familiar with selection sort, read the description below.

  • Selection sort sorts a sequence A={A1,A2,⋯ ,AN}A=\{A_1,A_2,\cdots,A_N\} in ascending order by repeating the following operation N−1N-1 times.
    1. In the ii-th operation, find the minimum AjA_j among Ai,Ai+1,⋯ ,ANA_i,A_{i+1},\cdots,A_N.
    2. Swap the positions of AiA_i and AjA_j. The moving distances of AiA_i and AjA_j each increase by (j−i)(j-i).

For example, take {1,3,5,2,4}\{1,3,5,2,4\}. Initially every number has moving distance 0. The algorithm runs as follows.

  1. Swap A1=1A_1=1 with itself, giving {1,3,5,2,4}\{1,3,5,2,4\}. The moving distance of 1 increases by 0.
  2. Swap A2=3A_2=3 with A4=2A_4=2, giving {1,2,5,3,4}\{1,2,5,3,4\}. The moving distances of 2 and 3 each increase by 2.
  3. Swap A3=5A_3=5 with A4=3A_4=3, giving {1,2,3,5,4}\{1,2,3,5,4\}. The moving distances of 3 and 5 each increase by 1.
  4. Swap A4=5A_4=5 with A5=4A_5=4, giving {1,2,3,4,5}\{1,2,3,4,5\}. The moving distances of 4 and 5 each increase by 1.

So 1 moves 0, 2 moves 2, 3 moves 3, 4 moves 1, and 5 moves 2.

Input

The first line contains the length NN of the sequence.

The second line contains the elements A1,A2,⋯ ,ANA_1,A_2,\cdots,A_N of the sequence, separated by spaces, in order.

Output

Print NN integers separated by spaces on the first line. The ii-th integer is the moving distance of ii.

Constraints

  • 1≤N≤5×1051\leq N\leq 5\times 10^5
  • Each integer from 11 to NN appears exactly once in AA.

Examples2

  1. Example 1

    Input
    5
    1 2 3 4 5
    
    Expected output
    0 0 0 0 0
    
  2. Example 2

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