Under construction

Still being built. Things may change

Minimum and Maximum

Interview

Time limit2sMemory limit192 MB

Summary
Given N numbers and M range queries, output the minimum and maximum value within each query's index range.
Level

Medium4 of 10

Topics
Segment tree, Array
Solved
No attempts yet

Problem

You are given N integers in input order, where 1 <= N <= 100,000. Each integer is between 1 and 1,000,000,000 inclusive.

For one interval from the a-th integer through the b-th integer, finding the minimum and maximum is simple. However, M such intervals are given, where 1 <= M <= 100,000. For every interval, find the minimum and maximum among the integers in that input-order range.

Here, the a-th integer means the integer that appeared a-th in the input. For example, if a = 1 and b = 3, consider the first, second, and third integers.

Input

The first line contains N and M.

Each of the next N lines contains one integer.

Each of the next M lines contains a pair a and b.

Output

Print M lines in the order the queries are given. For each pair a and b, print the minimum and then the maximum.

Examples1

  1. Example 1

    Input
    10 4
    75
    30
    100
    38
    50
    51
    52
    20
    81
    5
    1 10
    3 5
    6 9
    8 10
    
    Expected output
    5 100
    38 100
    20 81
    5 81