RMQ (Range minimum/maximum Query) The question is: For series A of length n, answer a number of questions RMQ (A,I,J) (i,j<=n), and return to column A and subscript in I, The smallest (large) value in J, that is, the RMQ problem is the problem of finding the maximum interval.
Balanced Lineup
Time Limit: 5000MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status
Description
For the daily milking, Farmer John's n cows (1≤ n ≤50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate frisbee with some of the cows. To keep things simple, he'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has fun they should not differ too much in height.
Farmer John has made a list of Q (1≤ q ≤200,000) Potential groups of cows and their heights (1≤ H Eight ≤1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the G Roup.
Input
Line 1:two space-separated integers,
Nand
Q.
Lines 2..
N+1:line
I+1 contains a single integer which is the height of cow
I
Lines
N+2..
N+
Q+1:two integers
Aand
B(1≤
A≤
B≤
N), representing the range of cows from
ATo
BInclusive.
Output
Lines 1..
Q: Each line contains a single integer so is a response to a reply and indicates the difference in height between the Tal Lest and shortest cow in the range.
Sample Input
6 31734251 54) 62 2
Sample Output
630
1. Simplicity (traversal): Complexity O (n)-O (QN).
2. Segment Tree: Complexity O (n)-O (Qlogn).
3.ST (Sparse Table) algorithm: O (NLOGN)-O (q)
The ST algorithm, because each query only O (1), when processing a large number of queries there is an advantage.
<1>. preprocessing (Dynamic programming DP)
For A[i] series, F[i][j] indicates the maximum value (DP state) from the number of consecutive 2^j in the sequence of I, and you can see that f[i][0] represents A[i] (the initial value of DP). Finally, the state transition equation is
F[i][j]=max (f[i][j-1],f[i+2^ (j-1)][j-1])
<2> Enquiry
If the query interval is (a, b), the interval length is b-a+1, and k=log2 (b-a+1) is taken, then Max (A, b) =max (F[a][k],f[b-2^k+1][k]).
1.ST algorithm
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Cmath> #define MAX (A, B) (a>b?a:b) #define MIN (A, b) (A<B?A:B) using namespace std;const int maxn = 50050;int mins[m Axn][20];int maxs[maxn][20];void RMQ (int n) {for (int j = 1; (1 << j) <= n;j++) for (int i = 1; i + (1 << J)-1 <= N; i++) {int p = (1 << (j-1)); Mins[i][j] = Min (mins[i][j-1], mins[i + p][j-1]); Maxs[i][j] = max (maxs[i][j-1], maxs[i + p][j-1]);}} int querymin (int l, int r) {int k = log (double) (r-l + 1))/log (2.0); return min (Mins[l][k], Mins[r-(1 << k) + 1][k ]);} int Querymax (int l, int r) {int k = log (double) (r-l + 1))/log (2.0); return Max (Maxs[l][k], Maxs[r-(1 << k) + 1][k ]);} int main () {int n, q;scanf ("%d%d", &n, &q), int num;for (int i = 1; I <= n; i++) {scanf ("%d", &num); Maxs[i][0] = mins[i][0] = num;} RMQ (n); int A, b;int ans;for (int i = 0; i < Q; i++) {scanf ("%d%d", &a, &b); ans= Querymax (A, b)-Querymin (A, B);p rintf ("%d\n", ans);}
2. Segment Tree
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < Algorithm> #define MAX (A, B) (a>b?a:b) #define MIN (A, b) (A<B?A:B) using namespace std;const int maxn = 50050;int num [Maxn];struct node{int r;int l;int max;int Min;} Tree[3*maxn];void Build (int l, int r, int i) {tree[i].l = l; tree[i].r = r;if (L = = r) {Tree[i]. Max = Tree[i]. Min = Num[l];return;} int m = (L + r) >> 1, ls = i << 1, rs = ls + 1;build (l, M, LS); build (M + 1, R, RS); Tree[i]. max = max (Tree[rs]. Max, Tree[ls]. MAX); Tree[i]. min = min (Tree[rs]. Min, Tree[ls]. Min);} int Querymax (int l, int r, int i) {if (tree[i].l = = L&&TREE[I].R = = r) return Tree[i]. Max;int m = (tree[i].l + tree[i].r) >> 1, ls = i << 1, rs = ls + 1;if (r <= m) return Querymax (L, R, LS); El Se if (L > m) return Querymax (L, R, RS), else return Max (Querymax (L, M, ls), Querymax (M + 1, R, RS));} int querymin (int l, int r, int i) {if (tree[i].l = = L&&TREE[I].R = = r) return Tree[i]. MIn;int m = (tree[i].l + tree[i].r) >> 1, ls = i << 1, rs = ls + 1;if (r <= m) return Querymin (L, R, LS); Els E if (L > m) return querymin (L, R, RS), else return min (Querymin (L, M, ls), querymin (M + 1, R, RS));} int main () {int n, q;scanf ("%d%d", &n, &q), for (int i = 1; I <= n; i++) scanf ("%d", &num[i]); build (1, N, 1); int A, b;int ans;for (int i = 0; i < Q; i++) {scanf ("%d%d", &a, &b); ans = Querymax (A, B, 1)-Querymin (A, B, 1); printf ("%d\n", ans);}}
Reference to the http://blog.csdn.net/niushuai666/article/details/6624672/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ-3264 Balanced Lineup (RMQ problem seeking interval maximum)