0
0
Data Structures Theoryknowledge~6 mins

Segment trees for range queries in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a long list of numbers and you want to quickly find the sum or minimum of any part of that list. Doing this by checking each number every time can be very slow. Segment trees solve this problem by organizing the data so you can get answers fast, even for big lists.
Explanation
Structure of a Segment Tree
A segment tree is a binary tree where each node represents a segment or range of the original list. The root covers the entire list, and each child node covers a smaller part. Leaf nodes represent single elements. This structure allows the tree to store information about all possible segments efficiently.
Each node in a segment tree stores information about a specific range of the list.
Building the Segment Tree
To build the tree, start from the entire list and split it into two halves recursively until each segment is a single element. At each node, combine the information from its children, like summing their values or taking the minimum. This process takes about twice the size of the list in time.
Building the tree involves recursively splitting the list and combining child results.
Querying the Segment Tree
When you want to find the sum or minimum in a range, you start at the root and check if the node's segment is fully inside, outside, or partially overlapping the query range. If fully inside, return the stored value. If outside, ignore it. If partial, check both children. This lets you get answers quickly without checking every element.
Queries use the tree structure to skip irrelevant parts and focus only on needed segments.
Updating the Segment Tree
If a value in the list changes, you update the corresponding leaf node and then update its ancestors to reflect the change. This keeps the tree accurate for future queries. Updates take logarithmic time because you only change nodes along one path from leaf to root.
Updates affect only a path from a leaf to the root, making them efficient.
Real World Analogy

Imagine a library with many books arranged on shelves. To quickly find how many books are in any group of shelves, the librarian keeps a summary for each shelf and for groups of shelves combined. When a book is added or removed, only the summaries for affected shelves and their groups are updated.

Structure of a Segment Tree → Each node is like a summary for a group of shelves in the library.
Building the Segment Tree → The librarian creates summaries by combining smaller shelf groups into bigger ones.
Querying the Segment Tree → To find the number of books in some shelves, the librarian checks only the summaries that cover those shelves.
Updating the Segment Tree → When a book is added or removed, the librarian updates summaries only for the affected shelves and their groups.
Diagram
Diagram
          [0..7]
         /       \
     [0..3]       [4..7]
     /    \       /    \
 [0..1]  [2..3] [4..5]  [6..7]
  /  \    /  \  /  \    /  \
[0] [1] [2] [3][4] [5] [6] [7]
This diagram shows a segment tree where each node covers a range of the list indices, splitting into smaller segments down to single elements.
Key Facts
Segment Tree NodeA node that stores information about a specific range of the list.
Leaf NodeA node representing a single element in the list.
Range QueryAn operation that asks for information (like sum or minimum) over a segment of the list.
Update OperationChanging a value in the list and updating the tree to keep data accurate.
Query Time ComplexitySegment trees answer range queries in about O(log n) time.
Update Time ComplexitySegment trees update values in about O(log n) time.
Code Example
Data Structures Theory
class SegmentTree:
    def __init__(self, data):
        self.n = len(data)
        self.size = 1
        while self.size < self.n:
            self.size <<= 1
        self.tree = [0] * (2 * self.size)
        for i in range(self.n):
            self.tree[self.size + i] = data[i]
        for i in range(self.size - 1, 0, -1):
            self.tree[i] = self.tree[2 * i] + self.tree[2 * i + 1]

    def update(self, index, value):
        pos = self.size + index
        self.tree[pos] = value
        pos //= 2
        while pos > 0:
            self.tree[pos] = self.tree[2 * pos] + self.tree[2 * pos + 1]
            pos //= 2

    def query(self, left, right):  # inclusive left, exclusive right
        result = 0
        left += self.size
        right += self.size
        while left < right:
            if left % 2 == 1:
                result += self.tree[left]
                left += 1
            if right % 2 == 1:
                right -= 1
                result += self.tree[right]
            left //= 2
            right //= 2
        return result

# Example usage
data = [2, 1, 5, 3, 4]
st = SegmentTree(data)
print(st.query(1, 4))  # sum from index 1 to 3
st.update(2, 2)         # update index 2 to value 2
print(st.query(1, 4))
OutputSuccess
Common Confusions
Thinking segment trees store all elements separately without combining.
Thinking segment trees store all elements separately without combining. Segment trees store combined information for ranges, not just individual elements, to answer queries efficiently.
Believing queries always check every element in the range.
Believing queries always check every element in the range. Queries skip irrelevant parts by using stored summaries, so they do not check every element.
Assuming updates require rebuilding the entire tree.
Assuming updates require rebuilding the entire tree. Only nodes along the path from the changed element to the root are updated, making updates fast.
Summary
Segment trees organize data in a tree where each node covers a range, enabling fast range queries.
Building and updating the tree take about O(n) and O(log n) time respectively, making it efficient for large lists.
Queries use the tree to quickly combine results from relevant segments without checking every element.