Batman Returns | 프로그래밍의 벗 PivotOJ
PivotOJ

Batman Returns

시간 제한: 2500ms메모리 제한: 1024MB출처: MOOI 2015-16 finalBOJ 30818

문제

Gotham City consists of a single street, and there are nn skyscrapers located along it. They are numbered from west to east with integers from 11 to nn, the height of the ii-th skyscraper is equal to hih_i meters.

Every night Batman performs an observation flight over the city. He climbs on the roof of some skyscraper and glides down to the roof of some other skyscraper. Due to the strong permanent wind he is only able to flight westward, but his altitude remains almost the same. Thus, he is able to glide down from skyscraper qq to skyscraper pp if and only if p<qp < q and hp<hqh_p < h_q. Moreover, Batman is very manoeuvrable, so the height of the buildings between pp and qq don't matter. Batman cares a lot about the crime level in the city so he chooses such pair of valid pp and qq that qpq - p is maximum possible.

City authorities have developed mm plans of city renewal. According to the ii-th plan only skyscrapers from lil_i to rir_i, inclusive will remain on this street, while others will be destroyed. For each plan ii Batman wants to know the optimal plan to observe the city, namely such pip_i and qiq_i that lipi<qiril_i \leq p_i < q_i \leq r_i, hpi<hqih_{p_i} < h_{q_i} and qipiq_i - p_i is maximum possible.

입력

The first line of the input contains one integer nn (1n2000001 \le n \le 200\,000) --- number of skyscrapers on the street.

The second line contains nn integers hih_i(1hi2000001 \le h_i \le 200\,000) --- heights of the skyscrapers.

Third line contains integer mm (1m2000001 \le m \le 200\,000) --- the number of plans designed by the city authorities.

Each of the last mm lines contains two integers lil_i and rir_i (1li<rin1 \leq l_i < r_i \leq n), denoting the range of the skyscrapers that will remain according to the ii-th plan.

출력

For each renewal plan you should print two integers --- optimal pip_i and qiq_i. If there is no possible observation flight at all, you should print -1 -1.

If there are many optimal answers, you may print any one of them.

힌트

Consider the first sample test. In the first query the only two available skyscrapers have heights 33 and 11 but they are not valid since 313 \geq 1. In the second query the pair consisting of the first and the second skyscrapers is valid since they have heights 22 and 33.

Consider the second sample test. In the first query the pair of skyscrapers with heights 22 and 33, and the pair of skyscrapers with heights 22 and 44 are valid. The distance between first two of them is greater so this pair produces the answer for this query.

예제

예제 1

입력
4
2 3 1 4
2
2 3
1 3
출력
-1 -1
1 2

예제 2

입력
7
5 4 2 4 3 1 5
4
2 6
2 7
1 7
3 7
출력
3 5
2 7
2 7
3 7
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.