1. #include<cstdio>
  2. #include<cstring>
  3.  
  4. const int maxn=1e5+;
  5. const int inf=0x3f3f3f3f;
  6.  
  7. inline int max(int x,int y)
  8. {
  9. return x>y?x:y;
  10. }
  11.  
  12. int a[maxn];
  13. int left[maxn];
  14. int right[maxn];
  15. int num[maxn];
  16. int dp[maxn][];
  17.  
  18. void init()
  19. {
  20. memset(a,,sizeof a);
  21. memset(left,-,sizeof left);
  22. memset(right,-,sizeof right);
  23. }
  24.  
  25. void init_rmq(int n)
  26. {
  27. for(int i=;i<=n;i++)
  28. dp[i][]=a[i];
  29. for(int j=;(<<j)<=n;j++)
  30. {
  31. for(int i=;i+(<<j)-<=n;i++)
  32. {
  33. dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
  34. }
  35. }
  36. }
  37.  
  38. int query(int l,int r)
  39. {
  40. if(l>r)
  41. return ;
  42. int k=;
  43. while((<<(k+))<=r-l+)
  44. k++;
  45. return max(dp[l][k],dp[r-(<<k)+][k]);
  46. }
  47.  
  48. void solve(int n,int q)
  49. {
  50. init_rmq(n);
  51. for(int i=;i<q;i++)
  52. {
  53. int L,R;
  54. scanf("%d%d",&L,&R);
  55. if(num[L]==num[R])
  56. {
  57. printf("%d\n",R-L+);
  58. continue;
  59. }
  60. int ans=max(right[num[L]]-L+,R-left[num[R]]+);
  61. ans=max(ans,query(num[L]+,num[R]-));
  62. printf("%d\n",ans);
  63. }
  64. return ;
  65. }
  66.  
  67. int main()
  68. {
  69. int cnt;
  70. while(scanf("%d",&cnt))
  71. {
  72. if(!cnt)
  73. break;
  74. int q;
  75. scanf("%d",&q);
  76. init();
  77. int pre=-inf;
  78. int n=;
  79. for(int i=;i<=cnt;i++)
  80. {
  81. int u;
  82. scanf("%d",&u);
  83. if(u!=pre)
  84. {
  85. right[n]=i-;
  86. a[++n]=;
  87. left[n]=i;
  88. }
  89. else
  90. a[n]++;
  91. num[i]=n;
  92. pre=u;
  93. }
  94. right[n]=cnt;
  95.  
  96. solve(n,q);
  97. }
  98. return ;
  99. }

POJ 3368 Frequent values RMQ 训练指南 好题的更多相关文章

  1. poj 3368 Frequent values(RMQ)

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

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

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

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

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

  4. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  5. POJ 3368 Frequent values (基础RMQ)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  6. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

  7. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  8. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  9. [RMQ] [线段树] POJ 3368 Frequent Values

    一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...

随机推荐

  1. URAL 1218 Episode N-th: The Jedi Tournament(强连通分量)(缩点)

    Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Kni ...

  2. hihoCoder#1107 : Shortest Proper Prefix (前缀树)

    题目大意:在n个单词中,如果以s作为前缀的单词个数不超过5个,那么称s为proper prefix.如果s为proper prefix并且s的任何一个前缀(不包括s)都不是proper prefix, ...

  3. 黑马程序员——JAVA基础之包,权限

    ------- android培训.java培训.期待与您交流! ---------- 包(package) 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称的是:包 ...

  4. caffe: train error: Serializing 25 layers--- Check failed: proto.SerializeToOstream(&output)

    I0221 21:47:41.826748  6797 solver.cpp:259]     Train net output #0: loss = 0.00413362 (* 1 = 0.0041 ...

  5. nginx log记录请求的头信息

    记录访问的log,为了在出现特殊情况时,方便检查出现问题的地方.log_format accesslog ‘$remote_addr – $remote_user [$time_local] “$re ...

  6. ExtJs 入门教程

    http://www.cnblogs.com/iamlilinfeng/archive/2012/12/31/2840663.html

  7. PHP GC垃圾回收机制之引用变量回收周期疑问

    普通的引用变量的销毁大家都知道, 当unset的时候如果refcount = 0 则认为无用, 销毁. 但是手册中提到一点会有递归引用的问题,很是奇葩 代码如下 <?php $a = 1; $a ...

  8. playframework1.x的eclipse插件开源-playtools

    playtools介绍 playframework(1.2.x)是一款令人兴奋的java restful风格的web框架,使用它已经有两年多. 其中结合eclipse开发项目往往要不断进行cmd窗口切 ...

  9. 05-Java 集合类详解

    (1)Java集合-Collection A.集合可以理解为一个动态的对象数组,不同的是集合中的对象内容可以任意扩充 B.集合特点:性能高,容易扩展和修改 C.Collection的常用子类:List ...

  10. Oracle数据库—— 游标的创建和应用

    一.涉及内容 游标的创建与应用 二.具体操作 (一)填空题 1.PL/SQL 程序块主要包含3个部分:声明部分.(执行部分 ).异常处理部分. 2.自定义异常必须使用(RAISE )语句引发. (二) ...