MEDIAN3 - The Famous ICPC Team Again

no tags 

When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem within a particular publish time interval. That is to say, if problems had been sorted in a line, each time they will choose one of them from a specified segment of the line.

Moreover, when collecting the problems, Mr. B had also known an estimation of the difficulty of each problem. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. For addressing this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore he needed a way to know the median number in the given interval of the sequence.

Input

For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficulties of each problem, already sorted by publish time. The next line is a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.

Output

For each query, output a single line containing an integer which denotes the difficulty of the problem that Mr. B should choose.

Example

Input:
5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5

Output:
Case 1:
3
3
2
Case 2:
6
6
4

hide comments
robbin: 2020-02-28 16:30:56

Use persistent segment tree; Mo's algorithm is too slow.

Ranker: 2012-06-26 13:43:57

Input ends with EOF, right??

Out0fbounds: 2012-06-06 09:07:54

sub id 7099006 getting tle plz help me out


Added by:Fudan University Problem Setters
Date:2012-05-25
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Classical Problem, used in FDU Local Contest 2012