Bob's Average
Time limit3sMemory limit1024 MB
For each query on an odd-length subarray, find the largest single value that repeated median-of-three merges can leave behind.
- Level
Hard8 of 10
- Topics
- Binary search, Prefix sum, Segment tree
- Solved
- No attempts yet
Problem
Bob came up with a new way to compute the average of an array with an odd number of elements. While the length of the array is greater than one, the following operation is performed: an arbitrary segment of length 3 with boundaries and is selected, the median of these three elements is computed, and the three elements are replaced by one element equal to the median. The median of three elements is the second-largest of them. For example, the median of is , and the median of is .
For the array , choose , . The median of is , so the array becomes . Then choose , . The median of is , so the array becomes .
The result depends on the choice of segments. Bob chooses the segments so that the single number left at the end is as large as possible. This number is called Bob's average for the array.
For an array of elements, you must answer queries. The -th query gives and , and the answer is Bob's average of the subarray . Every query has an odd segment length.
Input
The first line contains an integer (), the size of the array.
The second line contains integers (), the array elements.
The third line contains an integer (), the number of queries.
Each of the next lines contains two integers and (). It is guaranteed that is odd.
Output
For each query, print on its own line the answer to that query, a single integer.