Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 22 min to read

Play it as a journeyTutor comes with a plan

A total for every half: segment trees and lazy updates

Rotations and Range Trees
Rotations and Range Trees · Open board

The last tree on this board is the one this lesson builds: every node keeps the total of a stretch, so a question about a stretch reads a few nodes and nothing more.

Before you start. You need the segment tree sketch from Trees, Worked (rotations, B-trees, range trees): a tree where each node holds the total of a stretch of a list, and the root holds the total of everything. The running total (prefix sums and difference arrays) gave you the prefix sum, a list of totals so far, where any stretch is one subtraction. Trees (BST, balancing, heaps, tries) gave you children and recursion on a tree. Complexity & Big-O gave you O(log n), which means the work grows by one step each time the list doubles.

Lesson 84 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 1994, Auckland: a table of counts that changes after every symbol
  • Why the two old answers are both slow
  • The segment tree: every node holds the total of a stretch
  • The range query: cover the stretch with a few nodes
  • The point update: fix only the nodes above one number
  • The range update: why changing a whole stretch is a new problem
  • Lazy propagation: leave a note, and pay it only when someone looks inside
  • Storage and the four-times rule
  • LeetCode 307, Range Sum Query, Mutable (free, Medium)
  • The lazy tree: range add, range sum
  • LeetCode 2569, Handling Sum Queries After Update (free, Hard)
  • Edge cases and trade-offs

Terms it explains: node, segment tree, range query, point update, range update, Lazy propagation, tag, Common mistakes.

Review DSA