#P5788. 【模板】单调栈

【模板】单调栈

Background

This is a template problem, with no background.

Testdata was updated on 2019.12.12. The time limit was relaxed, and it no longer relies on constant-factor optimizations.

Problem Description

Given an integer sequence a1na_{1 \dots n} with nn terms.

Define a function f(i)f(i) as the index of the first element after the ii-th element in the sequence that is greater than aia_i, that is, f(i)=mini<jn,aj>ai{j}f(i)=\min_{i<j\leq n, a_j > a_i} \{j\}. If it does not exist, then f(i)=0f(i)=0.

Find f(1n)f(1\dots n).

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers a1na_{1\dots n}.

Output Format

Output one line with nn integers representing the values of f(1),f(2),,f(n)f(1), f(2), \dots, f(n).

5
1 4 2 3 5

2 5 4 5 0

Hint

Constraints

For 30%30\% of the testdata, n100n\leq 100.

For 60%60\% of the testdata, n5×103n\leq 5 \times 10^3.

For 100%100\% of the testdata, 1n3×1061 \le n\leq 3\times 10^6, 1ai1091\leq a_i\leq 10^9.

Translated by ChatGPT 5