How to implement segment trees with lazy propagation?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview of Segment Trees with Lazy Propagation
Segment Trees are a powerful data structure utilized in scenarios where we need to efficiently query and update array intervals. They support operations like finding a sum, minimum, or maximum over a subrange, which can be updated in logarithmic time.
Lazy Propagation is an optimization technique applied to Segment Trees, allowing us to defer and batch updates to minimize redundant work and enhance performance significantly. This is particularly useful for range updates, where the entire subrange of the segment must be updated.
In this article, we'll delve into the implementation of Segment Trees with Lazy Propagation, including technical details, examples, and key concepts.
Key Concepts
Segment Tree
A Segment Tree is a binary tree where each node represents an interval or segment of an array. Each leaf of the tree represents a single element of the array, and each internal node represents the aggregation of a segment of the array, like a sum or a minimum value.
- Construction: Building the segment tree takes time.
- Query: Querying the segment tree for an interval can be done in time.
- Update: Updating a segment tree in general also takes time.
Lazy Propagation
Lazy Propagation is used to delay updates to segments of the tree to optimize the update procedure. This is achieved by marking nodes to be updated later and only performing necessary updates on nodes upon query or further propagation.
- Reducing unnecessary updates by marking nodes as lazy.
- Efficiently handling frequent updates on large segments.
- Helping in reducing the overall time complexity for segment tree operations involving multiple update queries.
Building a Segment Tree with Lazy Propagation
Data Structures
- Tree Array: It stores the segment values (e.g., sums, min, max).
- Lazy Array: It stores updates that need to be propagated.
Example
Consider an array arr
with n
elements. We want to perform range sum updates and queries efficiently:
- Initialize with the given array.
- Build the segment tree recursively.
- Update Range: Adjust values in a given interval through lazy marking.
- Query Range: Calculate the sum over a range, applying any pending updates lazily.
- Range Updates: Efficient when there are many modifications required over large segments.
- Dynamic Arrays: Useful when arrays change frequently and need fast recalculation.
Related reading
- How to implement strlen as fast as possible
- How to implement the Bayesian average algorithm for a binary rating system
- How to improve the performance of Leetcode 4sum-ii challenge
- How to increment all values in an array interval by a given amount
- How to implement single-consumer-multi-queue model for rabbitMQ
- How to improve accuracy of decision tree in matlab
- How to intersect two sorted integer arrays without duplicates?
- How to iterate over n dimensions?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.