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!
把相同的数字分为一段,用cnt[i]表示第i段的出现次数,num[p],Left[p],Right[p]分别表示位置p所在段的编号和该段左右端点的位置,然后就可转化为对出现次数的RMQ问题,ans = max(max(Right[L] - l + 1 , R - Left[R] + 1) , RMQ(num[L] + 1 , num[R] - 1));
注意判断L与R在同一段的特例:ans = R - L + 1;
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int maxw = 100 + 20;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
int cnt[MAXN] , num[MAXN] , Left[MAXN] , Right[MAXN];
int a[MAXN];
int d[MAXN][20];
int n , num_of_cnt;
int RMQ_init()
{
FORR(i , 1 , num_of_cnt)d[i][0] = cnt[i];
for(int j = 1 ; (1 << j) <= num_of_cnt ; j++)
for(int i = 1 ; i + j - 1 <= num_of_cnt ; i++)
{
d[i][j] = max(d[i][j - 1] , d[i + (1 << (j - 1))][j - 1]);
}
}
int RMQ(int L , int R)
{
int k = 0;
while((1 << (k + 1)) <= R - L + 1)k++;
return max(d[L][k] , d[R - (1 <<k) + 1][k]);
}
//int RMQ(int i, int j)
//{
// int k = log(j-i+1) / log(2);
//
// return max(d[i][k] , d[j-(1 << k)+1][k]);
//}
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
int n , q , x , y;
while(scanf("%d",&n)&&n)
{
scanf("%d",&q);
num_of_cnt = 0;
clr(cnt , 0),clr(Left , 0) ,clr(Right , 0) , clr(num , 0) , clr(d , 0) , clr(a , 0);
FORR(i , 1 , n)
{
scanf("%d",&a[i]);
if(i == 0)
{
cnt[num_of_cnt] = 1;
num[i] = num_of_cnt;
Left[i] = i;
}
else if(a[i] == a[i - 1])
{
cnt[num_of_cnt]++;
num[i] = num_of_cnt;
Left[i] = Left[i - 1];
}
else
{
num_of_cnt++;
num[i] = num_of_cnt;
Left[i] = i;
cnt[num_of_cnt] = 1;
}
}
Right[n] = n;
REPP(i , n - 1, 1)
{
if(a[i] == a[i + 1])
{
Right[i] = Right[i + 1];
}
else Right[i] = i;
}
// FOR(i , 1 , n)
// {
// printf("%d %d %d\n",num[i] , Left[i] , Right[i]);
// }
// FORR(i , 1 , num_of_cnt)printf("%d\n" , cnt[i]);
RMQ_init();
// FORR(i , 1 , num_of_cnt)FORR(j , 1 , num_of_cnt)
// {
// printf(" %d\n" , d[i][j]);
// }
while(q--)
{
scanf("%d%d",&x ,&y);
int ans = 0;
if(a[x] == a[y])ans = y - x + 1;
else if(num[y] - num[x] == 1)ans = max(Right[x] - x + 1 , y - Left[y] + 1);
else if(num[y] - num[x] == 2)ans = max(max(Right[x] - x + 1 , y - Left[y] + 1) , cnt[num[y] - 1]);
else
{
ans = max(max(Right[x] - x + 1 , y - Left[y] + 1) , RMQ(num[x] + 1 , num[y] - 1));
// cout << Right[x] - x + 1 << ' ' << y - Left[y] + 1 << ' ' << RMQ(num[x] + 1 , num[y] - 1) << endl;
}
printf("%d\n" , ans);
}
}
return 0;
}