-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSparse Table RMQ.cpp
More file actions
40 lines (36 loc) · 799 Bytes
/
Sparse Table RMQ.cpp
File metadata and controls
40 lines (36 loc) · 799 Bytes
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
31
32
33
34
35
36
37
38
39
40
/**
Name : Sparse table(RMQ)
Description : Find min/max
Time Complexity : Build O(nlogn) Query O(1)
*/
#include <bits/stdc++.h>
using namespace std;
//0 Indexed
#define MX 10000
int spt[MX][22];
int n,ar[MX]={ 7, 2, 3, 0, 5, 10, 3, 12, 18 };
void buildST()
{
for (int i = 0; i < n; i++) spt[i][0] = ar[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
spt[i][j] = min(spt[i + (1 << (j - 1))][j - 1] , spt[i][j - 1]);
}
}
}
int query(int l, int r)
{
if(l>r) return INT_MAX;
int j = (int)log2(r - l + 1);
///j = 31 - __builtin_clz(r - l+1);
return min (spt[l][j], spt[r - (1 << j) + 1][j]);
}
// Driver program
int main()
{
n = 9;
buildST();
cout << query(4, 7) << endl;
cout << query(7, 8) << endl;
return 0;
}