3.4.16 · D1Trees

Foundations — Fenwick tree (Binary Indexed Tree) — prefix sums, O(log n) update and query

1,990 words9 min readBack to topic

This page assumes nothing. Before we can even read the parent note's first formula, we must earn every symbol in it: what a prefix sum is, how integers look in binary, what "the lowest set bit" means, what & and -i do, and why shows up. We build them in the order they depend on each other.


0. Counting numbers in a row (the array )

Picture it: boxes in a line, each with a label on top (1, 2, 3, …) and a value inside.

We choose to start counting at 1, not 0. Most programming languages start at 0, but — as you'll see in Section 4 — the whole Fenwick trick collapses at index 0, so we deliberately leave slot 0 empty and use .

Related idea in the vault: Prefix sum array.


1. Prefix sum — the question we keep asking

The little "" symbol you'll meet later just means "add all of these up". So The letter is a counter that walks through the addresses one by one — it isn't a fixed number, it's a placeholder.

Why the topic needs this: almost every question about a range of numbers ("what's the sum from box 3 to box 6?") reduces to a difference of two prefix sums: So if we can compute prefix sums fast, we can answer range questions fast. That is the entire goal.


2. Integers in binary — the alphabet of the trick

For example in binary is (the small means "this is base two"):

Picture it: think of switches. Each switch is worth double the one to its right. A number is which switches are ON.

A switch that is ON is called a set bit (bit = binary digit). The switch furthest to the right that is ON is the lowest set bit — "lowest" because it's the smallest power of two that's turned on.

Vault prerequisite: Binary representation of integers.

Notice the pattern: the lowest set bit is the largest power of two that divides the number evenly. , so is that power. This is exactly the "block length" the parent note uses.


3. Negative numbers in a computer — two's complement

Before we can compute the lowest set bit with the slick formula , we need to know what even looks like in bits, and what does.

Picture it: lay two rows of switches on top of each other; the AND keeps only the switches lit in both rows.

Here is the magic that this produces. Take .

step bits
flip all ()
add 1 ()

Now line up and and take the AND:

Only one switch survives — the lowest set bit, here the 's place. That's .


4. Why index 0 is forbidden

Now we can see the 1-indexing rule from Section 0 with fresh eyes.

Zero has no switches ON, so it has no lowest set bit. In the update loop j += lowbit(j) the step would be j += 0 — you never move, an infinite loop. In the query loop i -= lowbit(i) you subtract — you never make progress. That is why the Fenwick tree lives on indices and slot is dead space.


5. Blocks that nest — the over a range

Put the pieces together. The parent defines each cell as which now reads plainly: "add up the starting from index and ending at " — a block of exactly numbers ending at .

  • , : block covers indices to (length 4).
  • , : block covers indices to (length 2).
  • , : block covers index only (length 1).

Picture it: short blocks for odd indices (lowest bit = 1), longer blocks for indices divisible by high powers of two. They stack so that any prefix is a few of them laid end to end, and any single element sits inside only a few of them — that "few" is .


6. What is and why it appears

Both Fenwick loops change one bit of the index per step (strip a set bit, or add a bit that carries). A number has at most bits, so each loop runs at most times. That is the source of the speed. This is the same "logarithmic tree height" idea that powers the Segment tree.


Prerequisite map

Array a1..an with indices

Prefix sum: add up to i

1-indexing choice

Binary of integers

Lowest set bit

Bitwise AND

lowbit = i and minus i

Twos complement stores minus i

Cell owns a block of length lowbit

Fenwick tree topic

log n = number of bits


Equipment checklist

Test yourself — reveal only after you've answered.

What does mean and why do we start counting at 1?
is the value at position ; we start at 1 because would freeze the Fenwick loops.
What is in words?
The sum of all elements from position 1 up to and including .
How is a range sum built from prefix sums?
.
What does mean literally?
Add for every counter value from 1 to .
Write 12 in binary and name its lowest set bit.
; lowest set bit value is 4.
What does (bitwise AND) do?
Keeps a bit ON only where both and have it ON — the overlap of switch patterns.
How does a computer store ?
Two's complement: flip all bits of , then add 1.
Why does leave exactly the lowest set bit?
Below it there's no overlap (0s vs 1s), above it bits were flipped (disagree); only at the lowest set bit do both show 1.
What is and why is that a problem?
0 — the loops would never advance (infinite loop / no start), so index 0 is banned.
What does count, and why does it bound the loops?
The number of binary digits of ; each loop step changes one bit, so at most steps run.

Connections

  • Parent: Fenwick tree topic
  • Builds on: Prefix sum array, Binary representation of integers, Two's complement representation
  • Leads to: Segment tree, Range update with difference array, Inversion counting, Order statistics