How Many Can You See?

medium monotonic-stack stack 1000 ms · 256 MB

How Many Can You See?

n people stand in a row, numbered 1 to n from left to right, all facing right (toward increasing index). Person i has height h_i, and all heights are distinct.

Person i can see person j, for j > i, if every person standing strictly between them — at positions i+1, i+2, …, j-1 — is strictly shorter than both person i and person j. In particular, person i can always see person i+1: nobody stands between them. A person who is at least as tall as person i blocks the view of everyone standing beyond, no matter how tall that further person is.

For every person, print how many people they can see somewhere to their right.

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of people.

The second line contains n distinct integers h_1, h_2, …, h_n (1 ≤ h_i ≤ 10^9), the heights from left to right.

Output

Print n integers separated by spaces: the i-th of them is the number of people that person i can see to their right.

Notes

In the first example the heights are 10 6 8 5 11 9. Person 1 (height 10) sees person 2 (height 6, nobody stands between them), person 3 (height 8, since person 2 is shorter than both), and person 5 (height 11, since persons 2, 3 and 4 are all shorter than both 10 and 11). Person 1 cannot see person 4 (height 5): person 3 stands between them with height 8, which is not shorter than person 4. Person 1 also cannot see person 6: person 5 stands between them with height 11, which is taller than person 1 and blocks the view no matter who stands further away. So person 1 can see 3 people in total.

Person 6, last in line, can see 0 people — nobody stands to their right.

Example 1

Input

6
10 6 8 5 11 9

Output

3 1 2 1 1 0

Example 2

Input

5
1 2 3 4 5

Output

1 1 1 1 0

Hints · reveal one at a time if you're stuck

Hint 1

Scan from the right so that when you look at a person, you already know everyone standing to their right. Which of those people can be seen, and which are hidden behind someone else?

Hint 2

Keep a stack of the heights you have passed, strictly decreasing from bottom to top. Each person you pop out is visible from the current person, and the one left on top, if any, is one more person who is visible.

report an issue Prefer your own editor? sooks prep 75 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.