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. Java反射机制(Class类的使用)

    1:通过无参构造实例化对象 package cn.itcast; /* * 通过无参构造实例化对象 * 通过Class类本身实例化对象,使用newInstance方法 * 需要注意的是:实例化类中存在 ...

  2. 用python实现远程复制 (scp + expect )

    scp 功能很强大,但需要人工输入 password, 当然可以通过把 公钥保存在远程主机的 ~/.ssh 目录中,而后就不用输入password,但这需要配置. 用 sshpass 可能在命令输入 ...

  3. RPG JS:免费开源的跨平台RPG游戏引擎

    RPG JS是一个2D RPG游戏制作引擎,目前版本基于Ease|JS游戏引擎,基于Canvas Engine的新版本即将发布. RPG JS是免费且开源的. RPG JS有着完善的文档支持. RPG ...

  4. HDU 5927 Auxiliary Set (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...

  5. Spring Bean生命周期

    1.Bean的建立:BeanFactory容器寻找Bean的定义信息,读取Bean定义文件,并将其实例化,生成各个Bean实例.2.属性注入:使用依赖注入,Spring按照Bean定义信息配置Bean ...

  6. C++的辅助工具介绍 [转]

    C++的辅助工具介绍 1 文档类  (1) Doxygen  参考站点:http://www.doxygen.org  Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和 ...

  7. 查找DOM

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  9. C语言综述

    1.预处理指令:在变异之前执行的指令. 系统自带的文件用<>,自己写的文件用""; .h成为头文件,用来声明一些常用的函数,假如想使用这些函数,就必须包含这个头文件(注 ...

  10. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...