This page is still under construction.

Parts of this page are still being built. What you see may change.

Frequent Values

Time limit1sMemory limit128 MB

Summary
For each range query on a sorted array, output how many times the most frequent value occurs inside the range.
Level

Hard8 of 10

Topics
Segment tree, Divide and conquer, Array
Solved
No attempts yet

Problem

You are given a sequence of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n in non-decreasing order. You are also given several queries, each consisting of two indices ii and jj (1≤i≤j≤n1 \le i \le j \le n). For each query, determine how many times the most frequent value occurs among ai,ai+1,…,aja_i, a_{i+1}, \ldots, a_j.

Input

The input consists of several test cases. Each test case begins with a line containing two integers nn and qq (1≤n,q≤1000001 \le n, q \le 100000). The next line contains nn integers a1,…,ana_1, \ldots, a_n (−100000≤ai≤100000-100000 \le a_i \le 100000) separated by spaces. It is guaranteed that ai≤ai+1a_i \le a_{i+1} for every i∈{1,…,n−1}i \in \{1, \ldots, n-1\}. The following qq lines each contain one query, given as two integers ii and jj (1≤i≤j≤n1 \le i \le j \le n) denoting the boundary indices of the query.

The last test case is followed by a line containing a single 00.

Output

For each query, print on its own line a single integer: the number of occurrences of the most frequent value within the given range.

Examples2

  1. Example 1

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

    Input
    1 1
    5
    1 1
    0
    
    Expected output
    1