POJ3368Frequent values[RMQ 游程编码]
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17581 | Accepted: 6346 |
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
- //
- // main.cpp
- // poj3368
- //
- // Created by Candy on 10/8/16.
- // Copyright © 2016 Candy. All rights reserved.
- //
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <cmath>
- using namespace std;
- const int N=1e5+,INF=1e9;
- inline int read(){
- char c=getchar();int x=,f=;
- while(c<''||c>''){if(c=='-')f=-;c=getchar();}
- while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
- return x*f;
- }
- int n,q,l,r;
- int x,last,cnt=,a[N],v[N],c[N],id[N],left[N],right[N];
- int st[N][];
- void initRMQ(){
- memset(st,,sizeof(st));
- for(int i=;i<=cnt;i++) st[i][]=c[i];
- for(int j=;(<<j)<=cnt;j++)
- for(int i=;i+(<<j)-<=cnt;i++)
- st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
- }
- int rmq(int l,int r){
- if(l>r) return ;
- int k=log(r-l+)/log();
- return max(st[l][k],st[r-(<<k)+][k]);
- }
- int main(int argc, const char * argv[]) {
- while((n=read())){
- q=read();
- memset(c,,sizeof(c));
- memset(left,,sizeof(left));
- memset(right,,sizeof(right));
- v[]=v[n+]=INF;
- for(int i=;i<=n;i++){
- v[i]=read();
- if(v[i]==v[i-]){
- c[cnt]++;
- right[cnt]=i;
- id[i]=cnt;
- }else{
- cnt++;
- a[cnt]=v[i];
- c[cnt]++;
- left[cnt]=right[cnt]=i;
- id[i]=cnt;
- }
- }
- initRMQ();
- //for(int i=1;i<=cnt;i++) printf("init %d %d %d %d\n",a[i],c[i],left[i],right[i]);
- for(int i=;i<=q;i++){
- l=read();r=read();
- int ans=;
- if(id[l]==id[r]) ans=r-l+;
- else{
- ans=max(right[id[l]]-l+,r-left[id[r]]+);
- ans=max(ans,rmq(id[l]+,id[r]-));
- }
- printf("%d\n",ans);
- }
- }
- //printf("\n\n\n%d %d %d %d",id[1]+1,c[2],id[10]-1,rmq(id[1]+1,id[10]-1));
- return ;
- }
POJ3368Frequent values[RMQ 游程编码]的更多相关文章
- poj3368Frequent values(RMQ)
http://poj.org/problem?id=3368 追完韩剧 想起这题来了 想用线段树搞定来着 结果没想出来..然后想RMQ 想出来了 算是离散吧 把每个数出现的次数以及开始的位置及结束的位 ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- UVA 11235Frequent values(RMQ)
训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...
- [poj3368]Frequent values(rmq)
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
随机推荐
- C#如何静态调用C++中的方法(静态调用dll)
当我们想要在C#中使用C++项目的方法时,这个时候就可以通过调用C++项目的dll来实现,它有静态和动态调用两种方法. DLL(Dynamic Link Library)文件为动态链接库文件,又称“应 ...
- 使用SwipeListView实现滑动效果
QQ的滑动删除效果很不错,要实现这种效果,可以使用SwipeListView.1. 下载com.fortysevendeg.swipelistview这个项目(以前GitHub上有,现在GitHub上 ...
- ZHA profile与ZLL profile的一个例子
ZHA Coordinator 如何控制ZLL Light/Philips Hue Light 缩写: ZHA: ZigBee Home Automation profile ZLL: ZigBee ...
- Cats(3)- freeK-Free编程更轻松,Free programming with freeK
在上一节我们讨论了通过Coproduct来实现DSL组合:用一些功能简单的基础DSL组合成符合大型多复杂功能应用的DSL.但是我们发现:cats在处理多层递归Coproduct结构时会出现编译问题.再 ...
- Lind.DDD.Domain.IOwnerBehavor对实体的意义
回到目录 对于Lind.DDD架构,我之前写了不少文章,对于它的Domain模式也介绍了不少,像之前的IEntity,ILogicDeleteBehavor,IModifyBehavor,IStatu ...
- 使用SQLServer同义词和SQL邮件,解决发布订阅中订阅库丢失数据的问题
最近给客户做了基于SQLServer的发布订阅的“读写分离”功能,但是某些表数据很大,经常发生某几条数据丢失的问题,导致订阅无法继续进行.但是每次发现问题重新做一次发布订阅又非常消耗时间,所以还得根据 ...
- 源映射(Source Map)详解
一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...
- CSS、j's单行、多行文本溢出显示省略号
在项目中,由于实际描述文字过多,导致初始页面纵向长度过长,也使得余下信息利用率降低:所以在文字过多的时候,初始化限制行数是有必要的 1. CSS单行文本溢出,显示省略号 <div style=& ...
- javascript面试题:如何把一句英文每个单词首字母大写?
上周看到大家在JS群讨论如何把一句英文句子单词收割字母大写,大家都说用正则简单,对于正则还是有点模糊,于是乎自己敲了下 //面试题:如何把一句英文每个单词首字母大写? var str="wh ...
- JS特效之Tab标签切换
在我们平时浏览网站的时候,经常看到的特效有图片轮播.导航子菜单的隐藏.tab标签的切换等等.这段时间学习了JS后,开始要写出一些简单的特效.今天我也分享一个简单tab标签切换的例子.先附上代码: HT ...