#P2311. loidc,想想看

loidc,想想看

Description

Moreover, loidc has a special ability: he can foresee the future and accurately guess how many gold medals the Chinese team will win in any single unit of time. However, the workload is huge, and predicting gold medals consumes a lot of energy, so he cannot accurately determine which unit time within a given interval attains the maximum number of gold medals. This makes loidc quite upset. After thinking for a while, he thought of you, because he knows you are an OIer. He chuckled and handed the problem over to you. loidc hopes you can produce the answer within 1 s.

Description

Input Format

The first line contains an nn, indicating there are nn time intervals.

The next line contains nn numbers aia_i, where aia_i denotes the number of gold medals loidc predicts the Chinese team will win in the ii-th time interval.

Then, the third line contains a number mm, indicating loidc has mm queries.

The next mm lines each contain two numbers xix_i and yiy_i, asking which unit time within the interval [xi,yi][x_i, y_i] has the maximum number of gold medals.

loidc has a habit of asking questions in order; that is, each query is asked after the previous one.

Note that for the ii-th and (i+1)(i+1)-th queries, it strictly holds that xixi+1x_i \le x_{i+1} and yiyi+1y_i \le y_{i+1}.

Output Format

Output mm lines. Each line contains a single kik_i, denoting the answer to the ii-th query.

5
2 3 4 5 6
5
1 1
1 2
2 3
3 4
4 5

1
2
3
4
5

Hint

30%30\%: n1000n \le 1000, m1000m \le 1000.

100%100\%: n105n \le {10}^5, m105m \le {10}^5.

All other input and output values are less than maxlongint.

The testdata guarantees aia_i are all distinct.

Translated by ChatGPT 5