#BST0000023. Cây tìm kiếm nhị phân - Bộ đếm độ sâu (Binary Search Tree)

Cây tìm kiếm nhị phân - Bộ đếm độ sâu (Binary Search Tree)

Binary Search Tree

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A permutation of the integers from 11 to nn is given. Build a binary search tree in exactly this order: the first number becomes the root; for every later number, start at the root, go left when the new number is smaller and right when it is larger, until an empty child position is reached and the new node is created there.

The root has depth 00. The depth of any other node is the number of edges from the root to that node.

A counter CC is initially 00. After each insertion, add the depth of the newly inserted node to CC, then print the current value of CC.

The first inserted value is the root, so its depth is 00 and the first printed counter value is also 00.

Input

The first line contains nn.

The next nn lines contain a1,a2,…,ana_1,a_2,\ldots,a_n in order. The sequence is a permutation of 1,2,…,n1,2,\ldots,n.

Output

Print exactly nn lines. Line ii is the value of CC immediately after inserting aia_i.

Subtasks

Subtask 1 (20 points): 1≤n≤201\le n\le 20.

Subtask 2 (30 points): 1≤n≤50001\le n\le 5000.

Subtask 3 (50 points): 1≤n≤3000001\le n\le 300000.

In all subtasks, a1,a2,…,ana_1,a_2,\ldots,a_n is a permutation of the integers from 11 through nn.

Examples

Input

8
3
5
1
6
8
7
2
4

Output

0
1
2
4
7
11
13
15

Explanation

The depths of the newly inserted nodes are 0,1,1,2,3,4,2,20,1,1,2,3,4,2,2.

Starting from C=0C=0 and adding these depths after each insertion gives 0,1,2,4,7,11,13,150,1,2,4,7,11,13,15, matching the eight output lines.