Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries...
Transcript of Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries...
![Page 1: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/1.jpg)
Range Minimum and Lowest Common AncestorQueries
Slides by Solon P. Pissis
November 15, 2019
RMQs and LCA queries
![Page 2: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/2.jpg)
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
![Page 3: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/3.jpg)
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
![Page 4: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/4.jpg)
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
![Page 5: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/5.jpg)
Range Minimum Queries problem
DefinitionGiven an array A[1 . . . n], preprocess A so that a minimum of anyfragment A[i . . . j ] can be computed efficiently:
RMQA(i , j) = arg min A[k]
with 1 ≤ i ≤ k ≤ j ≤ n.
i jk1 n
Answering any query in O(1) time is trivial if we allow O(n2) timeand space preprocessing.
RMQs and LCA queries
![Page 6: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/6.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 7: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/7.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 8: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/8.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 9: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/9.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 10: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/10.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 11: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/11.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How?
B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 12: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/12.jpg)
O(n log n) time and space and O(1)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(1) time after O(n log n)time and space preprocessing.
We apply the well-known doubling technique for preprocessing.
For all k = 0, 1, . . . , log n construct:
Bk [i ] = min{A[i ],A[i + 1], . . . ,A[i + 2k − 1]}.
How? B0[i ] = A[i ] and Bk+1[i ] = min{Bk [i ],Bk [i + 2k ]}, for all i .
RMQs and LCA queries
![Page 13: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/13.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 14: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/14.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 15: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/15.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 16: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/16.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 17: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/17.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 18: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/18.jpg)
O(n log n) time and space and O(1)-time queries
And now the querying part.
Two cases:
I Fragment of length 2k : Trivial, use B;
I Otherwise, we can cover any range with two power-of-2ranges.
Compute k = blog(j − i + 1)c.
i j1 n
Return min{Bk [i ],Bk [j − 2k + 1]} in O(1) time!
RMQs and LCA queries
![Page 19: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/19.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 20: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/20.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 21: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/21.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 22: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/22.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 23: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/23.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 24: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/24.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.
RMQs and LCA queries
![Page 25: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/25.jpg)
O(n) time and space and O(log n)-time queries
Let us show the following lemma.
LemmaThe RMQ problem can be solved in O(log n) time after O(n) timeand space preprocessing.
(Segment tree.)
We apply the well-known micro-macro decomposition technique.
1 n
b = log n
Decompose array A into blocks of length b = log n.RMQs and LCA queries
![Page 26: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/26.jpg)
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
![Page 27: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/27.jpg)
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
![Page 28: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/28.jpg)
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
![Page 29: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/29.jpg)
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need?
O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
![Page 30: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/30.jpg)
O(n) time and space and O(log n)-time queries
1 n
b = log n
I Construct a new array A′:
A′[i ] = min{A[i · b + 1],A[i · b + 2], . . . ,A[(i + 1) · b]}.
I Build the previously described structure for A′[1 . . . nlog n ].
How much time and space do we need? O( nlog n log( n
log n )) = O(n).
RMQs and LCA queries
![Page 31: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/31.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 32: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/32.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 33: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/33.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 34: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/34.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 35: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/35.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 36: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/36.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems?
Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 37: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/37.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes!
When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 38: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/38.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 39: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/39.jpg)
O(n) time and space and O(log n)-time queries
Let i , j be a query (Notice: endpoints are inside blocks).
1 n
i j
I We do not have the min in the prefix and suffix of every block.
I For each block, precompute it: O(n) time and space!
I For the rest: use the structure we have built for A′.
I Now any such query takes O(1) time.
Any problems? Yes! When i , j are fully contained in a single block.
Solution: Naıve search in block gives O(log n)-time queries!
RMQs and LCA queries
![Page 40: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/40.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 41: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/41.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 42: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/42.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 43: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/43.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 44: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/44.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 45: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/45.jpg)
O(n) time and space and O(1)-time queries
OK. Now it is time to show the breakthrough.
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
I We only have to deal with the strictly-inside-a-block case.
I We will show how to do that for a very restricted case:
|A[i + 1]− A[i ]| = 1
I We will then explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 46: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/46.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 47: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/47.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 48: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/48.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 49: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/49.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 50: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/50.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 51: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/51.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 52: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/52.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Normalize blocks by subtracting the initial offset from everyelement.
I Choose b = 1/2 · log n (instead of b = log n).
I A normalized block is a vector of length b − 1.
I There are 2b−1 = Θ(√n) distinct vectors.
I Precompute and store all answers!
RMQs and LCA queries
![Page 53: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/53.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 54: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/54.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 55: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/55.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 56: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/56.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 57: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/57.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 58: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/58.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 59: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/59.jpg)
O(n) time and space and O(1)-time queries
Assume: |A[i + 1]− A[i ]| = 1.
Observation: If two arrays X [1 . . . k], Y [1 . . . k] differ by somefixed value at each position; i.e. there is a c such thatX [i ] = Y [i ] + c for every i , then all RMQ answers will be the samefor X and Y .
I Create Θ(√n) tables.
I In each table put all (log n/2)2 answers to all in-block queries.
I Θ(√n log2 n) extra preprocessing.
I Query time is O(1).
I Preprocessing time and space is O(n).
We still need to explain why this restricted case is sufficient!
RMQs and LCA queries
![Page 60: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/60.jpg)
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
![Page 61: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/61.jpg)
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
![Page 62: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/62.jpg)
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
![Page 63: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/63.jpg)
Lowest Common Ancestor queries
The lowest common ancestor (LCA) of two nodes u and v is thedeepest node that is an ancestor of both u and v .
u
v
DefinitionGiven a rooted tree T , preprocess T so that the LCA of u and vcan be computed efficiently.
RMQs and LCA queries
![Page 64: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/64.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 65: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/65.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 66: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/66.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 67: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/67.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 68: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/68.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 69: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/69.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 70: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/70.jpg)
Linear-Time Reduction: RMQ to LCA
Given array A we can build the Cartesian tree T of A:
I The root is a minimum element.
I Removing this element splits the array into two.
I Recurse on the two subarrays.
Querying:
A = [8, 7, 3, 20, 2, 17, 5, 21, 11, 12]
2
5
17 113
720
8
21 12
i j
T
RMQs and LCA queries
![Page 71: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/71.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 72: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/72.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 73: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/73.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).
I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 74: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/74.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.
I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 75: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/75.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 76: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/76.jpg)
Linear-Time Reduction: LCA to RMQ
Given a rooted tree T on n nodes we construct:
I E [1 . . . 2n − 1]: node labels traversed in an Euler Tour (DFS).I L[1 . . . 2n − 1]: the distance of E [i ] from the root.I R[i ] = min{j : E [j ] = i} (first occurrence of E [i ] in the tour).
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]
RMQs and LCA queries
![Page 77: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/77.jpg)
Linear-Time Reduction: LCA to RMQ
Querying:
I LCA(u, v) is translated to E [RMQL(R[u],R[v ])].
I LCA(5, 2) isE [RMQL(R[u],R[v ])] = E [RMQL(3, 6)] = E [4] = 4.
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]v u
u
v
i j
RMQs and LCA queries
![Page 78: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/78.jpg)
Linear-Time Reduction: LCA to RMQ
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]v u
u
v
i j
Any observation about array L?
Yes: |L[i + 1]− L[i ]| = 1.
RMQs and LCA queries
![Page 79: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/79.jpg)
Linear-Time Reduction: LCA to RMQ
0
4
5 1
2
3
E = [0, 4, 5, 4, 1, 2, 3, 2, 1, 4, 0]
L = [0, 1, 2, 1, 2, 3, 4, 3, 2, 1, 0]
R = [1, 5, 6, 7, 2, 3]v u
u
v
i j
Any observation about array L?
Yes: |L[i + 1]− L[i ]| = 1.
RMQs and LCA queries
![Page 80: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/80.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 81: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/81.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 82: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/82.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 83: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/83.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 84: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/84.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 85: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/85.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 86: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/86.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 87: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/87.jpg)
O(n) time and space and O(1)-time queries
Let’s put things together:
I Reduce RMQ problem to the LCA problem.
I Reduce LCA problem back to the RMQ problem.
I The reduction leads to a restricted RMQ problem.
I Solve it and we are done!
Theorem (Bender and Farach-Colton, LATIN 2000)
The RMQ problem can be solved in O(1) time after O(n) timeand space preprocessing.
Theorem (Bender and Farach-Colton, LATIN 2000)
The LCA problem can be solved in O(1) time after O(n) time andspace preprocessing.
RMQs and LCA queries
![Page 88: Range Minimum and Lowest Common Ancestor Queriesjrad/presentation.pdf · Range Minimum Queries problem De nition Given an array A[1:::n], preprocess A so that a minimum of any fragment](https://reader034.fdocuments.net/reader034/viewer/2022051107/6012e248fe552264a8745f9c/html5/thumbnails/88.jpg)
Note
Lecture by P. Charalampopoulos. I slightly edited the slides, so Iam responsible for any errors in them.
RMQs and LCA queries