Capstone: Quarry's Analytics Engine
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.

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:
- Backfill: "add
dto every minute fromltor", for clock skew, late batches and corrections. These arrive in bulk and can be wide. - Totals: "how many events between minute
land minuter?" - Floors: "what was the lowest count between
landr?", 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 addsd; - 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.
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:
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.