#abc342b. B - Which is ahead?

B - Which is ahead?

Score: 200200 points

问题描述

NN 名人员站成一排。站在第 ii 个位置的人是人员 PiP_i

执行 QQ 个查询操作。第 ii 个查询如下:

  • 给定整数 AiA_iBiB_i。在人员 AiA_i 和人员 BiB_i 之间,输出站在更靠前位置的人员编号。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN people standing in a line. The person standing at the ii-th position from the front is person PiP_i.

Process QQ queries. The ii-th query is as follows:

  • You are given integers AiA_i and BiB_i. Between person AiA_i and person BiB_i, print the person number of the person standing further to the front.

Constraints

  • All inputs are integers.
  • 1N1001 \leq N \leq 100
  • 1PiN1 \leq P_i \leq N
  • PiPj (ij)P_i \neq P_j\ (i \neq j)
  • 1Q1001 \leq Q \leq 100
  • 1Ai<BiN1 \leq A_i < B_i \leq N

Input

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

NN

P1P_1 \ldots PNP_N

QQ

A1A_1 B1B_1

\vdots

AQA_Q BQB_Q

Output

Print QQ lines. The ii-th line should contain the response for the ii-th query.

Sample Input 1

3
2 1 3
3
2 3
1 2
1 3

Sample Output 1

2
2
1

In the first query, person 22 is at the first position from the front, and person 33 is at the third position, so person 22 is further to the front.

In the second query, person 11 is at the second position from the front, and person 22 is at the first position, so person 22 is further to the front.

In the third query, person 11 is at the second position from the front, and person 33 is at the third position, so person 11 is further to the front.

Sample Input 2

7
3 7 2 1 6 5 4
13
2 3
1 2
1 3
3 6
3 7
2 4
3 7
1 3
4 7
1 6
2 4
1 3
1 3

Sample Output 2

3
2
3
3
3
2
3
3
7
1
2
3
3

update @ 2024/3/10 01:35:56