<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://michaelnielsen.org/polymath/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mio</id>
	<title>Polymath Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://michaelnielsen.org/polymath/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mio"/>
	<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Special:Contributions/Mio"/>
	<updated>2026-04-23T08:37:53Z</updated>
	<subtitle>User contributions</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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=3755"/>
		<updated>2010-12-28T02:37:07Z</updated>

		<summary type="html">&lt;p&gt;Mio: hardly material change in the for statement&lt;/p&gt;
&lt;hr /&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, where &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Property 1&#039;&#039;&#039; 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 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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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; satisfy Property 1.&lt;br /&gt;
&lt;br /&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;0 &amp;lt; 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;br /&gt;
&lt;br /&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 bitwise and.)&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k, e=v_.size(); i&amp;lt;e; i += -i&amp;amp;i)&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2955"/>
		<updated>2010-02-02T00:19:45Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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, where &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Property 1&#039;&#039;&#039; 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 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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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; satisfy Property 1.&lt;br /&gt;
&lt;br /&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;0 &amp;lt; 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;br /&gt;
&lt;br /&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 bitwise and.)&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += -i&amp;amp;i)&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2948"/>
		<updated>2010-02-02T00:15:56Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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, where &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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; satisfy Property 1.&lt;br /&gt;
&lt;br /&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;0 &amp;lt; 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;br /&gt;
&lt;br /&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 bitwise and.)&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += -i&amp;amp;i)&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2927"/>
		<updated>2010-02-01T19:47:45Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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, where &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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; satisfy Property 1.&lt;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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 bitwise and.)&lt;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += -i&amp;amp;i)&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2914"/>
		<updated>2010-02-01T10:35:41Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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, where &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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; satisfy Property 1.&lt;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += -i&amp;amp;i)&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= -i&amp;amp;i)&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2879"/>
		<updated>2010-01-28T06:28:04Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2877"/>
		<updated>2010-01-27T21:49:33Z</updated>

		<summary type="html">&lt;p&gt;Mio: Undo revision 2876 by Mio (Talk)&lt;/p&gt;
&lt;hr /&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 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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2876"/>
		<updated>2010-01-27T21:47:38Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2875"/>
		<updated>2010-01-27T21:46:22Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2868"/>
		<updated>2010-01-26T22:09:59Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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=2867</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2867"/>
		<updated>2010-01-26T21:14:48Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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=2866</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2866"/>
		<updated>2010-01-26T20:50:19Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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;br /&gt;
&lt;br /&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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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=2865</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2865"/>
		<updated>2010-01-26T19:18:59Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 O(1) time and retrieval takes O(N), in the case (b) update takes O(N) and retrieval O(1) 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;br /&gt;
&lt;br /&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. Then maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; 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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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=2864</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2864"/>
		<updated>2010-01-26T19:07:24Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 O(1) time and retrieval takes O(N), in the case (b) update takes O(N) and retrieval O(1) 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;br /&gt;
