Frequent values
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

  1. 10 3
  2. -1 -1 1 1 1 1 3 10 10 10
  3. 2 3
  4. 1 10
  5. 5 10
  6. 0

Sample Output

  1. 1
  2. 4
  3. 3

Source


调试到00:30
白书上的
把相同的RLE,cnt段,a是数值,c出现次数,left和right是这一段左右到原来位置那里,id[p]是p位置的编号
用RMQ快速求id[l]+1到id[r]-1段的最大值,其他的直接加减就行了
  1. //
  2. // main.cpp
  3. // poj3368
  4. //
  5. // Created by Candy on 10/8/16.
  6. // Copyright © 2016 Candy. All rights reserved.
  7. //
  8.  
  9. #include <cstdio>
  10. #include <algorithm>
  11. #include <cstring>
  12. #include <cmath>
  13. using namespace std;
  14. const int N=1e5+,INF=1e9;
  15. inline int read(){
  16. char c=getchar();int x=,f=;
  17. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  18. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  19. return x*f;
  20. }
  21. int n,q,l,r;
  22. int x,last,cnt=,a[N],v[N],c[N],id[N],left[N],right[N];
  23. int st[N][];
  24. void initRMQ(){
  25. memset(st,,sizeof(st));
  26. for(int i=;i<=cnt;i++) st[i][]=c[i];
  27. for(int j=;(<<j)<=cnt;j++)
  28. for(int i=;i+(<<j)-<=cnt;i++)
  29. st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
  30. }
  31. int rmq(int l,int r){
  32. if(l>r) return ;
  33. int k=log(r-l+)/log();
  34. return max(st[l][k],st[r-(<<k)+][k]);
  35. }
  36. int main(int argc, const char * argv[]) {
  37. while((n=read())){
  38. q=read();
  39. memset(c,,sizeof(c));
  40. memset(left,,sizeof(left));
  41. memset(right,,sizeof(right));
  42. v[]=v[n+]=INF;
  43. for(int i=;i<=n;i++){
  44. v[i]=read();
  45. if(v[i]==v[i-]){
  46. c[cnt]++;
  47. right[cnt]=i;
  48. id[i]=cnt;
  49. }else{
  50. cnt++;
  51. a[cnt]=v[i];
  52. c[cnt]++;
  53. left[cnt]=right[cnt]=i;
  54. id[i]=cnt;
  55. }
  56. }
  57. initRMQ();
  58. //for(int i=1;i<=cnt;i++) printf("init %d %d %d %d\n",a[i],c[i],left[i],right[i]);
  59. for(int i=;i<=q;i++){
  60. l=read();r=read();
  61. int ans=;
  62. if(id[l]==id[r]) ans=r-l+;
  63. else{
  64. ans=max(right[id[l]]-l+,r-left[id[r]]+);
  65. ans=max(ans,rmq(id[l]+,id[r]-));
  66. }
  67. printf("%d\n",ans);
  68. }
  69. }
  70. //printf("\n\n\n%d %d %d %d",id[1]+1,c[2],id[10]-1,rmq(id[1]+1,id[10]-1));
  71. return ;
  72. }
 

POJ3368Frequent values[RMQ 游程编码]的更多相关文章

  1. poj3368Frequent values(RMQ)

    http://poj.org/problem?id=3368 追完韩剧 想起这题来了 想用线段树搞定来着 结果没想出来..然后想RMQ 想出来了 算是离散吧 把每个数出现的次数以及开始的位置及结束的位 ...

  2. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  3. UVA 11235Frequent values(RMQ)

    训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...

  4. poj 3368 Frequent values(RMQ)

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

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

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

  6. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  7. [poj3368]Frequent values(rmq)

    题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...

  8. POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

    题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...

  9. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

随机推荐

  1. C#如何静态调用C++中的方法(静态调用dll)

    当我们想要在C#中使用C++项目的方法时,这个时候就可以通过调用C++项目的dll来实现,它有静态和动态调用两种方法. DLL(Dynamic Link Library)文件为动态链接库文件,又称“应 ...

  2. 使用SwipeListView实现滑动效果

    QQ的滑动删除效果很不错,要实现这种效果,可以使用SwipeListView.1. 下载com.fortysevendeg.swipelistview这个项目(以前GitHub上有,现在GitHub上 ...

  3. ZHA profile与ZLL profile的一个例子

    ZHA Coordinator 如何控制ZLL Light/Philips Hue Light 缩写: ZHA: ZigBee Home Automation profile ZLL:  ZigBee ...

  4. Cats(3)- freeK-Free编程更轻松,Free programming with freeK

    在上一节我们讨论了通过Coproduct来实现DSL组合:用一些功能简单的基础DSL组合成符合大型多复杂功能应用的DSL.但是我们发现:cats在处理多层递归Coproduct结构时会出现编译问题.再 ...

  5. Lind.DDD.Domain.IOwnerBehavor对实体的意义

    回到目录 对于Lind.DDD架构,我之前写了不少文章,对于它的Domain模式也介绍了不少,像之前的IEntity,ILogicDeleteBehavor,IModifyBehavor,IStatu ...

  6. 使用SQLServer同义词和SQL邮件,解决发布订阅中订阅库丢失数据的问题

    最近给客户做了基于SQLServer的发布订阅的“读写分离”功能,但是某些表数据很大,经常发生某几条数据丢失的问题,导致订阅无法继续进行.但是每次发现问题重新做一次发布订阅又非常消耗时间,所以还得根据 ...

  7. 源映射(Source Map)详解

    一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...

  8. CSS、j's单行、多行文本溢出显示省略号

    在项目中,由于实际描述文字过多,导致初始页面纵向长度过长,也使得余下信息利用率降低:所以在文字过多的时候,初始化限制行数是有必要的 1. CSS单行文本溢出,显示省略号 <div style=& ...

  9. javascript面试题:如何把一句英文每个单词首字母大写?

    上周看到大家在JS群讨论如何把一句英文句子单词收割字母大写,大家都说用正则简单,对于正则还是有点模糊,于是乎自己敲了下 //面试题:如何把一句英文每个单词首字母大写? var str="wh ...

  10. JS特效之Tab标签切换

    在我们平时浏览网站的时候,经常看到的特效有图片轮播.导航子菜单的隐藏.tab标签的切换等等.这段时间学习了JS后,开始要写出一些简单的特效.今天我也分享一个简单tab标签切换的例子.先附上代码: HT ...