POJ 3368 Frequent values(RMQ),pojrmq
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
也許看完題你可能想到了線段樹做法,但是這道題的確可以用RMQ來寫。
具體就是查詢區間中重複最多的次數,但是RMQ只能查詢區間最大值,我們
初始化的時候也只能初始化連續的那一部分,如果區間中不連續了了。
所以我們要在查詢上下功夫。從查詢左端點起,找到不連續的點pos,pos
以後的就可以RMQ查詢,具體答案就是max(pos-l,RMQ(POS,l))
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=1e5+100;int num[maxn],sum[maxn];int n,q;int dp[maxn][20];void init(){ REPF(i,1,n) dp[i][0]=sum[i]; for(int j=1;(1<<j)<=n;j++) for(int i=1;i+(1<<j)-1<=n;i++) dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);}int RMQ(int l,int r){ int k=(int)(log(r-l+1)/log(2.0)); return max(dp[l][k],dp[r-(1<<k)+1][k]);}void work(){ int x,y,ans; while(q--) { scanf("%d%d",&x,&y); int pos=x+1; while(num[pos]==num[pos-1]&&pos<=y) pos++; if(pos>y) ans=pos-x; else ans=max(pos-x,RMQ(pos,y)); printf("%d\n",ans); }}int main(){ int x; while(~scanf("%d",&n)&&n) { scanf("%d",&q); CLEAR(sum,0);num[0]=INF; REPF(i,1,n) { scanf("%d",&num[i]); if(num[i]==num[i-1]) sum[i]=sum[i-1]+1; else sum[i]=1; } init(); work(); } return 0;}