~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Fenwick tree (BIT)
Point update + prefix sum in O(log n) with i & -i.
Notes
Recognise it when: you need point updates and prefix or range sums, both in O(log n). Also for counting inversions and "smaller elements after self" over compressed values.
def add(i, v): # 1-indexed
while i <= n: t[i] += v; i += i & -i
def prefix(i):
s = 0
while i > 0: s += t[i]; i -= i & -i
return s
A range sum is prefix(r) - prefix(l - 1).
Gotchas: it must be 1-indexed. It only handles invertible operations (sum, xor), not min or max.
5 problems
Advanced & competitive
Range queries Fenwick trees, segment trees, sparse tables.
Fenwick tree (BIT) guide
- Basics: which cells a Fenwick tree touches basics easy
- Deli counter: people ahead of you py · c++ · java easy
- Range sums with point updates py · c++ · java easy
- Count smaller elements to the right py · c++ · java medium
- Weighted random sampler with insert and remove Citadel hard