<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Range_minimum_query</id>
		<title>Range minimum query - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Range_minimum_query"/>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;action=history"/>
		<updated>2026-04-24T09:12:19Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1780&amp;oldid=prev</id>
		<title>188.120.84.157: /* Dynamic */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1780&amp;oldid=prev"/>
				<updated>2014-04-24T16:46:49Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Dynamic&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 16:46, 24 April 2014&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L44&quot; &gt;Line 44:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 44:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Dynamic==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Dynamic==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The block-based solution handles the dynamic case as well; we must simply remember, whenever we update an element, to recompute the minimum element in the block it is in. This gives &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; time per update, and, assuming a uniform random distribution of updates, the expected update time is &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;constant&lt;/del&gt;. This is because if we decrease an element, we need only check whether the new value is less than the current minimum (constant time), whereas if we increase an element, we only need to recompute the minimum if the element updated was the minimum before (which takes &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; time but has a probability of occurring of only &amp;lt;math&amp;gt;O(1/m) = O(1/\sqrt{n})&amp;lt;/math&amp;gt;). Unfortunately, the query still has average-case time &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The block-based solution handles the dynamic case as well; we must simply remember, whenever we update an element, to recompute the minimum element in the block it is in. This gives &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; time per update, and, assuming a uniform random distribution of updates, the expected update time is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt;&lt;/ins&gt;. This is because if we decrease an element, we need only check whether the new value is less than the current minimum (constant time), whereas if we increase an element, we only need to recompute the minimum if the element updated was the minimum before (which takes &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; time but has a probability of occurring of only &amp;lt;math&amp;gt;O(1/m) = O(1/\sqrt{n})&amp;lt;/math&amp;gt;). Unfortunately, the query still has average-case time &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The [[segment tree]] can be computed in linear time and allows both queries and updates to be answered in &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; time. It also allows, with some cleverness, entire ranges to be updated at once (efficiently). Analysis of the average case is left as an exercise to the reader.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The [[segment tree]] can be computed in linear time and allows both queries and updates to be answered in &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; time. It also allows, with some cleverness, entire ranges to be updated at once (efficiently). Analysis of the average case is left as an exercise to the reader.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>188.120.84.157</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1776&amp;oldid=prev</id>
		<title>167.220.225.236: /* Sparse table */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1776&amp;oldid=prev"/>
				<updated>2014-03-05T06:10:34Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Sparse table&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 06:10, 5 March 2014&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L26&quot; &gt;Line 26:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 26:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Sparse table===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Sparse table===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;(&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Name &lt;/del&gt;due to &amp;lt;ref&amp;gt;&amp;quot;Range Minimum Query and Lowest Common Ancestor&amp;quot;. (n.d.). Retrieved from http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=lowestCommonAncestor#Range_Minimum_Query_%28RMQ%29&amp;lt;/ref&amp;gt;.) At the expense of space and preprocessing time, we can even answer queries in &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; using [[dynamic programming]]. Define &amp;lt;math&amp;gt;M_{i,k}&amp;lt;/math&amp;gt; to be the minimum of the elements &amp;lt;math&amp;gt;A_i, A_{i+1}, ..., A_{i+2^k-1}&amp;lt;/math&amp;gt; (or as many of those elements as actually exist); that is, the elements in an interval of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; starting from &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. Then, we see that &amp;lt;math&amp;gt;M_{i,0} = A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;M_{i,k+1} = \min(M_{i,k}, M_{i+2^k,k})&amp;lt;/math&amp;gt;; that is, the minimum in an interval of size &amp;lt;math&amp;gt;2^{k+1}&amp;lt;/math&amp;gt; is the smaller of the minima of the two halves of which it is composed, of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt;. Thus, each entry of &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; can be computed in constant time, and in total &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; has about &amp;lt;math&amp;gt;n\cdot \log n&amp;lt;/math&amp;gt; entries (since values of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;2^k &amp;gt; n&amp;lt;/math&amp;gt; are not useful). Then, given the query &amp;lt;math&amp;gt;[a,b)&amp;lt;/math&amp;gt;, simply find &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;[a,a+2^k)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;[b-2^k,b)&amp;lt;/math&amp;gt; overlap but are contained within &amp;lt;math&amp;gt;[a,b)&amp;lt;/math&amp;gt;; then we already know the minima in each of these two sub-intervals, and since they cover the query interval, the smaller of the two is the overall minimum. It's not too hard to see that the desired &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\lfloor\log(b-a)\rfloor&amp;lt;/math&amp;gt;; and then the answer is &amp;lt;math&amp;gt;\min(M_{a,k}, M_{b-2^k,k})&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;(&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Namely &lt;/ins&gt;due to &amp;lt;ref&amp;gt;&amp;quot;Range Minimum Query and Lowest Common Ancestor&amp;quot;. (n.d.). Retrieved from http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=lowestCommonAncestor#Range_Minimum_Query_%28RMQ%29&amp;lt;/ref&amp;gt;.) At the expense of space and preprocessing time, we can even answer queries in &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; using [[dynamic programming]]. Define &amp;lt;math&amp;gt;M_{i,k}&amp;lt;/math&amp;gt; to be the minimum of the elements &amp;lt;math&amp;gt;A_i, A_{i+1}, ..., A_{i+2^k-1}&amp;lt;/math&amp;gt; (or as many of those elements as actually exist); that is, the elements in an interval of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; starting from &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. Then, we see that &amp;lt;math&amp;gt;M_{i,0} = A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;M_{i,k+1} = \min(M_{i,k}, M_{i+2^k,k})&amp;lt;/math&amp;gt;; that is, the minimum in an interval of size &amp;lt;math&amp;gt;2^{k+1}&amp;lt;/math&amp;gt; is the smaller of the minima of the two halves of which it is composed, of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt;. Thus, each entry of &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; can be computed in constant time, and in total &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; has about &amp;lt;math&amp;gt;n\cdot \log n&amp;lt;/math&amp;gt; entries (since values of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;2^k &amp;gt; n&amp;lt;/math&amp;gt; are not useful). Then, given the query &amp;lt;math&amp;gt;[a,b)&amp;lt;/math&amp;gt;, simply find &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;[a,a+2^k)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;[b-2^k,b)&amp;lt;/math&amp;gt; overlap but are contained within &amp;lt;math&amp;gt;[a,b)&amp;lt;/math&amp;gt;; then we already know the minima in each of these two sub-intervals, and since they cover the query interval, the smaller of the two is the overall minimum. It's not too hard to see that the desired &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\lfloor\log(b-a)\rfloor&amp;lt;/math&amp;gt;; and then the answer is &amp;lt;math&amp;gt;\min(M_{a,k}, M_{b-2^k,k})&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Cartesian trees===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Cartesian trees===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>167.220.225.236</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1692&amp;oldid=prev</id>
		<title>Dhruvbird: /* Dynamic */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1692&amp;oldid=prev"/>
				<updated>2012-10-10T06:19:34Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Dynamic&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 06:19, 10 October 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L47&quot; &gt;Line 47:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 47:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The [[segment tree]] can be computed in linear time and allows both queries and updates to be answered in &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; time. It also allows, with some cleverness, entire ranges to be updated at once (efficiently). Analysis of the average case is left as an exercise to the reader.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The [[segment tree]] can be computed in linear time and allows both queries and updates to be answered in &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt; time. It also allows, with some cleverness, entire ranges to be updated at once (efficiently). Analysis of the average case is left as an exercise to the reader.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;We can also use any balanced binary tree (or dictionary data structure such as a skip-list) and augment it to support range minimum query operations with O(log n) per Update (Insert/Delete) as well as Query.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Dhruvbird</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1451&amp;oldid=prev</id>
		<title>Brian: needs code</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1451&amp;oldid=prev"/>
				<updated>2011-11-28T08:38:26Z</updated>
		
		<summary type="html">&lt;p&gt;needs code&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 08:38, 28 November 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L51&quot; &gt;Line 51:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 51:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Much of the information in this article was drawn from a single source:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Much of the information in this article was drawn from a single source:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Pages needing code]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1450&amp;oldid=prev</id>
		<title>Brian at 08:37, 28 November 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1450&amp;oldid=prev"/>
				<updated>2011-11-28T08:37:16Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;amp;diff=1450&amp;amp;oldid=1445&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1445&amp;oldid=prev</id>
		<title>Brian: Created page with &quot;The term '''range minimum query (RMQ)''' comprises all variations of the problem of finding the smallest element in a contiguous subsequence of a list of items taken from a [[tot...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Range_minimum_query&amp;diff=1445&amp;oldid=prev"/>
				<updated>2011-11-21T05:35:22Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;The term &amp;#039;&amp;#039;&amp;#039;range minimum query (RMQ)&amp;#039;&amp;#039;&amp;#039; comprises all variations of the problem of finding the smallest element in a contiguous subsequence of a list of items taken from a [[tot...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The term '''range minimum query (RMQ)''' comprises all variations of the problem of finding the smallest element in a contiguous subsequence of a list of items taken from a [[totally ordered set]] (usually numbers). This is one of the most extensively-studied problems in computer science, and many algorithms are known, each of which is appropriate for a specific variation.&lt;br /&gt;
&lt;br /&gt;
A single range minimum query is a set of indices &amp;lt;math&amp;gt;i &amp;lt; j&amp;lt;/math&amp;gt; into an array &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;; the answer to this query is some &amp;lt;math&amp;gt;k \in [i,j]&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_k \leq A_m&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;m \in [i,j]&amp;lt;/math&amp;gt;. In isolation, this query is answered simply by scanning through the range given and selecting the minimum element, which can take up to linear time. The problem is interesting because we often desire to answer a large number of queries, so that if, for example, 500000 queries are to be performed on a single array of 10000000 elements, then using this naive approach on each query individually is probably too slow.&lt;br /&gt;
&lt;br /&gt;
==Static==&lt;br /&gt;
In the ''static range minimum query'', the input is an array and a set of intervals (contiguous subsequences) of the array, identified by their endpoints, and for each interval we must find the smallest element contained therein. Modifications to the array are not allowed, but we must be prepared to handle queries &amp;quot;on the fly&amp;quot;; that is, we might not know all of them at once.&lt;br /&gt;
&lt;br /&gt;
===Sliding===&lt;br /&gt;
This problem can be solved in linear time in the special case in which the intervals are guaranteed to be given in such an order that they are successive elements of a [[sliding window]]; that is, each interval given in input neither starts earlier nor ends later than the previous one. This is the [[sliding range minimum query]] problem; an algorithm is given in that article.&lt;br /&gt;
&lt;br /&gt;
===Division into blocks===&lt;br /&gt;
In all other cases, we must consider other solutions. A simple solution for this and other related problems involves splitting the array into equally sized blocks (say, &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; elements each) and precomputing the minimum in each block. This precomputation will take &amp;lt;math&amp;gt;\Theta(n)&amp;lt;/math&amp;gt; time, since it takes &amp;lt;math&amp;gt;\Theta(m)&amp;lt;/math&amp;gt; time to find the minimum in each block, and there are &amp;lt;math&amp;gt;\Theta(n/m)&amp;lt;/math&amp;gt; blocks.&lt;br /&gt;
&lt;br /&gt;
After this, when we are given some query &amp;lt;math&amp;gt;[a,b]&amp;lt;/math&amp;gt;, we note that this can be written as the union of the intervals &amp;lt;math&amp;gt;[a,c_0), [c_0, c_1), [c_1, c_2), ..., [c_{k-1}, c_k), [c_k, b]&amp;lt;/math&amp;gt;, where all the intervals except for the first and last are individual blocks. If we can find the minimum in each of these subintervals, the smallest of those values will be the minimum in &amp;lt;math&amp;gt;[a,b]&amp;lt;/math&amp;gt;. But because all the intervals in the middle (note that there may be zero of these if &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b&amp;lt;/math&amp;gt; are in the same block) are blocks, their minima can simply be looked up in constant time.&lt;br /&gt;
&lt;br /&gt;
Observe that the intermediate intervals are &amp;lt;math&amp;gt;O(n/m)&amp;lt;/math&amp;gt; in number (because there are only about &amp;lt;math&amp;gt;n/m&amp;lt;/math&amp;gt; blocks in total). Furthermore, if we pick &amp;lt;math&amp;gt;c_0&amp;lt;/math&amp;gt; at the nearest available block boundary, and likewise with &amp;lt;math&amp;gt;c_k&amp;lt;/math&amp;gt;, then the intervals &amp;lt;math&amp;gt;[a,c_0)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;[c_k,b]&amp;lt;/math&amp;gt; have size &amp;lt;math&amp;gt;O(m)&amp;lt;/math&amp;gt; (since they do not cross block boundaries). By taking the minimum of all the precomputed block minima, and the elements in &amp;lt;math&amp;gt;[a,c_0)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;[c_k,b]&amp;lt;/math&amp;gt;, the answer is obtained in &amp;lt;math&amp;gt;O(m + n/m)&amp;lt;/math&amp;gt; time. If we choose a block size of &amp;lt;math&amp;gt;m \approx \sqrt{n}&amp;lt;/math&amp;gt;, we obtain &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; overall.&lt;br /&gt;
&lt;br /&gt;
===Segment tree===&lt;br /&gt;
While &amp;lt;math&amp;gt;O(\sqrt{n})&amp;lt;/math&amp;gt; is not optimal, it is certainly better than linear time, and suggests that we might be able to do better using precomputation. Instead of dividing the array into &amp;lt;math&amp;gt;\sqrt{n}&amp;lt;/math&amp;gt; pieces, we might consider dividing it in half, and then dividing each half in half, and dividing each quarter in half, and so on. The resulting structure of intervals and subintervals is a [[segment tree]]. It uses linear space and requires linear time to construct; and once it has been built, any query can be answered in &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time.&lt;br /&gt;
&lt;br /&gt;
===Sparse table===&lt;br /&gt;
(Name due to &amp;lt;ref&amp;gt;&amp;quot;Range Minimum Query and Lowest Common Ancestor&amp;quot;. (n.d.). Retrieved from http://community.topcoder.com/tc?module=Static&amp;amp;d1=tutorials&amp;amp;d2=lowestCommonAncestor#Range_Minimum_Query_%28RMQ%29&amp;lt;/ref&amp;gt;.) At the expense of space and preprocessing time, we can even answer queries in &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; using [[dynamic programming]]. Define &amp;lt;math&amp;gt;M_{i,k}&amp;lt;/math&amp;gt; to be the minimum of the elements &amp;lt;math&amp;gt;A_i, A_{i+1}, ..., A_{i+2^k-1}&amp;lt;/math&amp;gt; (or as many of those elements as actually exist); that is, the elements in an interval of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; starting from &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. Then, we see that &amp;lt;math&amp;gt;M_{i,0} = A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;M_{i,k+1} = \min(M_{i,k}, M_{i+2^k,k})&amp;lt;/math&amp;gt;; that is, the minimum in an interval of size &amp;lt;math&amp;gt;2^{k+1}&amp;lt;/math&amp;gt; is the smaller of the minima of the two halves of which it is composed, of size &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt;. Thus, each entry of &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; can be computed in constant time, and in total &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; has about &amp;lt;math&amp;gt;n\cdot \log n&amp;lt;/math&amp;gt; entries (since values of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;2^k &amp;gt; n&amp;lt;/math&amp;gt; are not useful). Then, given the query &amp;lt;math&amp;gt;[a,b]&amp;lt;/math&amp;gt;, simply find &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;[a,a+2^k)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;(b-2^k,b]&amp;lt;/math&amp;gt; overlap but are contained within &amp;lt;math&amp;gt;[a,b]&amp;lt;/math&amp;gt;; then we already know the minima in each of these two sub-intervals, and since they cover the query interval, the smaller of the two is the overall minimum. It's not too hard to see that the desired &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\lceil\log(b-a+1)\rceil&amp;lt;/math&amp;gt;; and then the answer is &amp;lt;math&amp;gt;\min(M_{a,k}, M_{b-2^k+1,k})&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- The TopCoder tutorial doesn't quite make sense here. How is the difference array used?&lt;br /&gt;
===Cartesian trees===&lt;br /&gt;
We can get the best of both worlds&amp;amp;mdash;that is, constant query time and linear preprocessing time and space&amp;amp;mdash;but the algorithm is somewhat more involved. It combines the block-based approach, the sparse table approach, and the use of [[Cartesian tree]]s.&lt;br /&gt;
&lt;br /&gt;
We observe that some minimum in a range in the given array occurs at the same place as the [[lowest common ancestor]] of the two nodes corresponding to the endpoints of the range in the Cartesian tree of the array. That is, if elements &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;A_j&amp;lt;/math&amp;gt; occur at nodes &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; in the Cartesian tree of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, then there is some &amp;lt;math&amp;gt;A_k&amp;lt;/math&amp;gt; in the range &amp;lt;math&amp;gt;A_{i..j}&amp;lt;/math&amp;gt; such that the element &amp;lt;math&amp;gt;A_k&amp;lt;/math&amp;gt; occurs at the lowest common ancestor of &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; in the Cartesian tree.&lt;br /&gt;
&lt;br /&gt;
''Proof'': An inorder traversal of the Cartesian tree gives the original array. Thus, consider the segment &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; of the inorder traversal beginning when &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; is visited and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is visited (&amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; are as above); this must be equivalent to the segment of the array &amp;lt;math&amp;gt;A_{i..j}&amp;lt;/math&amp;gt;. Now, if the LCA of &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is in its right subtree; but &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; must then contain only &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and elements from &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt;'s right subtree, since all nodes in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;'s right subtree (including &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;) will be visited immediately after &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; itself; so that all nodes in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; are descendants of &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt;. Likewise, if &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is the LCA, then &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; consists entirely of descendants of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. If the LCA is neither &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; nor &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; must occur in its left subtree and &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; in its right subtree (because if they both occurred in the same subtree, then the root of that subtree would be a lower common ancestor, a contradiction). But all elements in the left subtree of the LCA, including &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; are visited immediately before the LCA, and all elements in the right, including &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;, are visited immediately after the LCA, and hence, again, all nodes in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; are descendants of the LCA. Now, the labels on the nodes in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; correspond to the elements &amp;lt;math&amp;gt;A_i, ..., A_j&amp;lt;/math&amp;gt;, and all nodes in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; have labels less than or equal to the label of the LCA, since Cartesian trees are min-heap-ordered; so it follows that the label of the LCA is the minimum element in the range. &amp;lt;math&amp;gt;_{\blacksquare}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LCA queries may be answered in &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time with &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; preprocessing. This involves reducing LCA back to RMQ using the technique described in the [[Lowest common ancestor]] article, but now the difference between adjacent elements of the array, which we'll call &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, is either +1 or -1. We construct array &amp;lt;math&amp;gt;C&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;C_i = B_{i+1} - B_i&amp;lt;/math&amp;gt;, so that &amp;lt;math&amp;gt;C_i = \pm 1&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. Then, given some query &amp;lt;math&amp;gt;[i,j]&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, we observe that &amp;lt;math&amp;gt;B_k - B_i = C_i + C_{i+1} + ... + C_{k-1}&amp;lt;/math&amp;gt;, and therefore the smallest element in the range is the one at index &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; for which &amp;lt;math&amp;gt;C_i + ... + C_{k-1}&amp;lt;/math&amp;gt; is minimal.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
Much of the information in this article was drawn from a single source:&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>