poj 3368 Frequent values

来源:互联网 发布:阿里云邮箱pc客户端 编辑:程序博客网 时间:2024/05/17 04:22

题目链接:

点击打开链接


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 102 31 105 100

Sample Output

143



题目大意: 给出非下降的序列,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
原创粉丝点击