Segment tree
Дерево отрезков
This data structure stores precomputed results for parts of an array. It can quickly find the sum, minimum, or maximum over a range, even when individual elements change between queries.
When a segment tree helps
Suppose you track hourly usage and sometimes correct an old reading. You want the highest reading between any two hours. Scanning the requested range is simple, but a wide query can inspect nearly the entire array. A segment tree stores the maximum for selected blocks of hours, so a query can combine a few cached results instead of rereading every value. A correction affects only the blocks that contain the changed reading.
What the tree stores
The root covers the whole array. Each internal node splits its range into two roughly equal halves, and each leaf covers one element. To build a tree for range maximums, start with the array values at the leaves and store the larger of the two child values at each parent. With a constant-time combine operation, building takes O(n) time and the tree uses O(n) space for an array of length n. The nodes can live in an ordinary array rather than in separate objects connected by pointers.
A query starts at the root. If a node's range is entirely inside the query, use its stored value. If it's entirely outside, skip it. Otherwise, visit its children and combine their answers. Only nodes that straddle a query boundary need further exploration, so at most two nodes per level require that extra work. With O(log n) levels, a query takes O(log n) time. A point update also takes O(log n): change one leaf and recompute the nodes on its path back to the root.
Walk through a query
Take [8, 3, 5, 2, 6, 4, 1, 7] and query the maximum over [2, 7). The left endpoint is included and the right endpoint is excluded. The tree covers the query with [2, 4), [4, 6), and [6, 7), whose stored maximums are 5, 6, and 1. The answer is 6; the value 7 at index 7 is outside the query. If the reading at index 3 becomes 9, the tree updates that leaf and its ancestors. The same query now returns 9.
Choosing the operation and the data structure
The combine operation must be associative: changing the grouping must not change the result. Sum, min, and max all work. An average on its own does not: averaging two child averages gives the wrong weight to children of different sizes. Store a sum and a count instead, then divide after the query.
For sums on an array that never changes, prefix sums give O(1) queries after O(n) preprocessing, with less machinery. At the other end, if a single update must add a value to an entire range, a basic point-update tree would have to touch each affected leaf. A variant called lazy propagation records a pending update at a fully covered node and passes it down only when a later operation needs its children.
In a coding interview
A common prompt asks for range queries interleaved with point updates. State your index convention before coding and explain what a non-overlapping branch returns: 0 for sum, for example, but −∞ for max. Test with an all-negative array to catch an incorrect default of 0, and include a query that crosses the split between the root's children.