#arc173c. C - Not Median

C - Not Median

Score: 600600 points

问题陈述

给定一个整数序列 P=(P1,P2,,PN)P=(P_1,P_2,\dots,P_N),它是从 11NN 的一个排列。

对于每个 i=1,2,,Ni=1,2,\dots,N,打印出满足以下所有条件的整数对 (l,r)(l,r) 的最小值 rl+1r-l+1。如果不存在这样的 (l,r)(l,r),则打印 -1

  • 1lirN1 \leq l \leq i \leq r \leq N
  • rl+1r-l+1 是奇数。
  • 序列 PP 的连续子序列 (Pl,Pl+1,,Pr)(P_l,P_{l+1},\dots,P_r) 的中位数 不是 PiP_i

这里,对于长度为 LL(奇数)的整数序列 AAAA 的中位数定义为将 AA 排序后得到的序列 AA' 的第 L+12\frac{L+1}{2} 个值。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a permutation P=(P1,P2,,PN)P=(P_1,P_2,\dots,P_N) of integers from 11 to NN.

For each i=1,2,,Ni=1,2,\dots,N, print the minimum value of rl+1r-l+1 for a pair of integers (l,r)(l,r) that satisfies all of the following conditions. If no such (l,r)(l,r) exists, print -1.

  • 1lirN1 \leq l \leq i \leq r \leq N
  • rl+1r-l+1 is odd.
  • The median of the contiguous subsequence (Pl,Pl+1,,Pr)(P_l,P_{l+1},\dots,P_r) of PP is not PiP_i.

Here, the median of AA for an integer sequence AA of length LL (odd) is defined as the L+12\frac{L+1}{2}-th value of the sequence AA' obtained by sorting AA in ascending order.

Constraints

  • 3N3×1053 \leq N \leq 3 \times 10^5
  • (P1,P2,,PN)(P_1,P_2,\dots,P_N) is a permutation of integers from 11 to NN.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \dots PNP_N

Output

Print the answers for i=1,2,,Ni=1,2,\dots,N in this order, separated by spaces.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

3 3 3 5 3

For example, when i=2i=2, if we set (l,r)=(2,4)(l,r)=(2,4), then rl+1=3r-l+1=3 is odd, and the median of (P2,P3,P4)=(3,5,4)(P_2,P_3,P_4)=(3,5,4) is 44, which is not P2P_2, so the conditions are satisfied. Thus, the answer is 33.

On the other hand, when i=4i=4, the median of (Pl,,Pr)(P_l,\dots,P_r) for any of (l,r)=(4,4),(2,4),(3,5)(l,r)=(4,4),(2,4),(3,5) is P4=4P_4=4. If we set (l,r)=(1,5)(l,r)=(1,5), the median of (P1,P2,P3,P4,P5)=(1,3,5,4,2)(P_1,P_2,P_3,P_4,P_5)=(1,3,5,4,2) is 33, which is not P4P_4, so the conditions are satisfied. Thus, the answer is 55.

Sample Input 2

3
2 1 3

Sample Output 2

-1 3 3

When i=1i=1, no pair of integers (l,r)(l,r) satisfies the conditions.

Sample Input 3

14
7 14 6 8 10 2 9 5 4 12 11 3 13 1

Sample Output 3

5 3 3 7 3 3 3 5 3 3 5 3 3 3