Skip to the workspace
Pebellslearn by building
Prefix Sums & Range Queries  ›  Capstone Project

Capstone: Quarry's Analytics Engine

50 minRange bumps, totals and floors in one lazy segment tree, with every operation O(log n)

dsa / Capstone: Quarry's Analytics Engine

Explore each scene at your own pace.

01 · One node, two answers, one tag

One node, two answers, one tag

Capstone: Quarry's Analytics Engine

Definition
A lazy segment tree can store a sum and a minimum in every node together with one pending add tag, because both can be updated from a bump without visiting the children. Range additions, range sums and range minimums then each take O(log n).
In plain words
It is one chart that answers two questions at once: how much happened in total, and what was the lowest point. A single sticky note per node, add 3 to everything under here, is enough to keep both answers right.

In short: one lazy segment tree carries a sum and a minimum per node plus a single pending add, because both aggregates can be updated from a bump without looking at the children — the sum rises by d × width and the minimum rises by exactly d. Range bumps, range totals and range floors are all O(log n), the same three-case recursion as every segment tree query, with push and pull as the two small helpers that keep the tag honest.

A node holding sum and min plus a pending tag: before a +3 bump it shows sum 9, min 2, and after it shows sum 15, min 5, tag +3, with no children touched

Five lessons, five tools, each built because the one before it broke on some kind of change:

lesson handles breaks on
prefix row any range sum in O(1) any edit: O(n)
√n blocks edits and queries in O(√n) a busy dashboard
Fenwick tree point edits and sums in O(log n) min, which can't be subtracted
segment tree any combinable aggregate, with range edits via tags pages on disk
B-tree sorted keys on disk, a few reads per lookup (it's the disk layer)

Now Quarry's analytics engine needs all of the in-memory part at once. The operations team's list, word for word from the ticket:

  1. Backfill: "add d to every minute from l to r", for clock skew, late batches and corrections. These arrive in bulk and can be wide.
  2. Totals: "how many events between minute l and minute r?"
  3. Floors: "what was the lowest count between l and r?", which is how the on-call team spots an outage: a stretch where traffic fell to nothing.

Range updates plus min rules out everything except the lazy segment tree. The design question left is what each node has to carry.

One node, two answers, one tag

Each node stores the sum and the minimum of its range, plus one pending add. The key question for any lazy tree is: when everything under a node rises by d, can the node update its own answers without looking down?

  • the sum rises by d × width: every minute adds d;
  • the minimum rises by exactly d: adding the same amount to every value keeps the same value smallest.

Both are yes, so one tag serves both aggregates. That test is the one to apply whenever you design a lazy tree. An update that replaces every value by its remainder mod 10 fails it, for example: the new sum depends on each value separately, so no node can update its answer without looking all the way down.

What does this print?
values = [4, 9, 2, 7]
d = 3
bumped = [v + d for v in values]
print(sum(bumped) - sum(values), min(bumped) - min(values))

Here's the whole engine on four minutes. Each badge reads s for sum and m for minimum, and a + is a pending tag:

watch it move · tree

One tag updates both answers. The bump stops at nodes it covers, and the later query pushes the tag only where it has to go deeper.

The log is 4, 9, 2, 7. Each node holds its range's sum and minimum. Binary tree, 7 nodes, height 3.

1 / 9
Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the analytics engine
This piece — analytics(): Range bumps, sums and minima over the whole log in O(log n) each.
Scenario The ops team's ticket asks for three things at once: wide backfills, range totals, and range floors to spot outages. Quarry's analytics engine answers all three from one lazy segment tree, every operation O(log n) however wide the range.
analytics.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
analytics(values, ops) → listAnswers to the sum and min ops, with bumps applied in between.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.