Segment tree explained in plain terms means understanding how to answer questions about a slice of an array without scanning the whole slice. The central question is how a program can find the sum, the minimum, or the maximum of a thousand elements as quickly as it finds a single one, and the answer is a tree that stores the answer for every interval in advance.
A segment tree is a binary tree built over an array, where each node stores the result of combining its interval, so a query over any range can be answered by combining a handful of precomputed nodes. Both queries and point updates run in logarithmic time, which makes the structure a favourite in competitive programming and in systems that answer range questions all day.
The idea is wonderfully practical: pay a little memory and a little build time up front, and every range question afterwards costs almost nothing. This article explains what a segment tree is, how it works, and where it is used.
Key takeaways
- A segment tree stores precomputed answers for intervals of an array.
- Range queries and point updates both run in logarithmic time.
- It works for any operation that can be combined, like sum or minimum.
- Building the tree costs linearithmic time and about four times the array size.
- Lazy propagation extends it to range updates as well.
What a segment tree is
A segment tree starts with an array and divides it in half, again and again, until every piece is a single element. The root node represents the whole array, its children represent the two halves, and the leaves hold the individual elements. Each internal node stores the combination of its children’s values, so the root always holds the answer for the entire array.
For a sum segment tree, each node holds the sum of its interval, and for a minimum segment tree, each node holds the smallest value in its interval. The structure does not care which operation you choose, as long as the operation can combine two neighbouring intervals into one, which mathematicians call associativity. Sums, minimums, maximums, and greatest common divisors all qualify.
The tree is usually stored in a flat array rather than as linked nodes, which keeps it cache-friendly and simple to implement. A tree over an array of a million elements needs about four million slots, a modest price for the speed it buys. Our guide to database indexing explains a different way systems precompute answers to make queries fast.
How a segment tree answers a query
Answering a range query means walking down the tree and collecting the nodes that exactly cover the wanted interval. A query for the sum of positions five to twelve, say, might be answered by combining three or four stored nodes, and no node is ever visited unless its interval lies entirely inside the query or entirely outside it. The number of nodes touched is logarithmic, so the query is fast however large the array grows.
The walk is easy to picture. Starting at the root, the query descends into any child whose interval overlaps the wanted range, and it stops descending the moment a node’s interval fits wholly inside the range, taking that node’s stored value as part of the answer. Partially overlapping nodes are split further, and non-overlapping nodes are skipped entirely.
Updating a single element is just as quick. The new value is written to its leaf, and the tree walks back up to the root, recomputing each ancestor from its two children. Only the nodes on one root-to-leaf path change, so an update touches logarithmically few nodes and the tree is correct again immediately.
Range updates, where every element in an interval changes at once, need one extra idea, called lazy propagation. Instead of pushing an update down to every leaf, the tree records the pending change at the highest node that fits inside the range and applies it to children only when a later query actually needs them. Lazy values are the reason a segment tree can handle range additions and range assignments without losing its logarithmic guarantee.

Segment tree versus other range structures
A segment tree is one of several ways to answer range questions, and each has a different sweet spot. A prefix sum array answers static range sums in constant time but cannot handle updates at all, while a Fenwick tree handles point updates and prefix sums with less memory but cannot answer arbitrary range minimums. The segment tree is the generalist of the group.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Segment tree | Fenwick tree | Prefix sums |
|---|---|---|---|
| Range sum query | Logarithmic | Logarithmic, via two prefixes | Constant |
| Range minimum query | Logarithmic | Not supported in general | Not supported |
| Point update | Logarithmic | Logarithmic | Linear, whole array rebuilds |
| Range update | Logarithmic, with lazy values | Logarithmic, for sums only | Not supported |
| Memory used | About four times the array | Same size as the array | Same size as the array |
| Best for | General range queries | Prefix sums with updates | Static arrays, read-only data |
The practical result is that a segment tree is the right default whenever the queries are varied or the operation is not a simple sum, while the lighter structures win when the problem is narrow enough for them.
Where segment trees are used
Competitive programming is the segment tree’s natural home, and most programmers meet it there first. Range sum queries, range minimum queries, and counting problems with updates appear constantly in contests, and the segment tree is the standard answer. It is one of those structures that repays the afternoon spent learning it many times over.
Geographic information systems use segment trees and their cousins to answer questions about regions. Finding all the points in a rectangle, the nearest facility to a location, or the total rainfall over a county all reduce to range queries over ordered data, and the same tree idea applies in two dimensions as well as one.
Databases use segment-tree-like structures for range indexes and for answering aggregate queries over ordered keys. A query asking for total sales in a date range is, at heart, a range sum over an ordered array, and the indexes that answer it quickly share the segment tree’s spirit of precomputing interval answers. Our guide to inverted indexes explains another precomputed structure that makes search fast.
Graphics and games use segment trees for visibility and collision queries. A renderer deciding which objects overlap a region of the screen, or a game checking which entities sit inside a blast radius, is asking a range question, and a tree over intervals answers it without scanning every object in the scene.
Financial systems use segment trees for time-series analysis, where the question is almost always about a window: the highest price in the last hour, the total volume today, or the average over a rolling week. With a segment tree over the price history, each of those answers in logarithmic time, however long the history grows.
Strengths and limits
The greatest strength of a segment tree is its generality. One structure handles sums, minimums, maximums, and any combinable operation, over any range, with updates, and the lazy variant handles range updates too. Very few data structures cover that much ground with one implementation.
It is also a strength that the performance is predictable. Every query and update is logarithmic, with no worst case lurking behind an unlucky input, which makes the segment tree easy to trust in production code as well as in contests.
The limits are real, though. A segment tree uses roughly four times the memory of the array it indexes, it is slower than a Fenwick tree for simple prefix sums, and it cannot answer questions that are not decomposable into intervals. Our guide to Memcached shows how caching can sometimes answer repeated range questions even more cheaply than any tree.
Segment tree checklist
- Use a segment tree when range queries mix with point updates.
- Check the operation combines neighbouring intervals before choosing it.
- Store the tree in a flat array for speed and simplicity.
- Add lazy propagation only when range updates are actually needed.
- Prefer a Fenwick tree for simple prefix sums with updates.
- Prefer prefix sums when the array never changes.
- Test queries on intervals of length one and the full array.
- Remember the tree uses about four times the array’s memory.
- Draw the tree on paper the first time you implement it.
Frequently asked questions
What is a segment tree in simple terms?
It is a tree built over an array that stores precomputed answers for every interval, so any range question can be answered by combining a few stored nodes.
How fast is a segment tree query?
Logarithmic in the size of the array, because a query touches only a handful of nodes on its way down the tree.
What is lazy propagation?
It is a technique that records a pending range update at a high node and pushes it to children only when a later query needs them.
Can a segment tree find a range minimum?
Yes, and that is one of its advantages over a Fenwick tree, which handles sums well but not arbitrary range minimums.
How much memory does a segment tree use?
About four times the size of the array when stored in a flat array, which is a modest price for logarithmic queries.
When should I not use a segment tree?
When the array never changes, use prefix sums, and when only prefix sums with updates are needed, use a Fenwick tree.
The bottom line
Segment tree explained in one line is this: precompute the answer for every interval, and any range question becomes a quick walk down a tree.
It is the generalist of range structures, and the reason a program can answer questions about a million-element array in a dozen steps.
This article is general information about how segment trees work, not advice about choosing a data structure for a particular system.







