[poj3368]Frequent values

Time Limit: 2000MS
Memory Limit: 65536K

分数:2100,小思维题

Description

You are given a sequence of n integers a 1 , a 2 , . . . , a n a_1 , a_2 , ... , a_n a1,a2,...,an in non-decreasing order. In addition to that, you are given several queries consisting of indices i i i and j ( 1 ≤ i ≤ j ≤ n ) j (1 ≤ i ≤ j ≤ n) j(1ijn). For each query, determine the most frequent value among the integers a i , . . . , a j a_i , ... , a_j ai,...,aj.

Input

The input consists of several test cases. Each test case starts with a line containing two integers n n n and q ( 1 ≤ n , q ≤ 100000 ) q (1 ≤ n, q ≤ 100000) q(1n,q100000). The next line contains n n n integers a 1 , . . . , a n ( − 100000 ≤ a i ≤ 100000 a_1 , ... , a_n (-100000 ≤ ai ≤ 100000 a1,...,an(100000ai100000, for each i ∈ 1 , . . . , n ) i ∈ {1, ..., n}) i1,...,n) separated by spaces. You can assume that for each i ∈ 1 , . . . , n − 1 : a i ≤ a i + 1 i ∈ {1, ..., n-1}: a_i ≤ a_{i+1} i1,...,n1:aiai+1. The following q q q lines contain one query each, consisting of two integers i i i and j ( 1 ≤ i ≤ j ≤ n ) j (1 ≤ i ≤ j ≤ n) j(1ijn), 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

题意:
给定一个不下降序列,每次查询一个区间,询问这个区间种出现最多的数字的出现次数是多少。

题解:
先对序列进行预处理,处理出 n u m [ i ] num[i] num[i]表示与 a [ i ] a[i] a[i]相同的 a [ j ] ( j &lt; = i ) a[j](j&lt;=i) a[j](j<=i)有多少个。
然后对于一个区间来说,如果这个区间的开头的 n u m [ i ] num[i] num[i] 1 1 1我们可以直接查询这个区间的 n u m [ i ] num[i] num[i]的最大值作为答案,否则的话我们可以找到这个区间最靠前面的 1 1 1,然后,把这个区间分成从这个 1 1 1开始往后的,和这个 1 1 1之前的,之前的可以直接用 n u m [ i ] num[i] num[i]相减得到,后面的可以用st表查询区间最大值,最后取最大值即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define INF 1999122700
using namespace std;
int mm[100004],a[100004];
int f[100004][24],g[100004][24];
int n,q,num[100004];
void prework(){
    mm[0]=-1;
    for(int i=1;i<=n;i++){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
    }
    for(int j=0;j<=mm[n];j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            if(j==0){
                f[i][j]=num[i];
                g[i][j]=(num[i]==1)?i:INF;
            }
            else{
                f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
                g[i][j]=min(g[i][j-1],g[i+(1<<(j-1))][j-1]);
            }
        }
    }
}
int query(int x,int y){
    int k=mm[y-x+1];
    int l,r=y;
    l=min(g[x][k],g[y-(1<<k)+1][k]);
    if(l==INF)l=y+1;
    int ret=num[l-1]-num[x-1];
    if(l>r)return ret;
    k=mm[r-l+1];
    ret=max(ret,max(f[l][k],f[r-(1<<k)+1][k]));
    return ret;

}
int w33ha(){
    scanf("%d",&q);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        if(a[i]!=a[i-1])num[i]=1;
        else num[i]=num[i-1]+1;
    }
    prework();
    while(q--){
        int l,r;scanf("%d%d",&l,&r);
        printf("%d\n",query(l,r));
    }
    return 0;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        w33ha();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值