POJ3368---Frequent values(分组处理+RMQ)

Language:

Frequent values

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 14377 Accepted: 5244

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

根据题目的意思,相同的数字都是放在一起的,所以我们把相同的数字看成一组,每次查询都找出所在的组,然后去掉零头单独处理,中间完整部分用rmq来查,复杂度O(n?logn)

/*************************************************************************
  > File Name: POJ3368.cpp
  > Author: ALex
  > Mail: [email protected]
  > Created Time: 2015年04月23日 星期四 21时37分21秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100010;
int num[N];
int arr[N];
int arrarr[N];
int mp[N], mp2[N], L[N], zu[N];
int dp[N][20];
int LOG[N];

void initRMQ(int n) {
    for (int i = 1; i <= n; ++i) {
        dp[i][0] = num[i];
    }
    for (int j = 1; j <= LOG[n]; ++j) {
        for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
            dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
        }
    }
}

int ST(int a, int b) {
    int k = LOG[b - a + 1];
    return max(dp[a][k], dp[b - (1 << k) + 1][k]);
}
int main() {
    LOG[0] = -1;
    for (int i = 1; i <= 100000; ++i) {
        LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1;
    }
    int n, q;
    while (~scanf("%d", &n), n) {
        scanf("%d", &q);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &arrarr[i]);
        }
        int cnt = 1;
        arr[1] = 1;
        for (int i = 1; i <= n; ++i) {
            if (arrarr[i] == arrarr[i - 1]) {
                arr[i] = arr[i - 1];
            }
            else {
                arr[i] = ++cnt;
            }
        }
        memset(num, 0, sizeof(num));
        mp[arr[1]] = -inf;
        L[arr[1]] = 1;
        zu[arr[1]] = 1;
        num[1] = 1;
        cnt = 1;
        for (int i = 2; i <= n; ++i) {
            if(arr[i] == arr[i - 1]) {
                ++num[cnt];
            }
            else {
                ++cnt;
                zu[arr[i]] = i;
                L[arr[i]] = cnt;
                mp[arr[i]] = arr[i - 1];
                mp2[arr[i - 1]] = arr[i];
                num[cnt] = 1; //第i组数目
            }
        }
        mp2[arr[n]] = -inf;
        int l, r;
        initRMQ(cnt);
        while (q--) {
            scanf("%d%d", &l, &r);
            if (arr[l] == arr[r]) {
                printf("%d\n", r - l + 1);
                continue;
            }
            int nxt = mp2[arr[l]];
            int pos1 = zu[nxt];
            int cnt1 = pos1 - l;
            int last = mp[arr[r]];
            int pos2 = zu[arr[r]];
            int cnt2 = r - pos2 + 1;
            pos1 = L[nxt];
            pos2 = L[last];
//          printf("%d %d %d %d\n", cnt1, cnt2, pos1, pos2);
            if (pos1 > pos2) {
                printf("%d\n", max(cnt1, cnt2));
            }
            else {
                printf("%d\n", max(cnt1, max(cnt2, ST(pos1, pos2))));
            }
        }
    }
    return 0;
}
时间: 2024-11-09 00:03:08

POJ3368---Frequent values(分组处理+RMQ)的相关文章

POJ3368 Frequent values 线段树

N个数为非递减顺序,给定范围l,r,求[l,r]区间内数字出现频率最高的次数. 可以用线段树来做.先说查询,我们设节点P对应的区间为[a, b],左孩子节点为p1,右孩子节点为p2,那么 P也许不等于 max(p1 , p2),原因是如果p1中频率较低的某个数与p2中出现频率较低的某个数是同一个数,并且两者出现次数加起来大于max(p1, p2),但是,题目说N个数为非递减顺序排列,所以这个可能只会发生在p1的右端和p2的左端对应的数是同一个数,因此,我们还要处理这第三个可能. 查询的问题解决,

POJ3368 Frequent values(RMQ线段树)

题目大概说给一个递增序列,询问区间出现最多的数. 用莫队算法比较直观,虽然应该会T..好像也可以主席树..不过题目给的序列是有序的,因而相同的数会聚在一起. 考虑把序列分成一段一段,使每段都包含极大的相同的数字 这样对于每一个区间查询: 可能这个区间左边或右边没有包含完整的一段,而其长度在段里对左或右端点进行二分查找就知道了 而除去两边不完整的,还要求出中间若干完整段的最大长度,这个就是用RMQ来快速解决了 于是这题就能这样解决了. 1 #include<cstdio> 2 #include&

[poj3368]Frequent values(rmq)

题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cmath> 6 #include<iostream>

UVA 11235 Frequent values 线段树/RMQ

vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释************************************************************ 题目大意:给出一个非降序排列的整数数组 a1,a2,a3,...,an,你的任务是对于一系列询问 (i, j),回答 ai,ai+1,...,aj 中出现次数最多的值所出现的次数. 输入格式:包含多组数据.每组

poj3368 Frequent values

思路: 转化为RMQ. 实现: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int MAXN = 100005; 6 const int INF = 0x3f3f3f3f; 7 8 int a[MAXN], tree[MAXN * 4], n, m, sum[MAXN]; 9 10 void build(int num, in

uva 11235 - Frequent values(RMQ)

题目链接:uva 11235 - Frequent values 题目大意:给定一个非降序的整数数组,要求计算对于一些询问(i,j),回答ai,ai+1,-,aj中出现最多的数出现的次数. 解题思路:因为序列为非降序的,所以相同的数字肯定是靠在一起的,所以用o(n)的方法处理处每段相同数字的区间.然后对于每次询问: num[i]=num[j]:j?i+1 numi≠numj:max(RMQ(righti+1,reftj?1),max(righti?i+1,j?leftj+1)) #include

POJ 3368 Frequent values RMQ ST算法/线段树

                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms 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 f

POJ 3368 Frequent values (基础RMQ)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 Description You are given a sequence of n integersa1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consi