RMQ uva11235



Description

Download as PDF
2007/2008 ACM International Collegiate Programming Contest 
University of Ulm Local Contest

Problem F: Frequent values

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 Specification

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 Specification

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 10
2 3
1 10
5 10
0
Sample Output
1
4
3

A naive algorithm may not run in time!


RMQ经典题,游程编码之后进行RMQ查询区间最值,loc[i]表示位置i是第几段,cot[i]表示第i段的数的个数,L[i]表示i所在段的最左边,R[i]表示i所在段的最右边,查询时分两种情况:

(1) l,r区间里的数在同一段,答案为r-l+1.

(2)不在同一段 答案为R[l]-l+1,r-L[r]+1,RMQ(loc[R[l]+1],loc[L[r]-1])的最大值

代码如下:

/*************************************************************************
    > File Name: RMQ.cpp
    > Author: acvcla
    > QQ: 
    > Mail: acvcla@gmail.com 
    > Created Time: 2014年10月06日 星期一 13时29分38秒
 ************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int dp[maxn][20],A[maxn],L[maxn],R[maxn],loc[maxn],cot[maxn];
void Init(int n){
	for(int i=1;i<=n;i++)dp[i][0]=cot[i];
	for(int j=1;(1<<j)<=n;j++)
	for(int i=1;i+(1<<j)<=n;i++)
	dp[i][j]=max(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
}
int RMQ(int l,int r)
{
	if(l>r)return 0;
	int k=0;
	while(1<<(k+1)<=r-l+1)k++;
	return max(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(0);
		int n,m;
		while(cin>>n){
			if(!n)return 0;
			cin>>m;
			A[0]=999999999;
			int cnt=0,l=1;
			rep(i,1,n){
				cin>>A[i];
				if(A[i]!=A[i-1]){
					R[i]=i;
					++cnt;
					cot[cnt]=1;
					int k=i-1;
					while(k>=l){
						R[k--]=i-1;
					}
					L[i]=l=i;
				}else{
					cot[cnt]++;
					L[i]=l;
					R[i]=i;
				}
				loc[i]=cnt;
			}
			int k=n-1;
			while(k>=l){
				R[k--]=n;
			}
			int r;
			Init(cnt);
			while(m--){
				cin>>l>>r;
				int ans=0;
				if(loc[l]!=loc[r]){
					ans=max(R[l]-l+1,r-L[r]+1);
					ans=max(ans,RMQ(loc[R[l]+1],loc[L[r]-1]));
				}else ans=r-l+1;
				cout<<ans<<endl;
			}
		}
		return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值