您的位置:首页 > 产品设计 > UI/UE

POJ 3368 Frequent values

2015-08-09 11:48 369 查看
B - Frequent values
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ
3368

Appoint description:
bjtu_lyc (2011-08-08)System Crawler (2015-08-08)

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 andj (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 nintegers 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 10
2 3
1 10
5 10
0


Sample Output

1
4
3


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100005
int n,q;
int a[maxn],dp[maxn][30];
int value[maxn],cnt[maxn],l[maxn],r[maxn],has[maxn],ll[maxn],rr[maxn];
void init_rmq(int n){
for(int i=1;i<n;i++)dp[i][0]=cnt[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=0;i+(1<<(j-1))-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=0;
while((1<<(k+1))<=r-l+1)k++;
return max(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
int u,v;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n)&&n){
scanf("%d",&q);
scanf("%d",&a[1]);
int id=1;
memset(value,0,sizeof value);
memset(cnt,0,sizeof cnt);
value[id]=a[1];
cnt[id]++;
has[1]=id;
l[id]=1;
r[id]=1;
for(int i=2;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==a[i-1]){
cnt[id]++;
}
else {
id++;
l[id]=i;
value[id]=a[i];
cnt[id]++;
}
has[i]=id;
r[id]=i;
}
for(int i=1;i<=n;i++){
ll[i]=l[has[i]];
rr[i]=r[has[i]];
}
/*
for(int i=1;i<=n;i++){
cout<<has[i]<<" ";
}
cout<<endl;
*/
init_rmq(id);
for(int i=0;i<q;i++){
scanf("%d%d",&u,&v);
if(has[u]==has[v])printf("%d\n",v-u+1);
else {
int max1=rr[u]-u+1;            //若不设置ll[]的话,则直接用l[has[u]]即可
int max2=v-ll[v]+1;
int max3=0;
if(has[v]-has[u]>1){
max3=rmq(has[u]+1,has[v]-1);
}
printf("%d\n",max(max(max1,max2),max3));
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: