#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 to 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 . The depth of any other node is the number of edges from the root to that node.
A counter is initially . After each insertion, add the depth of the newly inserted node to , then print the current value of .
The first inserted value is the root, so its depth is and the first printed counter value is also .
Input
The first line contains .
The next lines contain in order. The sequence is a permutation of .
Output
Print exactly lines. Line is the value of immediately after inserting .
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, is a permutation of the integers from through .
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 .
Starting from and adding these depths after each insertion gives , matching the eight output lines.