博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3368(RMQ——ST)
阅读量:7026 次
发布时间:2019-06-28

本文共 1935 字,大约阅读时间需要 6 分钟。

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

10 3-1 -1 1 1 1 1 3 10 10 102 31 105 100

Sample Output

143

Source


分析
因为题上说是有序的,就可以求一样的数的数量,再来使用RMQ
开始我也不会做,看了别人的写的,思路还放不开
还记得以前求众数只会用桶排序,现在想起来实在可笑,但是路还很长
AC代码
1 #include
2 #include
3 #include
4 #define MAX 100010 5 using namespace std; 6 int f[MAX][20]; 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=1;i<=n;i++)17 f[i][0]=sum[i];18 int k=log((double)(n+1))/log(2.0);19 for(int i=1;i<=k;i++)20 for(int j=1;j+(1<
<=n+1;j++)//n+121 f[j][i]=_max(f[j][i-1],f[j+(1<<(i-1))][i-1]); 22 }23 int RMQ_Query(int l,int r)24 {25 if(l>r)return 0;26 int k=log((double)(r-l+1))/log(2.0);27 return _max(f[l][k],f[r-(1<

 

 

转载于:https://www.cnblogs.com/lwhinlearning/p/5677186.html

你可能感兴趣的文章
来一发算法
查看>>
[译] 通过一些例子深入了解 JavaScript 的 Async 和 Await
查看>>
Python学习教程_Python学习路线:Python3—数据指纹MD5校验对比
查看>>
瘦身UITableViewController
查看>>
Cookie和Session
查看>>
高性能缓存服务器 nuster v1.8.8.1 发布,支持 HTTP/2,多线程
查看>>
[译] 用 Flutter 开发你的第一个应用程序
查看>>
iOS学习笔记30 系统服务(三)蓝牙
查看>>
(三十二)spring cloud微服务架构b2b2c电子商务-Consul 介绍
查看>>
函数式编程学习之路:Mit-Scheme 在 Ubuntu16.04 下的环境配置
查看>>
浅谈Android 事件分发机制(二)
查看>>
Java 内存模型
查看>>
说说如何使用 vue-router 插件
查看>>
强大的代码保护软件 .NET Reactor使用教程(一):界面各功能说明
查看>>
Java并发编程:synchronized、Lock、ReentrantLock以及ReadWriteLock的那些事儿
查看>>
警告忽略
查看>>
Java Bean + 注册验证
查看>>
以太坊经典升级网络协议确保采矿保持活力
查看>>
PHP以树状形式遍历目录结构
查看>>
我的友情链接
查看>>