-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSparse_table.cpp
More file actions
executable file
·30 lines (24 loc) · 1.02 KB
/
Copy pathSparse_table.cpp
File metadata and controls
executable file
·30 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPARSE TABLE
// M[i][j] = index of the minimum value in subarray starting at i having length 2^j
void process2(int M[MAXN][LOGMAXN], int A[MAXN], int N)
{
int i, j;
//initialize M for the intervals with length 1
for (i = 0; i < N; i++)
M[i][0] = i;
//compute values from smaller to bigger intervals
for (j = 1; 1 << j <= N; j++)
for (i = 0; i + (1 << j) - 1 < N; i++)
if (A[M[i][j - 1]] < A[M[i + (1 << (j - 1))][j - 1]])
M[i][j] = M[i][j - 1];
else
M[i][j] = M[i + (1 << (j - 1))][j - 1];
}
/*
we have these values preprocessed, let’s show how we can use them to calculate RMQA(i, j).
The idea is to select two blocks that entirely cover the interval [i..j] and
find the minimum between them. Let k = [log(j - i + 1)].
For computing RMQA(i, j) we can use the following formula:
So, the overall complexity of the algorithm is <O(N logN), O(1)
RMQ( i ,j )= min ( M[i][k] , M[ j - 2^k + 1 ][k]) ;
*/