Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19998   Accepted: 7180

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

Source

 
  • 题目中序列是非递增序列,这样有序的序列有利于我们减少搜查的数据量
  • 我们可以利用相同数都在一起的性质给序列分块
  • 然后对于一个询问,我们总是整块地查询
  • 就是说对于一个查询我们先得出最左边分块出现次数和最右边分块出现次数,然后中间剩余分块可以用ST表RMQ查询
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e6 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int fac[], dp[maxn][];
int l[maxn], r[maxn];
void ST(int n){
int k=(int)(log((double)n)/log(2.0));
for(int i=;i<=n;i++)
dp[i][]=r[i]-l[i]+;
for(int j=;j<=k;j++)
for(int i=;i+fac[j]-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+fac[j-]][j-]);
}
int RMQ(int u, int v){
int k=(int)(log((double)(v-u+))/log(2.0));
return max(dp[u][k],dp[v-fac[k]+][k]);
}
int n, m, u, v, ans, tot, a[maxn], b[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%d",&n)){
if(==n){break;}
scanf("%d",&m);
a[]=1e6+;
tot=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i-]!=a[i]){
tot++;
b[i]=tot;
l[tot]=i;
r[tot]=i;
}else{
b[i]=tot;
r[tot]++;
}
// b[i]=tot;
}
ST(tot);
while(m--){
scanf("%d %d",&u,&v);
if(b[u]==b[v]){
ans=v-u+;
}else if(b[u]+==b[v]){
ans=max(r[b[u]]-u+,v-l[b[v]]+);
}else{
ans=max(r[b[u]]-u+,v-l[b[v]]+);
ans=max(ans,RMQ(b[u]+,b[v]-));
}
printf("%d\n",ans);
}
}
return ;
}

POJ_3368_Frequent values的更多相关文章

  1. 全部省市县数据库(MySQL脚本) (转)

    /*MySQL - 5.5.47 *************//*!40101 SET NAMES utf8 */; create table `base_area` (    `codeid` me ...

  2. JS+MySQL获取 京东 省市区 地区

    采集了一下JD的省市区地区 (非常简单,只是做个记录) 1.建表:account_area   2.进入页面: https://reg.jd.com/reg/company 在浏览器(Firefox) ...

  3. salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)

    本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...

  4. Django models .all .values .values_list 几种数据查询结果的对比

    Django models .all .values .values_list 几种数据查询结果的对比

  5. UVA - 11235 Frequent values

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

  6. codeforces 360 E - The Values You Can Make

    E - The Values You Can Make Description Pari wants to buy an expensive chocolate from Arya. She has  ...

  7. Default Parameter Values in Python

    Python’s handling of default parameter values is one of a few things that tends to trip up most new ...

  8. iOS开发之SQLite--C语言接口规范(四) —— Result Values From A Query

    数据库的在上一篇博客中<SQLite之C语言接口规范(三)——Binding Values To Prepared Statements>用到了如何从查询结果中取出结果值.今天的博客就详细 ...

  9. mybatis报错invalid types () or values ()解决方法

      原因: Pojo类User没提供无参数构造方法, 加上该构造方法后,问题解决 ### Cause: org.apache.ibatis.reflection.ReflectionException ...

随机推荐

  1. phonegap入门–1 Android 开发环境搭建

    一.JDK 安装JDK,安装包中包含了JDK和JRE两部分,建议将它们安装在同一个盘符下面. 配置环境变量: 1.右键点击我的电脑,选择属性,点击高级选项卡,选择环境变量. 2.找到Path变量名(无 ...

  2. iOS UTI(统一类型标识)

    同一类型标识符(Uniform Type Identifier,UTI)代表IOS信息共享的中心组件.可以把它看成下一代的MIME类型.UTI是标识资源类型(比如图像和文本)的字符串,他们制定哪些类型 ...

  3. SELECT中常用的子查询操作

    MySQL中的子查询 是在MySQL中经常使用到的一个操作,不仅仅是用在DQL语句中,在DDL语句.DML语句中也都会常用到子查询. 子查询的定义: 子查询是将一个查询语句嵌套在另一个查询语句中: 在 ...

  4. script 里写 html 模版

    js模版引擎(例如:template.js 或 handlebars.js)一般都用<script>标签来存放模版的内容 1)模版写在<script>标签和写在<div& ...

  5. Flash XSS 漏洞实例

    www.bsdxm.com/zeroclipboard/ZeroClipboard.swf?id=\"))}catch(e){alert(/xss/);}//&width=500&a ...

  6. SaltStack 使用 Jinja2 模板

    Jinja2 是基于 python 的一个模板引擎,如下,使用 Jinja2 实现根据不同的操作系统分发不同的文件: [root@localhost ~]$ cat /srv/salt/test.sl ...

  7. 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu

    环境:VMWare Player 5.0.2,Ubuntu 13.10  VMWare共有3种网络连接模式,分别是: 1. bridged(桥接模式):虚拟机将直接连接到物理局域网,使自身独立于宿主机 ...

  8. INSTALL_FAILED_USER_RESTRICTED

    我这里出问的问题是在 清单文件中 <provider <mate_data 中少了   android:resource="@xml/filepaths" 加上就好 了

  9. openjdk源码阅读导航

    转自:http://rednaxelafx.iteye.com/blog/1549577 这是链接帖.主体内容都在各链接中. 怕放草稿箱里过会儿又坑掉了,总之先发出来再说…回头再慢慢补充内容. 先把I ...

  10. 在MyEclipse(2015)中上传项目到github的步骤(很详细)

    (图文)在MyEclipse(2015)中上传项目到github的步骤(很详细) git|smartGit使用详解 SmartGit使用教程