POJ 3368 Frequent values (RMQ)

网友投稿 750 2022-08-26

POJ 3368 Frequent values (RMQ)

POJ 3368 Frequent values (RMQ)

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

题意

求区间某个数字出现的最大次数。

思路

题目中有一个很重要的性质,即原序列是不下降的,因此相等的数字一定是挨着的。

以样例来说吧: ​​-1 -1 1 1 1 1 3 10 10 10​​ 。

从左到右扫一遍记录每一个元素左边(包含)连续的相同数字有多少个,则处理后的序列为 ​​1 2 1 2 3 4 1 1 2 3​​ 。

根据这个序列 RMQ 建立 ST 表,我们将其记为 ST

另外我们还需要一个序列 tmp 从右往左扫一遍记录每个数值右端点的位置,即 ​​1 1 5 5 5 5 6 9 9 9​​ 。

对于每一个查询 l,r

假如 al=ar ,说明查询区间都为同一数字,此时区间长度 r−l+1假如 al=al−1 ,说明区间左边截断了某种数字的一部分,此时的结果为 max(tmpl−l+1,max(ST[tmpl+1],ST[r]))对于其他情况,结果为 max(ST[l],ST[r])

AC 代码

#include#includeusing namespace std;const int maxn = 2e5+10;int maxx[30][maxn];int a[maxn];int tmp[maxn];int n,q;void rmq(){ for(int i=1; (1<>n&&n) { cin>>q; for(int i=0; i>a[i]; for(int i=0; i=0; i--) { if(a[ind]!=a[i]) ind = i; tmp[i] = ind; } rmq(); for(int i=0; i>l>>r; --l,--r; if(a[l]==a[r]) ans = r-l+1; else if(l>0&&a[l]==a[l-1]) ans = max(tmp[l]-l+1,query(tmp[l]+2,r+1)); else ans = query(l+1,r+1); cout<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:听说你要做网站?可你知道这些吗(有没有说个网站啊)
下一篇:Codeforces 861 D. Polycarp's phone book (trie)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~