問題一覧 > 通常問題

No.2077 Get Minimum Algorithm

レベル : / AI推定難易度 : 2050 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 22
作問者 : chineristAC / テスター : HYEA LEE
お気に入りにしたユーザー ProblemId : 7409 / 自分の提出
問題文最終更新日: 2022-09-16 23:29:58
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 360 (順位表) の他の問題:

Problem Statement

There is a permutation $P=(P_1,\ P_2,\ \dots,\ P_N)$ of length $N$ consisting of the integers from $1$ to $N$.

Consider the following operation.

Operation

  • $1.$ Initialize with $X=N+1,\ i=1$.
  • $2.$ Simultaneously update $X\leftarrow \min(X,\ P_i),\ P_i\leftarrow \max(X,\ P_i)$.
  • $3.$ If $i=N$, end the operation. Otherwise, update $i\leftarrow i+1$ and return to $2$.

Let $P_k=(P_{k,1},\ P_{k,2},\ \dots,\ P_{k,N})$ denote the sequence $P$ after applying the operation $k$ times to $P$.

You are given $Q$ queries. In the $i$-th query, $2$ integers $x_i,\ y_i\ (x_i < y_i)$ are given. Output $k$ such that $P_{x_i,k}=y_i$ (it can be shown that such a $k$ always exists).

Input

$N$
$P_1$ $P_2$ $\dots$ $P_N$
$Q$
$x_1$ $y_1$
$x_2$ $y_2$
$\vdots$
$x_Q$ $y_Q$
  • $1 \le N \le 10^5$
  • $1 \le P_i \le N$
  • If $i\neq j$, then $P_i \neq P_j$
  • $1 \le Q \le 2 \times 10^5$
  • $1 \le x_i< y_i \le N$
  • All input values are integers

Output

Output $Q$ lines. On the $i$-th line, output $k$ such that $P_{x_i,k}=y_i$.

Print a newline at the end.

Sample

Sample 1
Input
5
2 5 3 1 4
2
1 2
2 5
Output
4
3

We have $P_1=(6,\ 5,\ 3,\ 2,\ 4)$, and in particular $P_{1,4}=2$. Therefore, output $4$ for the $1$st query.

Also, we have $P_2=(6,\ 6,\ 5,\ 3,\ 4)$, and in particular $P_{2,3}=5$. Therefore, output $3$ for the $2$nd query.

Sample 2
Input
5
1 2 3 4 5
2
1 3
3 5
Output
3
5

Sample 3
Input
20
20 19 9 15 18 16 13 14 17 12 11 10 6 5 8 4 2 3 7 1
10
4 6
13 19
10 19
14 17
11 15
9 10
10 15
2 9
3 18
7 14
Output
18
15
12
18
17
20
16
14
6
14

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。