Answer range queries with Fenwick and segment trees
8 steps · about 2 h 50 min · Intermediate
- Start with prefix sums for static ranges (article, Wikipedia, 10 min)
O(1) range sums when the array never changes.
- Know the range-minimum problem (article, Wikipedia, 10 min)
Frames the query types and the classic solutions.
- Answer static minimum queries with a sparse table (tutorial, cp-algorithms, 20 min)
O(1) idempotent queries after O(n log n) preprocessing.
- Understand the Fenwick tree layout (article, Wikipedia, 15 min)
How lowest-set-bit jumps give log-time prefix sums and updates.
- Implement a Fenwick tree (tutorial, cp-algorithms, 25 min)
Code for point update, range query and range update variants.
- Step through Fenwick tree updates (interactive, VisuAlgo, 15 min)
Animates which nodes each query and update touches.
- Implement a segment tree with lazy propagation (tutorial, cp-algorithms, 45 min)
From the basic tree to range updates and more general merges.
- Practise point-update range-sum problems (tutorial, USACO Guide, 30 min)
Guided problems that use both structures, with solutions.