Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16543   Accepted: 5985

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


分析
因为题上说是有序的,就可以求一样的数的数量,再来使用RMQ
开始我也不会做,看了别人的写的,思路还放不开
还记得以前求众数只会用桶排序,现在想起来实在可笑,但是路还很长
AC代码

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #define MAX 100010
  5. using namespace std;
  6. int f[MAX][];
  7. int num[MAX];
  8. int sum[MAX];
  9. int n,Q,ans,a,b;
  10. int _max(int a,int b)
  11. {
  12. return a>b?a:b;
  13. }
  14. void ST()
  15. {
  16. for(int i=;i<=n;i++)
  17. f[i][]=sum[i];
  18. int k=log((double)(n+))/log(2.0);
  19. for(int i=;i<=k;i++)
  20. for(int j=;j+(<<i)<=n+;j++)//n+1
  21. f[j][i]=_max(f[j][i-],f[j+(<<(i-))][i-]);
  22. }
  23. int RMQ_Query(int l,int r)
  24. {
  25. if(l>r)return ;
  26. int k=log((double)(r-l+))/log(2.0);
  27. return _max(f[l][k],f[r-(<<k)+][k]);//r-(1<<k)一定要+1
  28. }
  29. int main()
  30. {
  31. while(scanf("%d",&n)!=EOF,n)
  32. {
  33. scanf("%d",&Q);
  34. for(int i=;i<=n;i++)
  35. {
  36. scanf("%d",num+i);
  37. if(i==)
  38. {
  39. sum[i]=;
  40. continue;
  41. }
  42. if(num[i]==num[i-])
  43. sum[i]=sum[i-]+;
  44. else sum[i]=;
  45. }
  46. ST();
  47. while(Q--)
  48. {
  49. scanf("%d%d",&a,&b);
  50. if(a==b||num[a]==num[b])
  51. {
  52. cout<<b-a+<<endl;
  53. continue;
  54. }
  55. int t=a;
  56. while(num[t]==num[t-]&&t<=b)//t 必须小于 b
  57. t++;
  58. int cnt=RMQ_Query(t,b);
  59. ans=_max(cnt,t-a);
  60. printf("%d\n",ans);
  61. }
  62. }
  63. return ;
  64. }
 

poj3368(RMQ——ST)的更多相关文章

  1. [NOI2010]超级钢琴(RMQ+堆)

    小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中Ai可正可负 ...

  2. HDU 5726 GCD(RMQ+二分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...

  3. BZOJ 1067:[SCOI2007]降雨量(RMQ+思维)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1067 题意:…… 思路:首先我们开一个数组记录年份,一个记录降雨量,因为年份是按升序排列的,所以我们 ...

  4. POJ 3419 Difference Is Beautiful(RMQ变形)

    题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...

  5. 2015 多校联赛 ——HDU5289(二分+ST)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. (RMQ版)LCA注意要点

    inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...

  7. 【BZOJ 2006】2006: [NOI2010]超级钢琴(RMQ+优先队列)

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2792  Solved: 1388 Description 小 ...

  8. UVA - 1618 Weak Key(RMQ算法)

    题目: 给出k个互不相同的证书组成的序列Ni,判断是否存在4个证书Np.Nq.Nr.Ns(1≤p<q<r<s≤k)使得Nq>Ns>Np>Nr或者Nq<Ns&l ...

  9. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

随机推荐

  1. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  2. Delphi 中记录类型 给记录指针赋值。

    PPersion=^TPersion;  TPersion=packed record     Name:string;     Sex:string;     Clasee:string;  end ...

  3. lua面试基础知识

    1.lua中八种基础类型:nil(空),boolean(布尔),number(数字),string(字符串),userdata(自定义类型),function(函数),thread(线程),table ...

  4. 历史命令:history

    [root@linux ~]# history [n][root@linux ~]# history [-c][root@linux ~]# history [-raw] histfiles参数:n ...

  5. SQL Server 2012 通用分页存储过程

    创建存储过程: USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCedure [dbo].[Split ...

  6. Linux Windows 修改键盘映射

    Linux 下是编辑 ~/.Xmodmap 文件 remove Lock = Caps_Lockkeysym Escape = Caps_Lockkeysym Caps_Lock = Escapead ...

  7. 第四十章 微服务CICD(2)- jenkins(war版)

    一.下载 官网下载war包,放在tomcat下的webapps下, 第一章 tomcat安装与启动 第二章 部署war包到tomcat jenkins:2.19.1版本. 二.修改编码为utf-8 在 ...

  8. em 换算

    1 ÷ 父元素的font-size × 需要转换的像素值 = em值 http://www.w3cplus.com/css/px-to-em

  9. JS编写背景图切换

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

  10. curl 模拟登录微信公众平台带验证码

    这段时间一直写个项目, 从切图到前端到后台都要搞定,真tm累. 今天下午手残,不停用错误的密码去模拟登录微信公众平台,结果后来出现验证码,瞬间悲剧(菜鸟从来没搞过带验证码的). 研究了一下,发现其实很 ...