原题链接
Problem 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.
Note: A naive algorithm may not run in time!
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
题目大意
给出一个非降序数列,对于每个询问(i,j),回答 ai , ai+1 , ai+2 ··· aj 中出现次数最多的数字的个数。
解题思路
使用Sparse-Table算法,d[i][j]表示从第i位开始,长度为 2j 的区间中的最大值,编码时要考虑边界情况。
AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
using namespace std;
int a[100005];
int num[100005];
int lefta[100005];
int righta[100005];
int d[100005][32];
vector<int> counta;
int main(void)
{
int n,q;
while(cin>>n&&n)
{
cl(num);
cl(lefta);
cl(righta);
counta.clear();
cin>>q;
int pos=0;
int st=0;
int ed=n-1;
int jishu=1;
scanf("%d",&a[0]);
num[0]=0;
lefta[0]=0;
if(n==1) counta.pb(1);
rep(i,1,n)
{
scanf("%d",&a[i]);
if(a[i]==a[i-1])
{
num[i]=pos;
lefta[i]=st;
jishu++;
}
else
{
counta.pb(jishu);
jishu=1;
num[i]=++pos;
st=i;
lefta[i]=st;
}
}
if(jishu>=2) counta.pb(jishu);
righta[n-1]=n-1;
ed=n-1;
for(int i=n-2;i>=0;--i)
{
if(a[i]==a[i+1])
{
righta[i]=ed;
}
else
{
ed=i;
righta[i]=ed;
}
}
int ac=counta.size();
rep(i,0,ac) d[i][0]=counta[i];
for(int j=1;(1<<j)<=ac;++j)
{
for(int i=0;i+(1<<j)-1<ac;++i)
{
d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
}
rep(i,0,q)
{
scanf("%d%d",&st,&ed);
st--;
ed--;
int L,R,res;
L=num[st]+1;
R=num[ed]-1;
if(L-2==R) printf("%d\n",ed-st+1);
else if(L-1==R) printf("%d\n",max(righta[st]-st+1,ed-lefta[ed]+1));
else
{
res=-inf;
int k=0;
int ans;
while(1<<(k+1)<=R-L+1) k++;
ans=max(d[L][k],d[R-(1<<k)+1][k]);
res=max(res,ans);
res=max(res,righta[st]-st+1);
res=max(res,ed-lefta[ed]+1);
printf("%d\n",res);
}
}
}
return 0;
}