码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3368 Frequent values

时间:2015-08-09 12:33:49      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:poj 3368   frequent values   rmq+离散化   

B - Frequent values
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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));
            }
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3368 Frequent values

标签:poj 3368   frequent values   rmq+离散化   

原文地址:http://blog.csdn.net/u013497977/article/details/47374663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!