EOJ2458

题目:Eoj2458

 http://acm.cs.ecnu.edu.cn/problem.php?problemid=2458


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

Source

ulm local 2007


题目分析:

给定一个数组,查询给定区间出现频率最高的数出现的频率。构建线段树,每次查询只需要O(logn)的复杂度即可完成,保证了程序一定的效率。构建的线段树节点存储当前区间上出现频率最高的数出现的次数。即转化成区间最值问题,这与普通区间最值的问题的差别主要是两棵子数的断点处的值如果相同,在线段树的向上更新和向下查询时,需要比较左右子树和中间断点处合并后的长度。为了方便比较断点处相同数的区间长度,可以在读入数据时利用一个hash表存储每个数的左右端点,这样在更新和查询的时候就可以快速比较左右子树和因两子树断点处合并后的长度。这里应注意到hash表每次要初始化,在这个问题上wa了很久没找到错,主要是因为我写了构造函数就自以为初始化了,但其实在定义的时候定义的是全局变量,其实并没有每次都初始化。

 

AC代码:

#include <iostream>

#include <cstdio>

#include <string>

#include <cstring>

 

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

 

using namespace std;

const int MAXN=100007;

int line[MAXN<<2];

const int mo=100000;

struct Hash{

   int l,r;

   Hash(){l=1<<30;r=-(1<<30);}

};

 

int num[200005],cnt;

 

void pushup(int rt,int l,int r){

   int m=(l+r)>>1;

   if(num[m]==num[m+1]){

       line[rt]=min(r,h[num[m]+mo].r)-max(l,h[num[m]+mo].l)+1;

    }

   line[rt]=max(line[rt],max(line[rt<<1],line[rt<<1|1]));

}

 

void build(int l,int r,int rt){

   if(l==r){

       line[rt]=1;

       return ;

    }

   int m=(l+r)>>1;

   build(lson);

   build(rson);

   pushup(rt,l,r);

}

 

int query(int a,int b,int l,int r,int rt){

   if(a<=l && r<=b)

       return line[rt];

   int m=(l+r)>>1;

   int ans=0;

   if(a<=m && b>m){

       if(num[m]==num[m+1])

           ans=min(b,h[num[m]+mo].r)-max(a,h[num[m]+mo].l)+1;

    }

   if(a<=m){

       ans=max(ans,query(a,b,lson));

    }

   if(b>m){

       ans=max(ans,query(a,b,rson));

    }

   return ans;

}

 

int main()

{

   int n,m,i;

   while(~scanf("%d",&n),n){

       Hash h[300005];

       scanf("%d",&m);

       for(i=1;i<=n;++i){

           scanf("%d",&num[i]);

           h[num[i]+mo].l=min(h[num[i]+mo].l,i);

           h[num[i]+mo].r=max(h[num[i]+mo].r,i);

       }

       memset(line,0,sizeof(line));

       build(1,n,1);

       int a,b;

       while(m--){

           scanf("%d%d",&a,&b);

           printf("%d\n",query(a,b,1,n,1));

       }

    }

   return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值