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

#LCA0000040. Dựng cây Cartesian nhỏ nhất (Cartesian Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dựng cây Cartesian nhỏ nhất (Cartesian Tree)

Cartesian Tree

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

Given a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} with pairwise distinct integer values, construct the min Cartesian binary tree. The tree's inorder traversal must visit indices 0,1,…,n−10,1,\ldots,n-1 in this order, and every node's value must be smaller than all values in its subtree.

This tree is unique. Output the parent index pip_i for each node ii. For the root rr, set pr=rp_r=r (the root points to itself).

Input

  • First line: nn.
  • Second line: pairwise distinct a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}.

Output

Print nn integers p0,…,pn−1p_0,\ldots,p_{n-1}; the root is its own parent.

Subtasks

General constraints (all subtasks): 1≤n≤1061\le n\le 10^6; 0≤ai≤1090\le a_i\le 10^9; array values are pairwise distinct.

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; all aia_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; all aia_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤10000001\le n\le 1000000; all aia_i are pairwise distinct.

Examples

Input

3
1 0 2

Output

1 1 1

Explanation

The minimum value is at index 11, making index 11 the root and its own parent. Both indices 00 and 22 have parent 11.