Batman Returns
Time limit2.5sMemory limit1024 MB
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 skyscrapers stand along it. They are numbered from west to east from to , and the height of the -th skyscraper is 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 to skyscraper if and only if and . The buildings between and 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 , with the largest possible .
City authorities have drawn up renewal plans. In plan , only the skyscrapers from to , inclusive, remain, and the others are destroyed. For each plan , find and such that , , and is as large as possible.
Input
The first line contains one integer (), the number of skyscrapers. The second line contains integers (), the heights of the skyscrapers. The third line contains one integer (), the number of plans. Each of the last lines contains two integers and (), the range that remains in plan .
Output
For each plan, print two integers and for an optimal pair. If no observation flight is possible, print . 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 . 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.