Clear the lowest bit to query
transcript
A Fenwick tree stores block totals. Each entry holds the last few values. How do you total a prefix? The answer is a walk over bits.
Entry four holds positions one to four. Entry six holds positions five and six. Entry seven holds only position seven. Each entry covers a block that ends there.
Start at position seven. Read its block. Clear the lowest set bit. Seven becomes six. Read that block. Six becomes four. Read one more. Four becomes zero. Stop. Each step removed one set bit.
Two plus ten plus twenty four. That is thirty six, the total of seven hours. Each set bit of seven gave one block.
A query clears the lowest bit. An update adds the lowest bit. At most seventeen steps for a hundred thousand positions. Both walks stay short, even for huge trees.
A tree of blocks fixes both. Full breakdown in the lecture.