Lowest-Set-Bit Combinatorics for Fenwick Trees #
This module develops the binary arithmetic and interval combinatorics underlying Fenwick trees
(binary indexed trees, [Fenwick1994], "PRINCIPLES", pp. 328–329, and "OPERATIONS AND CODE TO
HANDLE THE STRUCTURE", pp. 329–330).
For a 1-based index m ≥ 1, lowbit m is the value 2 ^ k of the least significant 1-bit of
m (conventionally written m & -m in two's-complement arithmetic), and node m aggregates the
half-open 0-based index interval [m - lowbit m, m):
- Downward query chains (
downChain): steppingm ↦ m - lowbit mat least doubles the trailing power-of-two factor (two_mul_lowbit_le_lowbit_sub), terminating in at most⌊log₂ m⌋ + 1steps (length_downChain_le_log) and partitioning[0, m). - Upward update chains (
upChain): steppingm ↦ m + lowbit mup tonlikewise at least doubleslowbit(two_mul_lowbit_le_lowbit_add), terminating in at most⌊log₂ n⌋ + 1steps (length_upChain_le_log) and visiting precisely those nodesidx ≤ nwhose interval[idx - lowbit idx, idx)containsm - 1(mem_upChain_iff). - Immediate-children partition (
fenwickChildren): for everyidx ≥ 1, the singleton{idx - 1}together with the intervals[c - lowbit c, c)of immediate childrenc + lowbit c = idxpairwise-disjointly partition[idx - lowbit idx, idx)(insert_biUnion_fenwickChildren), justifying the standard BIT folklore immediate-parent construction (nparent-propagation steps).
Main definitions #
Cslib.DataStructure.FenwickTree.lowbit: value of the lowest set bit ofn(0at0).Cslib.DataStructure.FenwickTree.downChain: downward query indicesm, m - lowbit m, ….Cslib.DataStructure.FenwickTree.upChain: upward update indicesm, m + lowbit m, … ≤ n.Cslib.DataStructure.FenwickTree.fenwickChildren: immediate Fenwick childrenc < idxwithc + lowbit c = idx.
Main results #
Cslib.DataStructure.FenwickTree.exists_odd_mul_lowbit: odd-quotient power-of-two factorizationn = (2 * q + 1) * lowbit nwithlowbit n = 2 ^ k.Cslib.DataStructure.FenwickTree.two_mul_lowbit_le_lowbit_subandtwo_mul_lowbit_le_lowbit_add:lowbitat least doubles at every downward or upward step.Cslib.DataStructure.FenwickTree.fenwick_parent_bridge: for0 < m < idx,idx - lowbit idx < m ↔ idx - lowbit idx < m + lowbit m ∧ m + lowbit m ≤ idx.Cslib.DataStructure.FenwickTree.length_downChain_le_log,length_upChain_le_log,length_downChain_le_clog,length_upChain_le_clog: logarithmic chain-length bounds.Cslib.DataStructure.FenwickTree.mem_upChain_iff: exact ancestor characterization.Cslib.DataStructure.FenwickTree.pairwiseDisjoint_fenwickChildrenandinsert_biUnion_fenwickChildren: disjoint child-interval partition for linear-time construction.
References #
Value 2 ^ k of the least significant 1-bit of n (0 when n = 0), defined by binary
recursion on ℕ (modeling two's-complement n & -n on fixed-width words).
Parent-interval bridge #
Downward and upward traversal chains #
Intervals [c - lowbit c, c) of distinct immediate Fenwick children of idx are disjoint.
{idx - 1} is disjoint from all immediate-child intervals [c - lowbit c, c).
For idx > 0, {idx - 1} and the intervals [c - lowbit c, c) of c ∈ fenwickChildren idx
partition [idx - lowbit idx, idx).