Overview
Range query structures efficiently answer queries and perform updates on array ranges. This module includes segment trees, Fenwick trees (Binary Indexed Trees), sparse tables, and more.Segment Tree
A versatile data structure for range queries and point updates.Initial array values
Identity element for the operation (0 for sum, INF for min, -INF for max)
Associative binary operation (sum, min, max, gcd, etc.)
Methods
Update element at index to value
Query the range [l, r). Returns the result of applying the operation.
- Build: O(n)
- Query: O(log n)
- Update: O(log n)
- Space: O(n)
Lazy Segment Tree
Segment tree with lazy propagation for efficient range updates.Apply update to range [l, r)
Query the range [l, r)
- Build: O(n)
- Range Query: O(log n)
- Range Update: O(log n)
- Space: O(n)
Fenwick Tree (Binary Indexed Tree)
Efficient data structure for prefix sums and point updates.Add value v to element at position x
Return prefix sum from 0 to x
- Build: O(n)
- Query: O(log n)
- Update: O(log n)
- Space: O(n)
Custom Fenwick with Structs
Sparse Table
Answers static range queries in O(1) time (no updates).- Build: O(n log n)
- Query: O(1)
- Space: O(n log n)
- Note: Only works for idempotent operations (min, max, gcd)
Sqrt Decomposition
Divides array into blocks of size √n for range queries.- Build: O(n)
- Query: O(√n)
- Update: O(1)
- Space: O(n)
2D Range Sum Queries
Efficiently computes sum of subarrays in 2D grids.Return sum of rectangle from (row1, col1) to (row2, col2) inclusive
- Build: O(m × n)
- Query: O(1)
- Space: O(m × n)
Persistent Segment Tree
Supports querying previous versions of the segment tree.- Update: O(log n) time, O(log n) space per version
- Query: O(log n)
Dynamic Segment Tree
Segment tree with dynamic node allocation for sparse arrays. Complexity:- Update: O(log n)
- Query: O(log n)
- Space: O(q log n) where q is number of updates
Available Structures
| Structure | File | Best For |
|---|---|---|
| Segment Tree | range_query_segment_tree.cpp | General range queries |
| Lazy Segment Tree (Full) | range_query_segment_tree_lazy_full.cpp | Range updates |
| Lazy Segment Tree (Compress) | range_query_segment_tree_lazy_compress.cpp | Range updates (compact) |
| Lazy Segment Tree (Template) | range_query_segment_tree_lazy_template.cpp | Customizable lazy prop |
| Fenwick Tree | range_query_fenwick_tree_std.cpp | Prefix sums |
| Sparse Table | range_query_sparse_table_std.cpp | Static RMQ |
| Sqrt Decomposition | range_query_sqrt_decomposition.cpp | Simple range queries |
| Persistent Segment Tree | range_query_persistent_segment_tree.cpp | Version control |
| Dynamic Segment Tree | range_query_dynamic_segment_tree.cpp | Sparse arrays |
| 2D Range Sum | range_query_sum_2d_immutable.cpp | 2D prefix sums |
| 1D Range Sum | range_query_sum_immutable.cpp | 1D prefix sums |
| Less Than Query | range_query_less_than_segment_tree_lazy.cpp | Count < x queries |
| Sum + Lower Bound | range_query_sum_lower_bound_segment_tree_lazy.cpp | Binary search on segtree |
See Also
Data Structures
Other advanced data structures
Techniques
Algorithmic techniques and patterns