Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13516   Accepted: 4971

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

由于是非递减数列。同样的元素必须是连在一块的。能够统计不同元素的个数,以元素的个数建树,给出一段区间,再二分查找出如今第几个元素,特殊考虑最前和最后,中间的用线段树。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=100000+100;
int p;
int tree[maxn<<2];
int num[maxn],sum[maxn];
int a[maxn];
void build(int rt,int l,int r)
{
if(l==r)
{
tree[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return tree[rt];
}
int ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans=query(rt<<1,l,mid,L,R);
if(R>mid)
ans=max(ans,query(rt<<1|1,mid+1,r,L,R));
return ans;
} int find(int k)
{
int l=1,r=p;
int m;
while (l<=r)
{
m=(l+r)>>1;
if(k>sum[m])
l=m+1;
else if(k<sum[m])
r=m-1;
else
break;
}
if(sum[m-1]>=k)
return m-1;
else if(sum[m]>=k)
return m;
else
return m+1;
}
int main()
{
int n,m;
while(~scanf("%d",&n)&&n)
{
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(tree,0,sizeof(tree));
scanf("%d",&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int temp;
temp=0;
p=1;
num[p]=1;
sum[0]=0;
for(int i=1;i<n;i++)
{
if(a[temp]==a[i])
{
num[p]++;
}
else
{
temp=i;
sum[p]=sum[p-1]+num[p];
p++;
num[p]=1;
}
}
sum[p]=sum[p-1]+num[p];
build(1,1,p);
int l,r;
for(int i=0;i<m;i++)
{
int u,v;
int ans=0;
scanf("%d%d",&l,&r);
u=find(l);
v=find(r);
ans=sum[u]-l+1;
if(u==v)
{
printf("%d\n",r-l+1);
continue;
}
if(u+1<=v-1)
ans=max(ans,query(1,1,p,u+1,v-1));
ans=max(ans,r-sum[v-1]);
printf("%d\n",ans);
}
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. ECharts 初识(基于MVC+jQuery+Angularjs实现的Demo)
  2. 参加了iDOF2016会议,发表演讲“油田SOA与云平台的系统思考与实践”
  3. 如何理解和熟练运用js中的call及apply?
  4. webpack初学
  5. Win7 64位ORACLE取数字乱码的解决
  6. sqlserver的IO性能检查
  7. Hadoop with tool interface
  8. 友盟分享--集成QQ和微信
  9. JAVA大数类
  10. (二)Knockout - ViewModel 的使用
  11. php ajax 下拉加载数据
  12. Excel自动换行、Export2Excel 自动换行
  13. 【查漏补缺】File的path、absolutePath和canonicalPath的区别
  14. Kubernetes – Ingress
  15. python单下划线与双下划线的区别
  16. 让delphi像C语言一样灵活
  17. shell 脚本 计算 1加到100 的和
  18. 5 个非常有用的 Laravel Blade 指令,你用过哪些?
  19. IIS与Asp.net
  20. 2-ubuntu下访问window的磁盘

热门文章

  1. Windows Azure 网站自愈
  2. Linux设置高分辨率后无法进入X系统
  3. 【翻译】探究Ext JS 5和Sencha Touch的布局系统
  4. ofbiz学习笔记
  5. [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
  6. Ext JS学习第十七天 事件机制event(二)
  7. js中使用this的一些注意事项
  8. ADO.NET基础笔记
  9. resolve &quot;Undefined attribute name&quot; warning for Angular &quot;ng-&quot; attributes in HTML files
  10. 一些指令 &amp; 一些知识 (Linux Spring log4j...)