POJ - 3368 - Frequent values(RMQ)

Frequent values
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17279 Accepted: 6245

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

题意:给出n个数,给m组查询,每组查询让求出该区间内(闭区间)出现频率最高的数出现了多少次。

RMQ模板题,因为不需要求出某个数具体是多少,只需要求出现了多少次。输入后预处理出现的次数,就可转化为RMQ求区间最值。

不过要先求出左区间到某个tt位置与之前相等的数,然后再对[tt, ri]求RMQ值


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int M = 1e5 + 10;

int ai[M], num[M];
int rmq[M][30];
int Log[M + 5];

void rmqinit(int n)
{
  Log[0] = Log[1]= 0;
  for (int i = 2; i <= M; i++)
    Log[i] = Log[i >> 1] + 1;

  //int kcnt = log((double)(n + 1)) / log(2.0);

  for (int i = 1; i <= n; i++)
    rmq[i][0] = ai[i];
  for (int j = 1; j <= Log[n]; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      rmq[i][j] = max(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
    }
  }
}

int rmquery(int a, int b)
{
  if (a > b) return 0;
  int t = Log[b - a + 1];
  //int t = log((double)(b-a+1)) / log(2.0);
  return max(rmq[a][t], rmq[b-(1<<t)+1][t]);
}


int main()
{
  int n, m;
  while (cin >> n) {
    if (n == 0) break;
    scanf("%d", &m);
    memset(ai, 0, sizeof(ai));
    memset(num, 0, sizeof(num));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &num[i]);
      if (i == 1) {ai[i] = 1; continue;}
      if (num[i] == num[i - 1]) ai[i] = ai[i - 1] + 1;
      else ai[i] = 1;
    }
    rmqinit(n);
    int le, ri;
    for (int j = 1; j <= m; j++) {
      scanf("%d%d", &le, &ri);
      int tt = le;
      while(num[tt] == num[tt - 1] && tt <= ri)
        tt++;
      int ans = rmquery(tt, ri);
      ans = max(ans, tt - le);
      printf("%d\n", ans);
    }
  }
  return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值