UVa11235 FrequentValues(RMQ)

Problem F: Frequent values

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 Specification

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 Specification

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

题目大意:
给一个非降序排列的整数数组a,你的任务是对于一系列询问(i, j),回答ai,ai+1...aj中次数出现最多的值所出现的次数。

分析:
由于数列是非降序的,所以所有相等的数都会聚集在一起。这样我们就可以把整个数组进行编码。如-1,1,1,2,2,2,4就可以编码成(-1,1),(1,2),(2,3),(4,1)表示(a,b)数组中的a连续出现了b次。用num[i]表示原数组下表是i的数在编码后的第num[i]段。left[i],right[i]表示第i段的左边界和右边界,用coun[i]表示第i段有conu[i]个相同的数。这样的话每次查询(L, R)就只要计算(right[L]-L+1),(R-left[R]+1)和RMQ(num[L]+1, num[R]-1)这三个值的最大值就可以了。
其中,RMQ是对coun数组进行取件查询的结果。
特殊的,如果L和R在同一个区间内的话,那么结果就是(R-L+1)

详见代码:
 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define inf -0x3f3f3f3f
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 
25 template<class T> T CMP_MIN(T a, T b) { return a < b; }
26 template<class T> T CMP_MAX(T a, T b) { return a > b; }
27 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
28 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
31 
32 //typedef __int64 LL;
33 typedef long long LL;
34 const int MAXN = 100005;
35 const int MAXM = 100005;
36 const double eps = 1e-12;
37 
38 int num[MAXN], coun[MAXN], Left[MAXN], Right[MAXN];
39 int n, q, a, last, tot;
40 int DP[MAXN][20];
41 
42 void init_RMQ()
43 {
44     mem0(DP);
45     for(int i=1;i<=tot;i++) DP[i][0] = coun[i];
46     for(int j=1;(1<<j)<=n;j++)
47     {
48         for(int i=1;i+(1<<j)<=tot;i++)
49         {
50             DP[i][j] = max(DP[i][j-1], DP[i+(1<<(j-1))][j-1]);
51         }
52     }
53 }
54 
55 int RMQ(int L, int R)
56 {
57     if(L > R) return 0;
58     int k = 0;
59     while((1<<(1+k)) <= R-L+1) k++;
60     return max(DP[L][k], DP[R-(1<<k)+1][k]);
61 }
62 
63 int main()
64 {
65 //    FOPENIN("in.txt");
66 //    FOPENOUT("out.txt");
67     while(~scanf("%d", &n) && n)
68     {
69         scanf("%d", &q);
70         tot = 0; mem0(Left); mem0(Right); mem0(coun);
71         for(int i=1;i<=n;i++)
72         {
73             scanf("%d", &a);
74             if(i==1) { ++tot;   last=a;  Left[tot] = 1; }
75             if(last == a) { num[i]=tot; coun[tot]++; Right[tot]++; }
76             else { num[i]=++tot; coun[tot]++; Left[tot]=Right[tot]=i; last=a; }
77         }
78         init_RMQ();
79         int l, r;
80         for(int i=0;i<q;i++)
81         {
82             scanf("%d%d", &l, &r);
83             if(num[l] == num[r]) { printf("%d\n", r-l+1);  continue; }
84             printf("%d\n", max( RMQ(num[l]+1, num[r]-1), max( Right[num[l]]-l+1, r-Left[num[r]]+1 ) ) );
85         }
86     }
87     return 0;
88 }

 

posted @ 2014-03-06 15:43  再见~雨泉  阅读(1434)  评论(0编辑  收藏  举报