Tip 1: Use stable partition When building, maintain relative order of elements going to each child.
This guarantees position mapping works correctly. Tip 2: Handle empty ranges Always check if before proceeding.
This happens when all elements went to the other child. Tip 3: Precompute prefix sums Build prefix sum arrays for each bitvector during construction.
Don't compute rank on-the-fly. Tip 4: Test on small examples Trace through a 4-element example by hand. Verify your rank calculations match expected positions. Time: . Space: .