Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 49s1 days 16 hours 1 minutes 49 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 49s1 days 16 hours 1 minutes 49 seconds.

Sign in to watch this lesson
Algorithms - Trees and Range Queries · 4:59
A Fenwick tree stores, at every index, the sum of a run of values as long as the index's lowest set bit, which i & -i extracts in one instruction. Queries strip bits off the index and updates add them, so both take O(log n) in about a dozen lines of code. We draw the staircase of bars, walk sum(13) and add(10), handle range updates with two trees, find where a running total first reaches a target with a single descent, and time it on a Mac against square-root blocks, a plain array and a naive range loop.