Answer range queries with Fenwick and segment trees

8 steps · about 2 h 50 min · Intermediate

  1. Start with prefix sums for static ranges (article, Wikipedia, 10 min)

    O(1) range sums when the array never changes.

  2. Know the range-minimum problem (article, Wikipedia, 10 min)

    Frames the query types and the classic solutions.

  3. Answer static minimum queries with a sparse table (tutorial, cp-algorithms, 20 min)

    O(1) idempotent queries after O(n log n) preprocessing.

  4. Understand the Fenwick tree layout (article, Wikipedia, 15 min)

    How lowest-set-bit jumps give log-time prefix sums and updates.

  5. Implement a Fenwick tree (tutorial, cp-algorithms, 25 min)

    Code for point update, range query and range update variants.

  6. Step through Fenwick tree updates (interactive, VisuAlgo, 15 min)

    Animates which nodes each query and update touches.

  7. Implement a segment tree with lazy propagation (tutorial, cp-algorithms, 45 min)

    From the basic tree to range updates and more general merges.

  8. Practise point-update range-sum problems (tutorial, USACO Guide, 30 min)

    Guided problems that use both structures, with solutions.