Under construction

Still being built. Things may change

Colorful Village 2

Interview

Time limit2sMemory limit512 MB

Summary
For each query range in a nondecreasing brightness array, report how many times the most frequent value appears.
Level

Medium6 of 10

Topics
Array, Binary search, Prefix sum
Solved
No attempts yet

Problem

Minho looks after a country called Cheonnara, where there are NN houses. He numbered the houses 1 through NN.

One day Minho painted every house in the country. Each paint has its own brightness, and a careless order would spoil the look of the village, so he painted the houses so that the paint never gets darker as the house number grows. In other words, the brightness of house ii is at most the brightness of house i+1i+1.

Once the painting was done, Minho wanted to know how many times the most common brightness appears among the houses in a given range. Write a program that reports that count for every range he asks about.

Input

The first line contains the number of houses NN and the number of ranges QQ, separated by a space. (1≤N≤1000001 \le N \le 100000, 1≤Q≤1000001 \le Q \le 100000)

The second line contains the brightness values P1,P2,…,PNP_1, P_2, \dots, P_N painted on houses 1 through NN, in order. (−100000≤Pi≤100000-100000 \le P_i \le 100000, and Pi≤Pi+1P_i \le P_{i+1} for every ii.)

Each of the next QQ lines contains one range as two integers XX and YY (1≤X≤Y≤N1 \le X \le Y \le N), meaning the houses from XX to YY.

Output

Print QQ lines. On the ii-th line print how many times the most common brightness appears in the ii-th range.

Examples4

  1. Example 1

    Input
    10 3
    -1 -1 1 1 1 1 3 10 10 10
    2 3
    1 10
    5 10
    
    Expected output
    1
    4
    3
    
  2. Example 2

    Input
    1 1
    0
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    8 4
    7 7 7 7 7 7 7 7
    1 8
    3 3
    2 7
    1 2
    
    Expected output
    8
    1
    6
    2
    
  4. Example 4

    Input
    7 5
    -100000 -50000 -1 0 1 50000 100000
    1 7
    1 1
    3 5
    7 7
    2 6
    
    Expected output
    1
    1
    1
    1
    1