&lt;br /&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. Then maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; 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&#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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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 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 a &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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Experimental_results&amp;diff=2863</id>
		<title>Experimental results</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Experimental_results&amp;diff=2863"/>
		<updated>2010-01-26T18:46:20Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[The Erd&amp;amp;#337;s discrepancy problem|To return to the main Polymath5 page, click here]].&lt;br /&gt;
 &lt;br /&gt;
Perhaps we should have two kinds of subpages to this page: Pages about finding examples, and pages about analyzing them?&lt;br /&gt;
&lt;br /&gt;
== Experimental data==&lt;br /&gt;
* [[The first 1124-sequence]] with discrepancy 2. &#039;&#039;Some more description&#039;&#039;&lt;br /&gt;
* Other [[length 1124 sequences]] with discrepancy 2. &#039;&#039;Some more description&#039;&#039;&lt;br /&gt;
* A [[sequence of length 1091]] with discrepancy 2.&lt;br /&gt;
* A [[sequence of length 1112]] derived from one with nice multiplicative properties.&lt;br /&gt;
* [[Sequences given by modulated Sturmian functions]].&lt;br /&gt;
* Some data about the problem with [[different upper and lower bound]]. Let N(a,b) be the largest N such that there is a sequence &amp;lt;math&amp;gt;x_1,\dots,x_N&amp;lt;/math&amp;gt; all of whose HAP-errors are between -a and b, inclusive.&lt;br /&gt;
* Sequences taking values in &amp;lt;math&amp;gt;\mathbb{T}&amp;lt;/math&amp;gt;:&lt;br /&gt;
** [[4th roots of unity]]&lt;br /&gt;
** [[6th roots of unity]]&lt;br /&gt;
* [http://thomas1111.wordpress.com/2010/01/10/tables-for-a-c10-candidate/ A sequence of length 407] with discrepancy 2 such that &amp;lt;math&amp;gt;x_n=x_{32 n}&amp;lt;/math&amp;gt; for every n. [[The HAP-subsequence structure of that sequence]].&lt;br /&gt;
* More [[T32-invariant sequences]].&lt;br /&gt;
* Long [[multiplicative sequences]].&lt;br /&gt;
* Long sequences satisfying [[T2(x) = -x]].&lt;br /&gt;
* Long sequences satisfying [[T2(x) = T5(x) = -x]]&lt;br /&gt;
* Long sequences satisfying [[T2(x) = -T3(x)]].&lt;br /&gt;
* Long sequences satisfying constraints of the form [[T_m(x) = (+/-)T_n(x)]].&lt;br /&gt;
* Table of [[longest constrained sequences]].&lt;br /&gt;
* Table of [[short sequences statistics]].&lt;br /&gt;
* [[Dirichlet inverses]] of good sequences.&lt;br /&gt;
* Sequences with [[bounded Dirichlet inverse]].&lt;br /&gt;
* [[Shifts and signs]] related to an interesting structure of the first 1124-sequence.&lt;br /&gt;
* Long sequences with [[low discrepancy on PAPs]].&lt;br /&gt;
&lt;br /&gt;
==Source code==&lt;br /&gt;
&lt;br /&gt;
* [[Convert raw input string into CSV table]]&lt;br /&gt;
* [[Create tables in an HTML file from an input sequence]]&lt;br /&gt;
* [[Verify the bounded discrepancy of an input sequence]]&lt;br /&gt;
* [[Depth-first search]]&lt;br /&gt;
* [[Search for completely multiplicative sequences]]&lt;br /&gt;
* [[Refined greedy computation of multiplicative sequences]]&lt;br /&gt;
* [[Computing a HAP basis]]&lt;br /&gt;
* [[Estimate the number of discrepancy 2 sequences]]&lt;br /&gt;
* [[Updating partial sums with Fenwick tree]]&lt;br /&gt;
&lt;br /&gt;
==Wish list==&lt;br /&gt;
&lt;br /&gt;
There is a separate page for [[proposals for finding long low-discrepancy sequences]]. It goes without saying that implementing any of these proposals belongs to the wish list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
* What is the discrepancy of the sequence defined in [http://gowers.wordpress.com/2010/01/09/erds-discrepancy-problem-continued/ this post],   &lt;br /&gt;
DONE, i think.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
* Find long/longest quasi-multiplicative sequences with some fixed group G, function &amp;lt;math&amp;gt;G\to \{-1,1\}&amp;lt;/math&amp;gt; and maximal discrepancy C&lt;br /&gt;
** &amp;lt;math&amp;gt;G=C_6&amp;lt;/math&amp;gt; and the function that sends 0,1 and 2 to 1 (because this seems to be a good choice)&lt;br /&gt;
* Do a &amp;quot;Mark-Bennet-style analysis&amp;quot; of one of the new 1124-sequences. [http://gowers.wordpress.com/2010/01/06/erdss-discrepancy-problem-as-a-forthcoming-polymath-project/#comment-4827] Also [http://gowers.wordpress.com/2010/01/06/erdss-discrepancy-problem-as-a-forthcoming-polymath-project/#comment-4842 done] (by Mark Bennet).&lt;br /&gt;
*. Take a moderately large k and search for the longest sequence of discrepancy 2 that&#039;s constructed as follows. First, pick a completely multiplicative function f to the group &amp;lt;math&amp;gt;C_{2k}&amp;lt;/math&amp;gt;. Then set &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; to be 1 if f(n) lies between 0 and k-1, and -1 if f(n) lies between k and 2k-1. Alec has already [http://gowers.wordpress.com/2009/12/17/erdoss-discrepancy-problem/#comment-4563 done this for k=1] and [http://gowers.wordpress.com/2010/01/06/erdss-discrepancy-problem-as-a-forthcoming-polymath-project/#comment-4734 partially done it for k=3].&lt;br /&gt;
*Search for the longest sequence of discrepancy 2 with the property that &amp;lt;math&amp;gt;x_n=x_{32n}&amp;lt;/math&amp;gt; for every n. The motivation for this is to produce a fundamentally different class of examples (different because their group structure would include an element of order 5). It&#039;s not clear that it will work, since 32 is a fairly large number. However, if you&#039;ve chosen &amp;lt;math&amp;gt;x_{32n}&amp;lt;/math&amp;gt; then that will have some influence on several other choices, such as &amp;lt;math&amp;gt;x_{4n},x_{8n}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;x_{16n}&amp;lt;/math&amp;gt;, so maybe it will lead to something interesting.  Alec [http://gowers.wordpress.com/2010/01/09/erds-discrepancy-problem-continued/#comment-4873 has made a start on this] and an [http://gowers.wordpress.com/2010/01/09/erds-discrepancy-problem-continued/#comment-4874 initial investigation] suggests that the sequence he has found does indeed have some &amp;lt;math&amp;gt;C_{10}&amp;lt;/math&amp;gt;-related structure. &lt;br /&gt;
*Here&#039;s another experiment that should be pretty easy to program and might yield something interesting. It&#039;s to look at the how the discrepancy appears to grow when you define a sequence using a greedy algorithm. I say &amp;quot;a&amp;quot; greedy algorithm because there are various algorithms that could reasonably be described as greedy. Here are a few.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
1. For each n let &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; be chosen so as to minimize the discrepancy so far, given the choices already made for &amp;lt;math&amp;gt;x_1,\dots,x_{n-1}&amp;lt;/math&amp;gt;. (If this does not uniquely determine &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; then choose it arbitrarily, or randomly, or according to some simple rule like always equalling 1.)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
2. Same as 1 but with additional constraints, in the hope that these make the sequence more likely to be good. For instance, one might insist that &amp;lt;math&amp;gt;x_{2k}=x_{3k}&amp;lt;/math&amp;gt; for every k. Here, when choosing &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; one would probably want to minimize the discrepancy up to &amp;lt;math&amp;gt;x_{n+k}&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x_{n+1},\dots,x_{n+k}&amp;lt;/math&amp;gt; had already been chosen. Another obvious constraint to try is complete multiplicativity.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
3. A greedy algorithm of sorts, but this time trying to minimize a different parameter. The first algorithm will do this: when you pick &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; you look, for each factor d of n, at the partial sum along the multiples of d up to but not including n. This will give you a set A of numbers (the possible partial sums). If max(A) is greater than max(-A) then you set &amp;lt;math&amp;gt;x_n=-1&amp;lt;/math&amp;gt;, if max(-A) is greater than max(A) then you let &amp;lt;math&amp;gt;x_n=1&amp;lt;/math&amp;gt;, and if they are equal then you make the decision according to some rule that seems sensible. But it might be that you would end up with a slower-growing discrepancy if you regarded A as a multiset and made the decision on some other basis. For instance, you could take the sum of &amp;lt;math&amp;gt;2^k&amp;lt;/math&amp;gt; over all positive elements &amp;lt;math&amp;gt;k\in A&amp;lt;/math&amp;gt; (with multiplicity) and the sum of &amp;lt;math&amp;gt;2^{-k}&amp;lt;/math&amp;gt; over all negative elements and choose &amp;lt;math&amp;gt;x_n&amp;lt;/math&amp;gt; according to which was bigger. Although that wouldn&#039;t minimize the discrepancy at each stage, it might make the sequence better for future development because it wouldn&#039;t sacrifice the needs of an overwhelming majority to those of a few rogue elements.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
4. A greedy algorithm to choose a good completely multiplicative low-discrepancy sequence. Now you are free only to choose the values at primes. If you have chosen the values up to but not including p, then fill in all the values that are forced by multiplicativity and then make whatever seems to be the best choice for the value at p. Again, there are several approaches that could be reasonable here. One is simply to ensure that the partial sum of the sequence up to p is as small (in modulus) as you can make it. But that would be foolish if you&#039;ve already filled in the values at p+1,...,p+k. So an only slightly less greedy algorithm is to look at the effect of your choice at p on the partial sums all the way up to the next prime and choose the best value accordingly. If you do that, then at what rate do the partial sums grow? In particular, do they grow at least logarithmically? [http://michaelnielsen.org/polymath1/index.php?title=Multiplicative_sequences#Minimizing_D_up_to_the_next_prime This is being addressed here]&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
The motivation for these experiments is to see whether they, or some variants, appear to lead to sublogarithmic growth. If they do, then we could start trying to prove rigorously that sublogarithmic growth is possible. I still think that a function that arises in nature and satisfies f(1124)=2 ought to be sublogarithmic.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*What happens if one applies a backtracking algorithm to try to extend the following discrepancy-2 sequence, which satisfies &amp;lt;math&amp;gt;x_{2n}=-x_n&amp;lt;/math&amp;gt; for every n, to a much longer discrepancy-2 sequence: + - - + - + + - - + + - + - + + - + - - + - - + + - - + + - + - - + - - + + + + - - - + + + - - + - + + - + - - + - ? This question has been answered [http://gowers.wordpress.com/2010/01/09/erds-discrepancy-problem-continued/#comment-4893 in the comments following the asking of the question on the blog]. &lt;br /&gt;
&lt;br /&gt;
* Investigate what happens if our HAPs are restricted to allow differences divisible only by 2 or 3 [and then other sets of primes including 2] - {2,3,5,7} would be interesting - is there an infinite sequence of discrepancy 2 in these simple cases - is it easy to find an infinite sequence with finite discrepancy in these cases? [for sets of odd primes, take a sequence which is 1 on odd numbers, -1 on even numbers. Including 2 is the non-trivial case]. It is possible that completely multiplicative sequences could be found for some of these cases.&lt;br /&gt;
&lt;br /&gt;
* Compute the Dirichlet series &amp;lt;math&amp;gt;f(s) = \sum x_n n^{-s}&amp;lt;/math&amp;gt; for some of our long low-discrepancy series, and see what this function looks like in the vicinity of &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;, and elsewhere. [http://gowers.wordpress.com/2010/01/11/the-erds-discrepancy-problem-iii/#comment-5062  Alec has now looked at this].&lt;br /&gt;
&lt;br /&gt;
*Take a long sequence &amp;lt;math&amp;gt;(x_n)&amp;lt;/math&amp;gt; of discrepancy 2 and try to create a new long sequence &amp;lt;math&amp;gt;(y_n)&amp;lt;/math&amp;gt; subject to the constraint that &amp;lt;math&amp;gt;y_{2n}=x_n&amp;lt;/math&amp;gt;. How far does one typically get before getting stuck? And how much further does one get if one uses the resulting sequence as a seed for the usual algorithm? [http://gowers.wordpress.com/2010/01/14/the-erds-discrepancy-problem-iv/#comment-5096  One does not get too far, as Alec showed].&lt;br /&gt;
&lt;br /&gt;
*Take some of the good sequences and calculate the following parameter, which is supposed to measure the amount of multiplicity. If the sequence is defined up to n, then the parameter is the expected value of &amp;lt;math&amp;gt;x_ax_bx_cx_d&amp;lt;/math&amp;gt; over all quadruples (a,b,c,d) such that a,b,c,d are at most n and ab=cd. I&#039;m expecting that the answer will be significantly greater than zero -- perhaps something like 0.3 -- but would like to have this confirmed. [http://gowers.wordpress.com/2010/01/14/the-erds-discrepancy-problem-iv/#comment-5132 It has now been confirmed].&lt;br /&gt;
&lt;br /&gt;
* ... you are welcome to add more.&lt;/div&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=2862</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2862"/>
		<updated>2010-01-26T18:45:09Z</updated>

		<summary type="html">&lt;p&gt;Mio: &lt;/p&gt;
&lt;hr /&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 O(1) time and retrieval takes O(N), in the case (b) update takes O(N) and retrieval O(1) 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;br /&gt;
&lt;br /&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. Then maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; 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 up to &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;br /&gt;
&lt;br /&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;br /&gt;
&lt;br /&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 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 a &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;br /&gt;
&lt;br /&gt;
Here&#039;s a simple implementation in C++, add error checking as needed&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class fenwick_tree {&lt;br /&gt;
    std::vector&amp;lt;int&amp;gt; v_;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
    fenwick_tree(int N) : v_(N+1) {}&lt;br /&gt;
&lt;br /&gt;
    // neither add() nor get() will work &lt;br /&gt;
    // for k outside of [1, N] range&lt;br /&gt;
&lt;br /&gt;
    // add val to the k-th element&lt;br /&gt;
    void add(int k, int val) {&lt;br /&gt;
        for (int i=k; i&amp;lt;int(v_.size()); i += (-i&amp;amp;i))&lt;br /&gt;
            v_[i] += val;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // get sum of elements 1 thru k&lt;br /&gt;
    int get(int k) const {&lt;br /&gt;
        int r=0;&lt;br /&gt;
        for (int i=k; i&amp;gt;0; i -= (-i&amp;amp;i))&lt;br /&gt;
            r += v_[i];&lt;br /&gt;
        return r;&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&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=2861</id>
		<title>Updating partial sums with Fenwick tree</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Updating_partial_sums_with_Fenwick_tree&amp;diff=2861"/>
		<updated>2010-01-26T18:04:58Z</updated>

		<summary type="html">&lt;p&gt;Mio: New page: Straightforward ways to code updating and retrieval of partial sums of sequences are to either: (a) maintain the array of sequence elements - on each update of a sequence element update ju...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Straightforward ways to code updating and retrieval of partial sums of sequences are to either: (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 O(1) time and retrieval takes O(N), in the case (b) update takes O(N) and retrieval O(1). The Fenwick tree data structure (although it&#039;s not really a tree) provides a way to do it so that both update and retrieval take &amp;lt;math&amp;gt;O(\log N)&amp;lt;/math&amp;gt; time.&lt;br /&gt;
&lt;br /&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 orbits 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 &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 zero othewise. Then maintain an array of size &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; 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 up to &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;br /&gt;
&lt;br /&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, &amp;lt;math&amp;gt;r(i)&amp;lt;/math&amp;gt; is coded as &amp;lt;math&amp;gt;-i &amp;amp; i&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&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 &amp;lt;math&amp;gt;A^0(x) = B^0(x)\/&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, b_x, 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 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 &amp;lt;math&amp;gt;z01^{m}0^{n}&amp;lt;/math&amp;gt; into a &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 orbits intersect. QED&lt;/div&gt;</summary>
		<author><name>Mio</name></author>
	</entry>
</feed>