#P6412. [COCI 2008/2009 #3] BST

[COCI 2008/2009 #3] BST

Description

You are given a permutation aa of 1n1 \sim n. Insert the elements of the sequence into a BST one by one, and find the number of times the BST insertion function is executed.

Note: The first number is already the root of the BST.

If you do not understand the statement above, here is some pseudocode:

insert( number x, node n )
    c+1;
    if x is less than the number in node n
        if n has no left child
            create a new node with the number x and set it to be the left child of node n
     else
         insert(x, left child of node n)
     else (x is greater than the number in node n)
         if n has no right child
             create a new node with the number x and set it to be the right child of node n
         else
             insert(x, right child of node n) 

What you need to output is the value of cc after each call to the insert function.

Again: The first number is already the root of the BST.

Input Format

The first line contains an integer nn, the length of the permutation aa.

The next nn lines each contain an integer. The ii-th of these lines is aia_i.

Output Format

Output a total of nn lines, each containing an integer, representing the value of cc after each insertion (i.e., after each call of the insert function above).

4
1
2
3
4 

0
1
3
6

5
3
2
4
1
5 

0
1
2
4
6 
8
3
5
1
6
8
7
2
4 

0
1
2
4
7
11
13
15 

Hint

Constraints

  • For 50%50\% of the testdata, n103n \le 10^3 is guaranteed.
  • For 100%100\% of the testdata, 1n3×1051 \le n \le 3 \times 10^5, 1ain1 \le a_i \le n, and all aia_i are distinct.

Notes

This problem is translated from T5 BST of Croatian Open Competition in Informatics 2008/2009 Contest #3.

Translated by ChatGPT 5