poj 3368 Frequent values

题目链接:

点击打开链接


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.

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



题目大意: 给出非下降的序列,q次询问某个区间内数最多的值是多少数(比如在区间1-10内最多的是-1,有4个,则输出4)

思路: 离散化+RMQ,求离散后的最大值在对应到相应的区间上

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

int M[100000];///存线段树的节点
int A[100000];///存每个值的长度(相当于离散化)
int pre[100000];///存输入区间值所对应的离散后的区间
int l[100000];///每个值的左边界
int r[100000];///右边界
int a[100000];
int n,q;
int len;///长度
///建线段树
void build_RMQ(int node,int b,int e,int M[],int A[])
{
    if(b==e)
    {
        M[node]=A[b];
    }
    else
    {
        build_RMQ(2*node,b,(b+e)/2,M,A);
        build_RMQ(2*node+1,(b+e)/2+1,e,M,A);
        if(M[2*node]>=M[2*node+1])M[node]=M[2*node];
        else M[node]=M[2*node+1];
    }
}
///查询线段树区间取最大值
int query(int node,int b,int e,int M[],int A[],int i,int j)
{
    int p1,p2;
    if(i>e||j<b)return -1;
    if(b>=i&&e<=j)return M[node];
    p1=query(2*node,b,(b+e)/2,M,A,i,j);
    p2=query(2*node+1,(b+e)/2+1,e,M,A,i,j);
    if(p1==-1)return p2;
    if(p2==-1)return p1;
    if(p1<=p2)return p2;
    return  p1;
}
int main()
{
    while(scanf("%d",&n))
    {
        memset(M,0,sizeof(0));
        if(!n)break;
        scanf("%d",&q);
        scanf("%d",&a[1]);
        memset(A,0,sizeof(A));
        len=1;
        A[len]=1;
        l[len]=1;
        pre[1]=len;
        for(int i=2; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==a[i-1])
            {
                A[len]++;
                pre[i]=len;
            }
            else
            {
                r[len]=i-1;
                len++;
                l[len]=i;
                pre[i]=len;
                A[len]=1;
            }
            //cout<<"**"<<endl;
        }
        //cout<<"#"<<A[1]<<" "<<A[2]<<endl;
        //r[len]=n;
       // cout<<len<<endl;
       /* for(int i=1;i<=len;i++)
        {
            cout<<i<<" "<<A[i]<<endl;
        }*/
        build_RMQ(1,1,len,M,A);
        int u,v;
        while(q--)
        {
            scanf("%d%d",&u,&v);
            int c=pre[u];
            int d=pre[v];
            int b=pre[u]+1;///把输入的区间去掉(因为不知道输入的
            ///是否包含了该值的所有长度)
            int e=pre[v]-1;
            //<<b<<" "<<e<<endl;
            if(c==d)
            {
                printf("%d\n",v-u+1);
            }
            ///特殊情况处理
            else if(c+1==d)
            {
                printf("%d\n",max(r[c]-u+1,v-l[d]+1));
            }
            else
            {
                int mx=query(1,1,len,M,A,b,e);
                //cout<<"mx:"<<mx<<endl;
                //cout<<"r[]:"<<r[pre[u]]<<endl;
                //cout<<"l[]:"<<l[pre[v]]<<endl;
                int k=r[pre[u]]-u+1;
                int h=v-l[pre[v]]+1;
                ///在比较中间的最大长度的值和残余的两个边界的长度
                printf("%d\n",max(mx,max(k,h)));
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值