<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://michaelnielsen.org/polymath/index.php?action=history&amp;feed=atom&amp;title=Updating_partial_sums_with_Fenwick_tree</id>
	<title>Updating partial sums with Fenwick tree - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://michaelnielsen.org/polymath/index.php?action=history&amp;feed=atom&amp;title=Updating_partial_sums_with_Fenwick_tree"/>
	<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;action=history"/>
	<updated>2026-06-03T08:09:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.3</generator>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=3755&amp;oldid=prev</id>
		<title>Mio: hardly material change in the for statement</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=3755&amp;oldid=prev"/>
		<updated>2010-12-28T02:37:07Z</updated>

		<summary type="html">&lt;p&gt;hardly material change in the for statement&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 19:37, 27 December 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l30&quot;&gt;Line 30:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 30:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     // add val to the k-th element&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     // add val to the k-th element&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     void add(int k, int val) {&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     void add(int k, int val) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;; i&amp;lt;int(&lt;/del&gt;v_.size()&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;)&lt;/del&gt;; i += -i&amp;amp;i)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;, e=&lt;/ins&gt;v_.size()&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;; i&amp;lt;e&lt;/ins&gt;; i += -i&amp;amp;i)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             v_[i] += val;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             v_[i] += val;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2955&amp;oldid=prev</id>
		<title>Mio at 00:19, 2 February 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2955&amp;oldid=prev"/>
		<updated>2010-02-02T00:19:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:19, 1 February 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot;&gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, which satisfy the following:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, which satisfy the following:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&#039;&#039;&#039;Property 1&#039;&#039;&#039; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Orbit &lt;/del&gt;of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and nowhere otherwise.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&#039;&#039;&#039;Property 1&#039;&#039;&#039; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;For any two positive integers &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;, orbit &lt;/ins&gt;of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and nowhere otherwise.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The general algorithm when we have &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; that satisfy the property above is as follows. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. By updating something we mean adding a quantity to it, whether positive or negative. On each update of the &amp;lt;math&amp;gt;i^{th}&amp;lt;/math&amp;gt; sequence element, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The general algorithm when we have &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; that satisfy the property above is as follows. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. By updating something we mean adding a quantity to it, whether positive or negative. On each update of the &amp;lt;math&amp;gt;i^{th}&amp;lt;/math&amp;gt; sequence element, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2948&amp;oldid=prev</id>
		<title>Mio at 00:15, 2 February 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2948&amp;oldid=prev"/>
		<updated>2010-02-02T00:15:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:15, 1 February 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l11&quot;&gt;Line 11:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 11:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; satisfy Property 1.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; satisfy Property 1.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;0 &amp;lt; &lt;/ins&gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;. (Actually, &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt; will parse just fine since unary minus has higher precedence in C than bitwise and.)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;. (Actually, &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt; will parse just fine since unary minus has higher precedence in C than bitwise and.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2927&amp;oldid=prev</id>
		<title>Mio at 19:47, 1 February 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2927&amp;oldid=prev"/>
		<updated>2010-02-01T19:47:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:47, 1 February 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l13&quot;&gt;Line 13:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 13:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;. (Actually, &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt; will parse just fine since unary minus has higher precedence in C than &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;binary &lt;/del&gt;and.)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;. (Actually, &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt; will parse just fine since unary minus has higher precedence in C than &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;bitwise &lt;/ins&gt;and.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2914&amp;oldid=prev</id>
		<title>Mio at 10:35, 1 February 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2914&amp;oldid=prev"/>
		<updated>2010-02-01T10:35:41Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 03:35, 1 February 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;. &lt;/del&gt;&amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time in the worst case, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;, where &lt;/ins&gt;&amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time in the worst case, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all &lt;/del&gt;the &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;which satisfy &lt;/ins&gt;the &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;following:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Here&#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;are two such actions. What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;&#039;&#039;&#039;Property 1&#039;&#039;&#039; Orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and nowhere otherwise. &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;The general algorithm when we have &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; that satisfy the property above is as follows. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. By updating something we mean adding a quantity to it, whether positive or negative. On each update of the &amp;lt;math&amp;gt;i^{th}&amp;lt;/math&amp;gt; sequence element, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;The two straightforward algorithms we discussed in the first paragraph already fit into this scheme. In the case (a) above, &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity. It&#039;s obvious that in both cases the pair &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; satisfy Property 1.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Here&#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;satisfy Property 1&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;. (Actually, &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt; will parse just fine since unary minus has higher precedence in C than binary and.)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l22&quot;&gt;Line 22:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 30:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     // add val to the k-th element&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     // add val to the k-th element&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     void add(int k, int val) {&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     void add(int k, int val) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k; i&amp;lt;int(v_.size()); i += &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(&lt;/del&gt;-i&amp;amp;i&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;)&lt;/del&gt;)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k; i&amp;lt;int(v_.size()); i += -i&amp;amp;i)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             v_[i] += val;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             v_[i] += val;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l29&quot;&gt;Line 29:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 37:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     int get(int k) const {&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     int get(int k) const {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         int r=0;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         int r=0;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k; i&amp;gt;0; i -= &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(&lt;/del&gt;-i&amp;amp;i&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;)&lt;/del&gt;)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;         for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             r += v_[i];&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             r += v_[i];&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         return r;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         return r;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2879&amp;oldid=prev</id>
		<title>Mio at 06:28, 28 January 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2879&amp;oldid=prev"/>
		<updated>2010-01-28T06:28:04Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:28, 27 January 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time. &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(although it&#039;s not really a tree) &lt;/del&gt;has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time in the worst case, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time. &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time in the worst case, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2877&amp;oldid=prev</id>
		<title>Mio: Undo revision 2876 by Mio (Talk)</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2877&amp;oldid=prev"/>
		<updated>2010-01-27T21:49:33Z</updated>

		<summary type="html">&lt;p&gt;Undo revision 2876 by &lt;a href=&quot;/polymath/index.php?title=Special:Contributions/Mio&quot; title=&quot;Special:Contributions/Mio&quot;&gt;Mio&lt;/a&gt; (&lt;a href=&quot;/polymath/index.php?title=User_talk:Mio&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:Mio (page does not exist)&quot;&gt;Talk&lt;/a&gt;)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 14:49, 27 January 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l5&quot;&gt;Line 5:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 5:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(&lt;/del&gt;We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this.&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;) &lt;/del&gt;The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2876&amp;oldid=prev</id>
		<title>Mio at 21:47, 27 January 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2876&amp;oldid=prev"/>
		<updated>2010-01-27T21:47:38Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 14:47, 27 January 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l5&quot;&gt;Line 5:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 5:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&amp;#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;(-i)&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(&lt;/ins&gt;We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this.&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;) &lt;/ins&gt;The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here&amp;#039;s a simple implementation in C++, add error checking as needed&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2875&amp;oldid=prev</id>
		<title>Mio at 21:46, 27 January 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2875&amp;oldid=prev"/>
		<updated>2010-01-27T21:46:22Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 14:46, 27 January 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot;&gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Here&#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;-i&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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;Here&#039;s the punchline: for a positive integer &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, if we call &amp;lt;math&amp;gt;r(n)&amp;lt;/math&amp;gt; the integer we get when we isolate the rightmost, i.e. the least significant, set bit in the binary representation of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so e.g. &amp;lt;math&amp;gt;r(6) = 2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;r(8) = 8&amp;lt;/math&amp;gt;), then &amp;lt;math&amp;gt;A(i) = i + r(i)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B(i) = i - r(i)&amp;lt;/math&amp;gt; are two such actions. What&#039;s more, in C and its ilk, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;code&amp;gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;(&lt;/ins&gt;-i&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;)&lt;/ins&gt;&amp;amp;i&amp;lt;/code&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Proof: &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; increases its argument while &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; decreases it, so if &amp;lt;math&amp;gt;x=y&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;A^0(x) = B^0(y)&amp;lt;/math&amp;gt;, and that&amp;#039;s the only intersection. If &amp;lt;math&amp;gt;x &amp;lt; y&amp;lt;/math&amp;gt;, then binary representations of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are &amp;lt;math&amp;gt;y = a1b_y&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x = a0b_x&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are (possibly empty) binary sequences, and &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;b_y&amp;lt;/math&amp;gt; are of same length &amp;lt;math&amp;gt;|b|&amp;lt;/math&amp;gt;. We may have had to pad the representation of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; by zeros to the left as needed for this. The action &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; unsets the rightmost set bit, so eventually &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; arrive at &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, if it&amp;#039;s not of that form to begin with. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has no set bits, then one more action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; will make it equal to &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;b_x&amp;lt;/math&amp;gt; has set bits, then &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, which turns a number of the form &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;z10^{m+n}&amp;lt;/math&amp;gt; will eventually bring &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;a10^{|b|}&amp;lt;/math&amp;gt;, which is where we left &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;. By the argument from the beginning, this is the only place the two orbits intersect. QED&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2868&amp;oldid=prev</id>
		<title>Mio at 22:09, 26 January 2010</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2868&amp;oldid=prev"/>
		<updated>2010-01-26T22:09:59Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 15:09, 26 January 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time. &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure (although it&#039;s not really a tree) has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; 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 two straightforward ways to code updating and retrieval of partial sums of sequences are: (a) maintain the array of sequence elements - on each update of a sequence element update just the sequence element, and each time you need a partial sum you compute it from scratch, (b) maintain the array of partial sums - on each update of a sequence element update all the partial sums this element contributes to. In the case (a) update takes &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time and retrieval takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt;, in the case (b) update takes &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; and retrieval &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; time. &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is the length of the sequence. The [http://www.cs.ubc.ca/local/reading/proceedings/spe91-95/spe/vol24/issue3/spe884.pdf Fenwick tree] data structure (although it&#039;s not really a tree) has both update and retrieval taking &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;in the worst case&lt;/ins&gt;, while still using the same &amp;lt;math&amp;gt;O(N)&amp;lt;/math&amp;gt; amount of space.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The idea is to have two actions on positive integers, say &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt;, so that orbit of &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, and orbit of &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; under action of &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; intersect in exactly one point if &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;, and none otherwise. We will maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; with all elements initialized to 0. On each update of a sequence element &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, update all the elements of the array with indices in the &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; by that same amount. To retrieve the partial sum of elements &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; thru &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, sum up all the elements from the array with indices in the &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; orbit of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. In the case (a) above &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is identity and &amp;lt;math&amp;gt;B(j) = j-1&amp;lt;/math&amp;gt;, for (b) it&amp;#039;s &amp;lt;math&amp;gt;A(i) = i+1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; is the identity.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
</feed>