PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000064. Nghịch thế sau mỗi phép quay trái (Shift and Inversions)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nghịch thế sau mỗi phép quay trái (Shift and Inversions)

Shift and Inversions

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation A=[a0,a1,…,aN−1]A=[a_0,a_1,\ldots,a_{N-1}] of the integers 00 through N−1N-1. Each integer occurs once.

For every kk from 00 to N−1N-1, form the left-rotated sequence B(k)B^{(k)} with bi=a(i+k) mod Nb_i=a_{(i+k)\bmod N}.

An inversion is a pair of positions i<ji<j whose values satisfy bi>bjb_i>b_j. Compute the inversion count of each of the NN rotations of the original sequence, in increasing order of kk.

Input

The first line contains NN.

The second line contains NN integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1} forming a permutation of 0,1,…,N−10,1,\ldots,N-1.

Output

Print exactly NN lines. Line k+1k+1 contains the inversion count of the left rotation by kk positions, for k=0,1,…,N−1k=0,1,\ldots,N-1.

Subtasks

  • Subtask 1 (20 points): 2≤N≤602\le N\le 60; AA is a permutation of 0,1,…,N−10,1,\ldots,N-1.

  • Subtask 2 (30 points): 2≤N≤30002\le N\le 3000; AA is a permutation of 0,1,…,N−10,1,\ldots,N-1.

  • Subtask 3 (50 points): 2≤N≤3000002\le N\le 300000; AA is a permutation of 0,1,…,N−10,1,\ldots,N-1.

Examples

Input

4
0 1 2 3

Output

0
3
4
3

Explanation

Consider each rotation separately:

  • k=0k=0: [0,1,2,3][0,1,2,3] has 00 inversions.
  • k=1k=1: [1,2,3,0][1,2,3,0] has 33 inversions, all ending at 00.
  • k=2k=2: [2,3,0,1][2,3,0,1] has 44 inversions: (2,0)(2,0), (2,1)(2,1), (3,0)(3,0), (3,1)(3,1).
  • k=3k=3: [3,0,1,2][3,0,1,2] has 33 inversions, all starting at 33.

The printed counts are 0, 3, 4, 3.