Description

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

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

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

Source

Ulm Local 2007
 
 
无脑的线段树。写着玩玩。
 program rrr(input,output);
type
treetype=record
l,r,m,lm,rm:longint;
end;
var
a:array[..]of treetype;
c:array[..]of longint;
n,i,q,x,y:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;
if l=r then begin a[k].m:=;a[k].lm:=;a[k].rm:=;exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);build(i+,mid+,r);
a[k].m:=max(a[i].m,a[i+].m);if c[mid]=c[mid+] then a[k].m:=max(a[k].m,a[i].rm+a[i+].lm);
if c[l]=c[mid+] then a[k].lm:=a[i].lm+a[i+].lm else a[k].lm:=a[i].lm;
if c[mid]=c[r] then a[k].rm:=a[i].rm+a[i+].rm else a[k].rm:=a[i+].rm;
end;
function query(k:longint):longint;
var
mid,i,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].m);
mid:=(a[k].l+a[k].r)>>;i:=k+k;
ans:=;
if x<=mid then ans:=query(i);
if mid<y then ans:=max(ans,query(i+));
if (x<=mid) and (mid<y) and (c[mid]=c[mid+]) then ans:=max(ans,min(mid-x+,a[i].rm)+min(y-mid,a[i+].lm));
exit(ans);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
while true do
begin
read(n);if n= then break;
readln(q);
for i:= to n do read(c[i]);
build(,,n);
for i:= to q do begin readln(x,y);writeln(query()); end;
end;
close(input);close(output);
end.

最新文章

  1. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];
  2. php中的常用数组函数(八) 排序函数汇总(sort、rsort、usort、asort、uasort、arsort、ksort、uksort、krsort、natsort、natcasesort、array_multisort)
  3. oracle中如何创建dblink
  4. 《一课经济学》书摘笔记II
  5. 学会简单使用poi进行excel有关操作
  6. 搜索之BM25和BM25F模型
  7. Android:Logcat中找不到本应该输出的Log调试信息
  8. oracle 自增列设置
  9. 函数lock_rec_get_nth_bit
  10. 关于Failed to convert property value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean
  11. Bridge 桥梁模式 桥接模式
  12. ab -n -c
  13. MSMQ是什么?
  14. HDOJ 4745 Two Rabbits DP
  15. java_ log4j的基本配置参数
  16. 字符集 ISO-8859-1(2)
  17. LeetCode 118. Pascal&#39;s Triangle (杨辉三角)
  18. EF6CodeFirst+MVC5+Autofac泛型注册 入门实例
  19. OFFICE2007软件打开word时出现SETUP ERROR的解决方法
  20. asp.net简繁体转换

热门文章

  1. Python2.7-SciPy
  2. 浅谈 MVC 和 MTV
  3. redis的使用,相比memcached
  4. VB6 CHECK is run as admin privilege
  5. Compensating-Transaction模式
  6. 11、可扩展MySQL+12、高可用
  7. 7、mysql高级特性
  8. 欧几里得算法(及扩展)&amp;&amp;快速幂(二分+位运算)
  9. Linux中tty、pty、pts的概念区别 转载
  10. libgdx学习记录22——3d物体创建