描述

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.

输入

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 nintegers 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.

输出

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

样例输入

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

样例输出

1
4
3

提示

A naive algorithm may not run in time!
题目大意:
给定一个序列,若干个查询,查询区间内出现最频繁的数的个数。
先预处理当前位置的值前面和它相同的个数,例如样例转化为1,2,1,2,3,4,1,1,2,3。然后ST预处理区间最大值,单纯的查询最大值会出现问题,例如[4,7]ST表得出的答案为4,所以可以将我们要查询的左端点更新一下,更新到一个新的1的位置,这样就不会出现那种问题了。
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e5+;
int n,m,a[N],rt[N],pre[N];
int st[N][];
int query(int l,int r)
{
if(l>r) return ;
int k=log2(r-l+);
return max(st[l][k],st[r-(<<k)+][k]);
}
int main()
{
while(~scanf("%d",&n),n)
{
memset(st,,sizeof st);
scanf("%d",&m);
a[]=INF;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
if(a[i]==a[i-]) pre[i]=pre[i-]+;
else pre[i]=;
}
for(int i=;i<=n;i++)
st[i][]=pre[i];
for(int j=;j<;j++)
for(int i=;i+(<<(j-))<=n;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
rt[n]=n;
for(int i=n-;i>=;i--)
{
if(a[i]==a[i+]) rt[i]=rt[i+];
else rt[i]=i;
}
while(m--)
{
int l,r,tmp;
scanf("%d%d",&l,&r);
if(rt[l]==rt[l-]) tmp=min(r,rt[l])+;
else tmp=l;
int ans=query(tmp,r);
ans=max(ans,tmp-l);
printf("%d\n",ans);
}
}
return ;
}

Frequent values(ST)的更多相关文章

  1. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  2. UVA-11235 Frequent values (RMQ)

    题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...

  3. 【POJ 3368】Frequent values(RMQ)

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  4. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  5. poj 3368 Frequent values(段树)

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

  6. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  7. 51nod——1174 区间中最大的数(ST)

    题目链接 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数 ...

  8. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  9. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

随机推荐

  1. SQL server的一个分割表值函数

    CREATE FUNCTION [dbo].[Fn_Split] ( @SplitString text, -- 如果要传入NText类型,下面需要相应的修改,注释行为NText下同 ) = ','- ...

  2. ZOJ 3471 Most Powerful (状压DP,经典)

    题意: 有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子.已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量? 思路: 一开始以为是找一个有序序列,使得能量最大 ...

  3. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

  4. UVA 10891 Game of Sum (决策优化)

    这是一个零和博弈,最高得分只和序列以及谁先手有关. d[i][j],表示i到j的序列当前取的这个人的最高得分,转移以后状态是新的区间和另一个人取,从中取最小值. 决策的最小值也可递推. #includ ...

  5. CPP-基础:extern"C"

    简介:extern "C" 包含双重含义,从字面上即可得到:首先,被它修饰的目标是“extern”的:其次,被它修饰的目标是“C”的.让我们来详细解读这两重含义. 含义: 1.被e ...

  6. 基于arcgis api for js高速公路智能化智慧公路养护WebGIS开源系统

    伴随着高速公路建设进程加快,其涉及信息量增大.类型多样.地点分布广,传统的信息管理方式已不适应公路建设迅速发展的需要,而目前能对高速公路在设计.施工.养护等阶段的各类信息综合进行管理的信息系统尚较少见 ...

  7. 将unity3d项目嵌入到Android App中使用

    创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...

  8. ReactiveCocoa入门-part1

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  9. HTML与XHTML区别

    1. html超文本标记语言,xhtml可扩展超文本标记语言,xhtml是将html作为xml的应用重新定义的一个标准. 2. xhtm比html的代码规则严格很多,例如'a < b'在xhtm ...

  10. MyBatis的增删改查操作

    搭建好mybatis之后 进行对数据库的操作 添加语句 在映射文件中添加语句 insert into student(name,age,score) values(#{name},#{age},#{s ...