Batman Returns

시간 제한2.5초메모리 제한1024 MB

요약
각 구간마다 h[p]<h[q]인 가장 먼 두 위치 p<q를 찾고, 그러한 쌍이 없으면 -1 -1을 출력한다.
난이도

어려움10점 중 8점

유형
세그먼트 트리, 이분 탐색, 분할 정복, 배열
정답자
아직 제출이 없습니다

문제

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 h_ih\_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 h_p<h_qh\_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 q−pq - p is maximum possible.

City authorities have developed mm plans of city renewal. According to the ii-th plan only skyscrapers from l_il\_i to r_ir\_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 p_ip\_i and q_iq\_i that l_i≤p_i<q_i≤r_il\_i \leq p\_i < q\_i \leq r\_i, h_p_i<h_q_ih\_{p\_i} < h\_{q\_i} and q_i−p_iq\_i - p\_i is maximum possible.

입력

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

The second line contains nn integers h_ih\_i(1≤h_i≤200,0001 \le h\_i \le 200\\,000) --- heights of the skyscrapers.

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

Each of the last mm lines contains two integers l_il\_i and r_ir\_i (1≤l_i<r_i≤n1 \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 p_ip\_i and q_iq\_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 3≥13 \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.

예제2

  1. 예제 1

    입력
    4
    2 3 1 4
    2
    2 3
    1 3
    
    예상 출력
    -1 -1
    1 2
    
  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