Frequent values

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1476    Accepted Submission(s): 541

Problem 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
 
有个RMQ的解法,利用游标编码,代码简单,但是理解可能复杂点
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std; int a[N];
struct Tree{
int l,r;
int lv,rv,mv;
}tree[*N]; void PushUp(int l,int r,int idx){
tree[idx].lv = tree[idx<<].lv;
tree[idx].rv = tree[idx<<|].rv;
tree[idx].mv = max(tree[idx<<].mv,tree[idx<<|].mv);
int mid = (l+r)>>;
int len = r- l+;
if(a[mid]==a[mid+]){
if(tree[idx].lv==len-(len>>)) tree[idx].lv+=tree[idx<<|].lv;
if(tree[idx].rv==(len>>)) tree[idx].rv+=tree[idx<<].rv;
tree[idx].mv = max(tree[idx].mv,tree[idx<<].rv+tree[idx<<|].lv);
}
}
void build(int l,int r,int idx){
tree[idx].l = l;
tree[idx].r = r;
if(l==r){
tree[idx].lv = tree[idx].rv = tree[idx].mv = ;
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
PushUp(l,r,idx);
}
int query(int l,int r,int idx){
if(tree[idx].l>=l&&tree[idx].r<=r){
return tree[idx].mv;
}
int mid = (tree[idx].l+tree[idx].r)>>;
int ans = ;
if(l<=mid) ans = max(ans,query(l,r,idx<<));
if(r>mid) ans=max(ans,query(l,r,idx<<|));
if(a[mid]==a[mid+]){
ans = max(ans,min(mid-l+,tree[idx<<].rv)+min(r-mid,tree[idx<<|].lv));
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF,n){
scanf("%d",&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,));
}
}
return ;
}
 

最新文章

  1. iOS 触摸事件与UIResponder(内容根据iOS编程编写)
  2. 解决JSP页面获取的数据库数据乱码问题
  3. TypeScript Handbook 2——接口1(翻译)
  4. tomcat WEB-INF中的结构
  5. .net RPC框架选型
  6. 玩转无线电 -- GPS Hacking (上)
  7. SQL SERVER树型数据处理时,函数递归调用问题,查询根节点,子节点函数
  8. ERROR (ClientException) nova image-list
  9. 使用git的正确姿势
  10. GCC依赖库顺序问题
  11. Python安装MySQLdb并连接MySQL数据库
  12. ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
  13. 在线用户数-Constants
  14. java集合之ArrayList源码解读
  15. SpringBoot之旅第四篇-web开发
  16. 【不遮遮掩掩】Github上传本地代码以及常见问题解决方案
  17. BZOJ4652 NOI2016循环之美(莫比乌斯反演+杜教筛)
  18. python中的全局变量和局部变量(转)
  19. InfluxDB 安装以及使用
  20. asp.net调用js方法

热门文章

  1. IOI2000 Post Office (POJ1160)
  2. Hcharts和Echarts----制作报表的工具
  3. 如何在Linux上安装QQ
  4. uboot的硬件驱动
  5. 图论:最短路-Dijkstra
  6. [Luogu 1640] SCOI2010 连续攻击游戏
  7. mysql binlog日志手动清除
  8. jQuery基本动画
  9. bzoj2516 电梯
  10. 面试精选之Promise