POJ 3368 (RMQ)

来源:互联网 发布:java split多个分隔符 编辑:程序博客网 时间:2024/06/05 19:37

Frequent values
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15924 Accepted: 5794

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

Source

Ulm Local 2007



题意:有n个数字,m次查询,每次询问区间[L,R]中出现最多次数的数字,输出最多次数。



题解:先说思路,我们每次查询[L,R]区间,左区间L有可能不是某个数字X的开始位置,那么我们可能会多算了一些长度,那么我们就要设法把这不完整的一段删去,那么我们可以这么处理,把X的[最后出现的下标-当前下标],这段长度就是X的长度,去掉了数字X的干扰,使得查找区间变为[最后出现X的下标+1,y ]。这里需要考虑一个特殊情况,就是如果区间[L,R]是属于同一个数字,那么就是输出(R-L+1)。详情见下图:




先使用结构体数组记录记录每个数字是第几次出现和数字本身。然后记录每个数字最后出现的下标(建议使用map是最好的),




#include<cstdio>  #include<cstring>  #include<cstdlib>  #include<cmath>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<set>  #include<queue>  #include<string>  #include<bitset>  #include<utility>  #include<functional>  #include<iomanip>  #include<sstream>  #include<ctime>  using namespace std;#define N int(1e5+10)  #define inf int(0x3f3f3f3f)  #define mod int(1e9+7)  typedef long long LL;#ifdef CDZSC  #define debug(...) fprintf(stderr, __VA_ARGS__)  #else  #define debug(...)   #endif  int dmax[N][32];void RMQ(vector< pair<int, int> >&A){int n = A.size();for (int i = 0; i<n; i++)dmax[i][0] = A[i].first;for (int j = 1; (1 << j) <= n; j++){for (int i = 0; i + (1 << j) - 1<n; i++){dmax[i][j] = max(dmax[i][j - 1], dmax[i + (1 << (j - 1))][j - 1]);}}}int query_max(int L, int R){int k = 0;while ((1 << (k + 1)) <= R - L + 1)k++;return max(dmax[L][k], dmax[R - (1 << k) + 1][k]);}vector<pair<int, int> >v;int a[N];map<int, int>w;int main(){#ifdef CDZSC  freopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);  int _time_jc = clock();#endif  int n, m, x, y;while (~scanf("%d", &n)){w.clear();v.clear();if (n == 0)break;scanf("%d", &m);for (int i = 0; i<n; i++)scanf("%d", &a[i]);int last = a[0];w[a[0]] = 0;v.push_back(make_pair(1, a[0]));for (int i = 1; i<n; i++){if (last == a[i]){v.push_back(make_pair(v[i - 1].first + 1, a[i]));}else   {v.push_back(make_pair(1, a[i]));last = a[i];}w[a[i]] = i;}RMQ(v);while (m--){int ans;scanf("%d%d", &x, &y);y--; x--;if (v[x].second == v[y].second){printf("%d\n",y-x+1);continue;}ans = w[v[x].second] - x+1;x = w[v[x].second] + 1;printf("%d\n", max(ans, query_max(x, y)));}}#ifdef CDZSC  debug("time: %d\n", int(clock() - _time_jc));#endif  return 0;}








0 0