Approximate Evaluation of Range Nearest Neighbor Queries with
Range Minimum Queries
-
Upload
benjamin-sach -
Category
Education
-
view
29 -
download
0
Transcript of Range Minimum Queries
Advanced Algorithms – COMS31900
Range Minimum Queries
Benjamin Sach
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n17 823 73 51 82 19 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
17 823 73 51 82 19 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
19 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
i = 5 j = 11
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]
RMQ(5, 11) = 8
e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]
9 21 545
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
i = 5 j = 11
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]
RMQ(5, 11) = 8
e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]
• We will discuss several algorithms which give trade-offs between
space used, prep. time and query time
9 21 545
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query
i = 5 j = 11
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]
RMQ(5, 11) = 8
e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]
• We will discuss several algorithms which give trade-offs between
space used, prep. time and query time
• Ideally we would like O(n) space, O(n) prep. time and O(1) query time
9 21 545
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
19
Block decomposition
smallest from each pair
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
19
Block decomposition
smallest from each pair
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
19
Block decomposition
smallest from each pair
19A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
smallest from each four
1919
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
smallest from each four
1919
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
smallest from each eight
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
smallest from each eight
8 19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
n2
n4
n8
n16
+
+
+
+
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
n2
n4
n8
n16
+
+
+
+
O(n)
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
How quickly can we build them?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
How quickly can we build them?
construct the Ak
arrays bottom-up
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
How quickly can we build them?
compute this from
these in O(1) time
construct the Ak
arrays bottom-up
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
How quickly can we build them? O(n) preprocessing time
compute this from
these in O(1) time
construct the Ak
arrays bottom-up
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
Ak is an array of length nk so that for all i: Ak[i] = (x, v)
where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.
We store Ak for all k = 1, 2, 4, 8 . . . 6 n
How much space is this? O(n) in total
How quickly can we build them? O(n) preprocessing time
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
RMQ(1,9)
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
RMQ(1,9)
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
RMQ(1,9)
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
RMQ(1,9)
Repeat:
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
RMQ(1,9)
Repeat:
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
RMQ(1,9)
Repeat:
(break ties arbitrarily)
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
RMQ(1,9)
Repeat:
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
RMQ(1,9)
Repeat:
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
How many blocks do we pick?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
never threein a row
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
19
never threein a row
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
never threein a row
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
never two onone side
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
never two onone side
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
no gaps
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
no gaps
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
10,000 foot view
no valleys
no plateaus
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
There are O(logn) sizes
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
There are O(logn) sizes
Picking the blocks from
Ak takes O(1) time
19
Block decomposition
A
n
17 823 73 51 82 32 5 67 91 14 46 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
21 4 6 8 11 13 14
17 8 51 19 5 14 9 21
2 6 8 13
8 19 5 9
2 8
8 5
8
5
A2
A4
A8
A16
How do we find RMQ(i,j)?
Find the largest block which
is completely contained within the query interval
but doesn’t overlap a block you chose before
The minimum is the smallest in all these blocksbecause they cover the query
RMQ(1,9)
Repeat:
How many blocks do we pick?
at most 2 blocks of each size
There are O(logn) sizes
Picking the blocks from
Ak takes O(1) time
So we have . . . O(n) space,
O(n) prep time
O(logn) query time
19
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
2
The array R2 stores RMQ(i, i+ 1) for all i
A
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
2stored in R2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
R4 stores RMQ(i, i+ 3) for all i
2stored in R2
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
R4 stores RMQ(i, i+ 3) for all i
2stored in R2
4stored in R4
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
2stored in R2
4stored in R4
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
2stored in R2
4stored in R4
8stored in R8
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
2stored in R2
4stored in R4
8stored in R8
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
2stored in R2
4stored in R4
8stored in R8
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
2stored in R2
4stored in R4
8stored in R8
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
We build R2 from A in O(n) time
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
2k
k k
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
2k
k k
the min in here
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
2k
k k
the min in here
is the min of these two mins(which we have in Rk )
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
More space, faster queries
Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .
The array R2 stores RMQ(i, i+ 1) for all i
A
We build Rk for k = 2, 4, 8, 16 . . . 6 n
R4 stores RMQ(i, i+ 3) for all i
R8 stores RMQ(i, i+ 7) for all i
Rk stores RMQ(i, i+ k − 1) for all i
each of the O(logn) arrays uses O(n) space
so O(n logn) total space
2stored in R2
4stored in R4
8stored in R8
We build R2 from A in O(n) time
We build R2k from Rk in O(n) time
how?
This takes O(n logn) prep time
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
8stored in R8
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
8stored in R8
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2k
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2k
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
More space, faster queries
A
2stored in R2
4stored in R4
8stored in R8
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
More space, faster queries
A
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
More space, faster queries
A
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
i j`
More space, faster queries
A
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
i j`
kk
More space, faster queries
A
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
i j`
kk
these overlap
because 2k > `
More space, faster queries
A
we build Rk for k = 2, 4, 8, 16 . . . 6 n
Rk stores RMQ(i, i+ k − 1) for all i,
How do we compute RMQ(i, j)?
If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer
these queries take O(1) time
Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)
(these two queries take O(1) time)
This takes O(1) time but why does it work?
i j`
kk
these overlap
because 2k > `
the min is either in
here or in here
Range minimum query (intermediate) summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query (intermediate) summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query (intermediate) summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Can we do better?Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query (intermediate) summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Can we do better?Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(yes)
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
n
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
n
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
n
n = nlogn
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
log n
n
n = nlogn
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
log n
the smallest of these
is stored here
n
n = nlogn
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
n
n = nlogn
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
all of these
are stored here
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
Solution 2 on H
O(n log n) prep time
O(1) query time
O(n log n) space
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
Solution 2 on H
O(n log n) prep time
O(1) query time
O(n log n) space = O((
nlogn
)log(
nlogn
))
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
Solution 2 on H
O(n log n) prep time
O(1) query time
O(n log n) space = O((
nlogn
)log(
nlogn
))= O(n)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
Solution 2 on H
O(n log n) prep time
O(1) query time
O(n log n) space = O((
nlogn
)log(
nlogn
))= O(n)
= O(n)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2
Solution 2 on A
O(n logn) prep time
O(1) query time
O(n logn) space
Recall . . .
Solution 2 on H
O(n log n) prep time
O(1) query time
O(n log n) space = O((
nlogn
)log(
nlogn
))= O(n)
= O(n)
in O(n) space/prep time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2
Solution 2 on Li
O(1) query timeO((logn) log logn)) space/prep time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Solution 2 on Li
O(1) query timeO((logn) log logn)) space/prep time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Total space = O(n) +O(n logn log logn)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Total space = O(n) +O(n logn log logn)
space for RMQ structures
for all the Li arraysspace for RMQ structure for H
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Total space = O(n) +O(n logn log logn)
space for RMQ structures
for all the Li arraysspace for RMQ structure for H
= O(n log logn)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Total space = O(n) +O(n logn log logn) = O(n log logn)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
using Solution 2 in O(n) space/prep time
Preprocess each array Li (which has length logn) to answer RMQs. . .
using Solution 2 in O(logn log logn) space/prep time
Total space = O(n) +O(n logn log logn) = O(n log logn)
Total prep. time = O(n log logn)
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
indices into H
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
then take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
This takes O(1) total query timethen take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
This takes O(1) total query timethen take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
This takes O(1) total query timethen take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
Solution 3
O(n log logn) space O(n log logn) prep time O(1) query time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
This takes O(1) total query timethen take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
Solution 4
O(n log log logn) space O(n log log logn) prep time O(1) query time
L0L1
L2L3
L4L5 Ln
Low-resolution RMQ
A
Key Idea replace A with a smaller, ‘low resolution’ array H
H
n
i j
and many small arrays L0, L1, L2 . . . ‘for the details’
n
n = nlogn
How do we answer a query in A?
Do at most one query in H . . .
and one query in at most two different Li (here we query L1 and L5)
This takes O(1) total query timethen take the smallest
i′ =⌈
ilogn
⌉j′ =
⌊j
logn
⌋i′ j′
Solution 4
O(n log log logn) space O(n log log logn) prep time O(1) query time
how?
L0L1
L2L3
L4L5 Ln
Range minimum query summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
Solution 3
O(n log logn) space
O(n log logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Can we do O(n) space and O(1) query time?
Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
Solution 3
O(n log logn) space
O(n log logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Can we do O(n) space and O(1) query time? Yes. . .
Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
Solution 3
O(n log logn) space
O(n log logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Range minimum query summary
A
Preprocess an integer array A (length n) to answer range minimum queries. . .
n
After preprocessing, a range minimum query is given by RMQ(i, j)
the output is the location of the smallest element in A[i, j]
17 823 73 51 82 19 32 5 67 91 14 46
i = 3 j = 7
RMQ(3, 7) = 6
19 9 21 54
Can we do O(n) space and O(1) query time? Yes. . . but not until next lecture
Solution 1
O(n) space
O(n) prep time
O(logn) query time
Solution 2
O(n logn) space
O(n logn) prep time
O(1) query time
Solution 3
O(n log logn) space
O(n log logn) prep time
O(1) query time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15