Segment Tree Last Updated : 31 Aug, 2025 Comments Improve Suggest changes Like Article Like Report Segment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree is built recursively by dividing the array into segments until each segment represents a single element. This structure enables fast query and update operations with a time complexity of O(log n)The following diagram shows a segment tree built for an array [1, 4, 5, 5, 9, 10, 10, 12, 19, 31, 41] of size 11. The example tree is built for range sum queries. Every node stores sum of a range. The root nodes stores sum of the whole array and leaf nodes store sums of single elements in the array. Please refer Segment Tree Introduction article for details about construction and query.arr[] = [1, 4, 5, 5, 9, 10, 10, 12, 19, 31, 41]Basics of Segment Tree:Introduction Persistent Segment TreeEfficient implementationIterative Segment TreeRange Sum and Update in Array Dynamic Segment TreesApplications, Advantages and DisadvantagesLazy Propagation:Lazy Propagation Lazy Propagation | Set 2Flipping Sign ProblemRange Queries:Queries for any non-repeating elementRange Minimum QueryQuerying maximum number of divisors Min-Max Range Queries in ArrayRange LCM QueriesNumber of primes in a subarrayLargest Sum Contiguous SubarrayLongest Correct Bracket SubsequenceMaximum Occurrence in a Given RangeMaximum product pair in range with updatesChessboard PiecesNumber of subsets equal to a given StringMinimum distance between two ZerosQueries to evaluate the given equation in a range [L, R]Maximum weight in given price range for Q queriesSome interesting problem on Segment Tree:Longest Increasing Subsequences (LIS) Longest subarray consisting of same elements by at most K decrementsGenerate original permutation from given array of inversionsMaximum of all subarrays of size KBuild a segment tree for N-ary rooted tree Count number of increasing sub-sequences : O(NlogN)Calculate the Sum of GCD over all subarraysCartesian tree from inorder traversalLIS using Segment TreeReconstructing Segment TreeApplications of Segment Tree:Interval scheduling: Segment trees can be used to efficiently schedule non-overlapping intervals, such as scheduling appointments or allocating resources.Range-based statistics: Segment trees can be used to compute range-based statistics such as variance, standard deviation, and percentiles.Image processing: Segment trees are used in image processing algorithms to divide an image into segments based on color, texture, or other attributes. Introduction of Segment Tree Visit Course Introduction of Segment Tree Constructing the Segment Tree Comment More info H harendrakumar123 Follow Improve Article Tags : Tree DSA Huawei Segment-Tree Uber TCS-coding-questions +2 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like