Bob's Average

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

In a course in mathematical statistics, Bob came up with a new way to calculate the average for arrays containing an odd number of elements.

As long as the length of the array is greater than one, the following operation is performed: an arbitrary segment of the array of length 3 with boundaries ll and r=l+2r = l + 2 is selected, the median of these three elements is calculated and then these elements are replaced by one element equal to the median.

The median of an array of three elements is the second-highest element of this array. For example, the median of the array \[1,5,4]\[1, 5, 4] is 44, and the median the array \[2,2,2]\[2, 2, 2] is 22.

Let's consider one of the ways to calculate the Bob's average on an array \[4,1,3,2,5]\[4, 1, 3, 2, 5]. In the first step, we select a sub-section with boundaries l=2l = 2, r=4r = 4. Since 22 is the median of the subarray \[1,3,2]\[1, 3, 2], then the array is converted as follows: \[4,1,3,2,5]\[4,2,5]\[4, **1, 3, 2**, 5] \rightarrow \[4, **2**, 5]. In the second step, the only possible segment of length 33 has boundaries l=1l = 1, r=3r = 3. Since 44 is the median of the subarray \[4,2,5]\[4, 2, 5], then \[4,2,5]\[4]\[**4, 2, 5**] \rightarrow \[4].

Bob noticed that his method of calculating the average is not quite correctly defined: depending on the choice of segments, the result of the calculation may be different. To fix this situation, Bob decided to choose segments to replace with the median in such a way that the only number remaining at the end was the maximum possible. It is this number that Bob calls Bob's average for this array.

For an array aa of nn elements, you need to answer qq queries, The jj-th of those queries is characterized by the boundaries of L_jL\_j and R_jR\_j.

The answer to the ii-th query is the Bob's average of the segment of the original array \[a_L_j,a_L_j+1,a_R_j]\[a\_{L\_j}, a\_{L\_j + 1}, \ldots a\_{R\_j}]. For all queries, it is guaranteed that the length of the query segment is odd.

입력

The first line contains an integer nn (3n51043 \leq n \leq 5 \cdot 10^4) --- array size.

The second line contains nn integers a_ia\_i (0a_i1090 \leq a\_i \leq 10^9) --- array elements.

The third line contains an integer qq (1q1051 \leq q \leq 10^5) --- number of requests.

The following qq lines contain integers L_jL\_j, R_jR\_j (1L_jR_jn1 \leq L\_j \leq R\_j \leq n) --- the boundaries of the sub-section of the jj-th query, it is guaranteed that R_jL_j+1R\_j - L\_j + 1 is not divisible by 22.

출력

In the jj-th line of the output, output a single integer --- the answer to the jj-th request.