Problem F: Frequent values

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 Specification

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 Specification

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 <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; int dp[MS][];
int a[MS],cnt[MS];
int num[MS],l[MS],r[MS];
int n,q; void RMQ_init()
{
for(int i=;i<n;i++)
dp[i][]=cnt[i];
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
} int RMQ(int l,int r)
{
int k=;
while(<<(k+)<=r-l+)
k++;
return max(dp[l][k],dp[r-(<<k)+][k]);
} int main()
{
while(scanf("%d%d",&n,&q)==&&n)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
a[n]=a[n-]+;
int start=;
int id=;
for(int i=;i<=n;i++)
{
if(i>&&a[i]>a[i-])
{
for(int j=start;j<i;j++)
r[j]=i-;
cnt[id]=i-start;
id++;
start=i;
}
l[i]=start;
num[i]=id;
}
n=id;
RMQ_init();
int x,y;
while(q--)
{
scanf("%d%d",&x,&y);
x--;
y--;
int ans=;
if(num[x]==num[y])
{
printf("%d\n",y-x+);
continue;
}
ans=max(r[x]-x+,y-l[y]+);
if(num[x]+<num[y])
ans=max(ans,RMQ(num[x]+,num[y]-));
printf("%d\n",ans);
} }
return ;
}

												

H - Frequent values的更多相关文章

  1. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  2. POJ 3368:Frequent values

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14764   Accepted: 5361 ...

  3. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  4. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  5. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  6. 【暑假】[实用数据结构]UVa11235 Frequent values

    UVa 11235 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11241   Accep ...

  7. [HDU 1806] Frequent values

    Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

  9. poj 3368 Frequent values(段树)

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

随机推荐

  1. 第三百二十六天 how can I 坚持

    今天元宵节啊,晚上去蓝色港湾看了看灯光节,快冻死了,人倒是挺多. 其他没啥了. 还有晚上吃了几个元宵. 好像冻感冒了,有点头晕. 睡觉.

  2. Django Signals 从实践到源码分析(转)

    原文:http://foofish.net/blog/66/django-signals 当某个事件发生的时候,signal(信号)允许senders(发送者)用来通知receivers(接收者),通 ...

  3. [翻译]创建ASP.NET WebApi RESTful 服务(7)

    实现资源分页 本章我们将介绍几种不同的结果集分页方式,实现手工分页,然后将Response通过两个不同的方式进行格式化(通过Response的Envelop元数据或header). 大家都知道一次查询 ...

  4. Javascript事件处理进阶

    这篇文章是我在看乌龟书<编写可维护的Javascript>发现的一篇写的非常好的章节,在这里我并不会教大家什么是绑定事件等比较基础的事.有兴趣了解DOM事件的同学们,可以去w3cschoo ...

  5. 适应所有浏览器的cookie

    //设置cookie的方法 weiyingfunction SetCookie(a, b) {        var d = new Date();    var v = arguments;    ...

  6. HDU 1847 Good Luck in CET-4 Everybody!(找规律,或者简单SG函数)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. Java反射机制(取得类的结构)

    通过反射得到一个类中的完整的结构,就要使用java.lang.reflect包中的以下几个类:   Constructor:表示类中的构造方法 Field:表示类中的属性 Method:表示类中的方法 ...

  8. 关于“未使用GUID分区表”无法安装的解决方案

    原帖链接:http://itc.do-johodai.ac.jp/~s0823612/ 原版的Mac不能安装在mbr分区.必须得用GUID分区,其实装在mbr也可以,需要修改两个文件一个是OSInst ...

  9. hdu 2881 Jack's struggle(DP)

    对于所有的任务所在的地点按照时间排序,可以将其看成是一道非常简单的“天上掉馅饼”的题的二维版本. 将这些任务看成从天而降的馅饼,统计有哪些之前馅饼掉落的位置可以在两者时间差内到达该点的点,取其最大值. ...

  10. CCPC总结

    [印象·南阳] 10月15日出发,威海—烟台—郑州—南阳,一路上欢声笑语,从谁是卧底到各类纸牌游戏,也是欢乐.在从郑州到南阳的车上,对面的好像是河南当地的学长,感叹道工作不易的样子,说还是学生时代最为 ...