Under construction

Still being built. Things may change

Batman Returns

Time limit2.5sMemory limit1024 MB

Summary
Given an array of heights and many range queries, find the pair p < q inside each range with h_p < h_q and maximal q - p, or print -1 -1 if none exists.
Level

Hard8 of 10

Topics
Divide and conquer, Array, Prefix sum
Solved
No attempts yet

Statement

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

Every night Batman flies over the city. He climbs onto the roof of one skyscraper and glides down to the roof of another. A strong permanent wind means he can only fly westward, and his altitude stays almost the same. So he can glide from skyscraper qq to skyscraper pp if and only if p<qp < q and hp<hqh_p < h_q. The buildings between pp and qq do not matter, because Batman is very maneuverable. Batman cares a lot about the crime level in the city, so he picks a valid pair pp, qq with the largest possible q−pq - p.

City authorities have drawn up mm renewal plans. In plan ii, only the skyscrapers from lil_i to rir_i, inclusive, remain, and the others are destroyed. For each plan ii, find pip_i and qiq_i such that li≤pi<qi≤ril_i \le p_i < q_i \le r_i, hpi<hqih_{p_i} < h_{q_i}, and qi−piq_i - p_i is as large as possible.

Input

The first line contains one integer nn (1≤n≤200 0001 \le n \le 200\,000), the number of skyscrapers. The second line contains nn integers hih_i (1≤hi≤200 0001 \le h_i \le 200\,000), the heights of the skyscrapers. The third line contains one integer mm (1≤m≤200 0001 \le m \le 200\,000), the number of plans. Each of the last mm lines contains two integers lil_i and rir_i (1≤li<ri≤n1 \le l_i < r_i \le n), the range that remains in plan ii.

Output

For each plan, print two integers pip_i and qiq_i for an optimal pair. If no observation flight is possible, print −1-1 −1-1. If several optimal answers exist, print any of them.

Hint

Consider the first sample. In the first query, the only two available skyscrapers have heights 3 and 1, but they are not valid because 3≥13 \ge 1. In the second query, the pair formed by the first and second skyscrapers is valid, since their heights are 2 and 3.

Consider the second sample. In the first query, the pairs with heights 2 and 3 and with heights 2 and 4 are both valid. The first of these pairs is farther apart, so it gives the answer for that query.

Examples2

  1. Example 1

    Input
    4
    2 3 1 4
    2
    2 3
    1 3
    
    Expected output
    -1 -1
    1 2
    
  2. Example 2

    Input
    7
    5 4 2 4 3 1 5
    4
    2 6
    2 7
    1 7
    3 7
    
    Expected output
    3 5
    2 7
    2 7
    3 7