Segment Trees & Lazy Propagation
In short: a segment tree stores one aggregate per node, built bottom-up
from its two children, so a range query visits only nodes that are wholly
inside the range (total cover), wholly outside (disjoint), or split
between — about O(log n) nodes either way. It never subtracts, so it handles
min, max, gcd, anything combinable from two halves, unlike a Fenwick tree.
Lazy propagation extends it to range updates: a totally-covered node gets
its value and a pending tag updated without visiting its children, and the
tag is pushed down only right before something needs to read below it.

Quarry's latency panel doesn't want a total. It wants the fastest and slowest response between two minutes, while new measurements keep arriving. Every structure so far fails on that:
- the prefix row and the Fenwick tree both answer a range as prefix minus
prefix, and
mincan't be subtracted; - square-root blocks can do
min, but only at O(√n).
A segment tree handles any aggregate you can combine from two halves: sum, min, max, gcd, "longest run of healthy minutes", in O(log n) per query and per edit. It needs no subtraction, only a way to merge a left answer with a right answer. Later in the lesson it learns to take range updates too, using lazy propagation.
The shape
The root owns the whole log, minutes 0..n−1. Each node splits its range in
half and hands one half to each child, down to leaves that own one minute each.
Every node stores the aggregate of its range, computed from its two children.
There are about 2n nodes and the height is about log₂ n.
A query for l..r starts at the root. At every node it meets, exactly one of
three things is true:
- total cover: the node's range lies inside
l..r. Use its stored value and stop. - disjoint: no overlap at all. Contribute nothing and stop.
- partial: some overlap. Ask both children and combine their answers.
Here's the minimum over minutes 1..6, with the three outcomes in three colours. Badges show each node's stored minimum:
A range query visits a few nodes per level. Green nodes are wholly inside the range and answer for everything beneath them. Grey ones don't overlap, so they're skipped with everything beneath them. Only the partly covered ones go deeper.
The log is 5, 2, 7, 1, 3, 4, 6, 2. Every node's badge is the minimum of its range, built bottom-up from its children. Binary tree, 15 nodes, height 4.
Why only a few nodes per level? The range is contiguous, so on any level at most two nodes are partial: the ones containing its left edge and its right edge. Every other node there is wholly in or wholly out, and stops. So a query visits at most about four nodes per level, which is O(log n) in all.