FREQUENT - Frequent values


You are given a sequence of n integers a1, a2 ... an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai ... aj.

Input Specification

The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 ... an (-100000 ≤ ai ≤ 100000, for each i ∈ {1 ... n}) separated by spaces. You can assume that for each i ∈ {1 ... n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

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

Output Specification

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3

A naive algorithm may not run in time!

hide comments
rajsaradava: 2024-07-09 19:10:42

Use scanf and printf.
There are multiple test cases so 0 is there for a reason at the end. (If someone is new to this type)
Avoid cin , cout - causes TLE
Use fast I/O

mahabub618: 2023-01-26 19:03:03

Nice problem. I solved it using Mo's. But be careful about handling negative (-10^5 ) value and multi-test case.

princemishra: 2021-12-05 07:00:51

with mos can be solved

sal_vat_ion: 2021-11-17 11:58:26

Read input carefully, there are multiple TCs.

a520huynm: 2021-10-05 17:24:18

although i create so much test case, i can't find bug. Help me ! https://ideone.com/<snip>

[Simes]: Please use the forum for this.

Last edit: 2022-06-01 07:09:14
ravi_mhw: 2021-05-28 14:57:07

what to do for last 0 input ,i am getting errors coz of this

ravi_mhw: 2021-05-28 10:36:23

yes we can use squareroot decomposition and mos algorithm here

wheneveright: 2021-04-06 04:11:56

Nice problem,Segment Tree can be used.

yaseenmollik: 2020-08-01 06:18:00

Same idea of maximum subarray sum problem. Nice problem.

kushagra_2: 2020-07-11 09:35:21

classic segtree problem try GSS1 after this


Added by:Adrian Kuegel
Date:2007-07-06
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:University of Ulm Local Contest 2007