cenzura

Time limit: 1.5s Memory limit: 128MB Input: Output:

Given NN and a1 a2  aNa_1 \ a_2 \ \dots \ a_N. You are given QQ queries of the form ll, rr. For the ii-th query, you will choose the numbers ll and rr (1lrN1 \leq l \leq r \leq N) and you want to know what the maximum of the array would be and how many times it would appear in the array if the subarray [l..r][l..r] were censored.

  • The subarray [l..r][l..r] of an array is the sequence al al+1 al+2ara_l \ a_{l+1} \ a_{l+2} \dots a_r.
  • By censoring a subarray [l..r][l..r], we mean replacing aia_i with 00 for every lirl \leq i \leq r. For example, if we censor the subarray [2..4][2..4] of the array [1,2,3,4,5,6,7][1, 2, 3, 4, 5, 6, 7], we get the array [1,0,0,0,5,6,7][1, 0, 0, 0, 5, 6, 7].

Input data

The first line contains two integers, NN and QQ.

The second line contains an array of NN natural numbers, representing the array aa.

The next QQ lines each contain a pair of numbers ll, rr.

Output data

The answers to the queries will be printed on QQ lines. The ii-th line will contain two numbers, representing the answer to the ii-th query.

Constraints and clarifications

  • 1N,Q1061 \leq N, Q \leq 10^6
  • 1ai1091 \leq a_i \leq 10^9
  • For 4444 points, N,Q5 000N, Q \leq 5 \ 000

Example 1

stdin

9 6
3 2 3 1 6 7 5 7 7
1 2
1 3
3 6
6 9
1 9
2 8

stdout

7 3
7 3
7 2
6 1
0 9
7 1

Explanation

N=9N = 9, Q=6Q = 6, and a=[3,2,3,1,6,7,5,7,7]a = [3, 2, 3, 1, 6, 7, 5, 7, 7].

For the first query, if we censor the subarray (11, 22), the resulting array is [0,0,3,1,6,7,5,7,7][0, 0, 3, 1, 6, 7, 5, 7, 7], with the maximum being 77 and appearing 33 times.

For the third query, if we censor the subarray (33, 66), the resulting array is [3,2,0,0,0,0,5,7,7][3, 2, 0, 0, 0, 0, 5, 7, 7], with the maximum being 77 and appearing 22 times.

For the fourth query, if we censor the subarray (66, 99), the resulting array is [3,2,3,1,6,0,0,0,0][3, 2, 3, 1, 6, 0, 0, 0, 0], with the maximum being 66 and appearing once.

For the fifth query, if we censor the subarray (11, 99), the resulting array is [0,0,0,0,0,0,0,0,0][0, 0, 0, 0, 0, 0, 0, 0, 0], with the maximum being 00 and appearing 99 times.

Log in or sign up to be able to send submissions!