-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparsetable.java
More file actions
39 lines (28 loc) · 1.06 KB
/
Copy pathSparsetable.java
File metadata and controls
39 lines (28 loc) · 1.06 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
31
32
33
34
35
36
37
38
39
import java.util.*;
import java.io.*;
public class Sparsetable {
// in sparse table original array should not be updated
static long[][] st;
static int LOG;
public static void init(int n) {
LOG = (int) (Math.log(n) / Math.log(2));
st = new long[n][LOG + 1];
}
public static void fill(long[] a, int n) {
for (int i = 0; i < n; i++)
st[i][0] = a[i];
for (int j = 1; j <= LOG; j++) {
for (int i = 0; i + (1 << j) - 1 < n; i++) {
st[i][j] = Math.min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
// here we have used overlapping intervals to query minimum in O(1) time
public static long query(int L, int R) {
int length = R - L + 1;
int log = (int) (Math.log(length) / Math.log(2));
long res= Math.min(st[L][log], st[R - (1 << log) + 1][log]);
return res;
}
//in case of sum queries it gives query value in O(log(length of range)) as we have add up ans of the bits of which length is composed of
}