Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17581   Accepted: 6346

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


调试到00:30
白书上的
把相同的RLE,cnt段,a是数值,c出现次数,left和right是这一段左右到原来位置那里,id[p]是p位置的编号
用RMQ快速求id[l]+1到id[r]-1段的最大值,其他的直接加减就行了
//
// main.cpp
// poj3368
//
// Created by Candy on 10/8/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,l,r;
int x,last,cnt=,a[N],v[N],c[N],id[N],left[N],right[N];
int st[N][];
void initRMQ(){
memset(st,,sizeof(st));
for(int i=;i<=cnt;i++) st[i][]=c[i];
for(int j=;(<<j)<=cnt;j++)
for(int i=;i+(<<j)-<=cnt;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
}
int rmq(int l,int r){
if(l>r) return ;
int k=log(r-l+)/log();
return max(st[l][k],st[r-(<<k)+][k]);
}
int main(int argc, const char * argv[]) {
while((n=read())){
q=read();
memset(c,,sizeof(c));
memset(left,,sizeof(left));
memset(right,,sizeof(right));
v[]=v[n+]=INF;
for(int i=;i<=n;i++){
v[i]=read();
if(v[i]==v[i-]){
c[cnt]++;
right[cnt]=i;
id[i]=cnt;
}else{
cnt++;
a[cnt]=v[i];
c[cnt]++;
left[cnt]=right[cnt]=i;
id[i]=cnt;
}
}
initRMQ();
//for(int i=1;i<=cnt;i++) printf("init %d %d %d %d\n",a[i],c[i],left[i],right[i]);
for(int i=;i<=q;i++){
l=read();r=read();
int ans=;
if(id[l]==id[r]) ans=r-l+;
else{
ans=max(right[id[l]]-l+,r-left[id[r]]+);
ans=max(ans,rmq(id[l]+,id[r]-));
}
printf("%d\n",ans);
}
}
//printf("\n\n\n%d %d %d %d",id[1]+1,c[2],id[10]-1,rmq(id[1]+1,id[10]-1));
return ;
}
 

最新文章

  1. jxl导入导出实例
  2. js的异常捕获
  3. Unity手游之路&lt;二&gt;Java版服务端使用protostuff简化protobuf开发
  4. 通过Migration在EF6中用多个DbContext
  5. 【欧拉定理】计算(a^(b^c))%1000000007
  6. DLL搜索路径和DLL劫持
  7. Linux Vi 删除全部内容,删除某行到结尾,删除某段内容 的方法
  8. WinRT Toolkit 介绍--Control篇
  9. 运行PHP
  10. openssh-clients(CentOS 7 自带的SSH客户端)
  11. 准备 overlay 网络实验环境 - 每天5分钟玩转 Docker 容器技术(49)
  12. 将js进行到底:node学习笔记2
  13. 译-BSA NSH Command介绍
  14. 微信的自动回复&amp;接入聊天机器人
  15. Spring 自带的定时任务Scheduled
  16. Spring学习笔记4——AOP
  17. 04_NoSQL数据库之Redis数据库:set类型和zset类型
  18. runtime统计页面数据或者统计按钮的点击次数
  19. postgresql vacuum操作
  20. PHP学习笔记2

热门文章

  1. this上下文,以及通过call 、apply 实现继承
  2. button 按钮,结合onclick事件,验证和提交表单
  3. 转载:《TypeScript 中文入门教程》 3、接口
  4. java.lang.IllegalArgumentException: Illegal character in query at index 261
  5. Cropper – 简单的 jQuery 图片裁剪插件
  6. WAMPServer安装和配置
  7. JavaScript判断变量值简单的方法
  8. [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播
  9. CSS3中的弹性布局——&quot;em&quot;的用法
  10. 商业智能SAAS走向中小企业