Open In App

Segment Trees for Competitive Programming

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Segment Tree is one of the most important data structures used for solving problems based on range queries and updates. Problems based on Segment Trees are very common in Programming Contests. This article covers all the necessary concepts required to have a clear understanding of Segment Trees.

What is a Segment Tree?

A Segment Tree is a data structure that stores information about a range of elements in its nodes. It also allows users to modify the array and perform range queries in smaller complexity. For example, we can perform a range summation of an array between the range L to R while also modifying the array from range L to R all in log(N) time complexity.

Structure of the Segment Tree:

The segment tree works on the principle of divide and conquer

  • At each level, we divide the array segments into two parts. If the given array had [0, . . ., N-1] elements in it then the two parts of the array will be [0, . . ., N/2-1] and [N/2, . . ., N-1]
  • We will then recursively go on until the lower and upper bounds of the range become equal. 
  • The structure of the segment tree looks like a binary tree.

The segment tree is generally represented using an array where the first value stores the value for the total array range and the child of the node at the ith index are at (2*i + 1) and (2*i + 2).

Construction Of Segment Tree:

There are two common approaches to construct a Segment Tree:

  1. Recursive method
  2. Iterative method

1. Construction Of Segment Tree using Recursive Approach:

There are two important points to be noted while constructing the segment tree:

  • Choosing what value to be stored in the nodes according to the problem definition
  • What should the merge operation do

If the problem definition states that we need to calculate the sum over ranges, then the value at nodes should store the sum of values over the ranges.

  • The child node values are merged back into the parent node to hold the value for that particular range, [i.e., the range covered by all the nodes of its subtree]. 
  • In the end, leaf nodes store information about a single element. All the leaf nodes store the array based on which the segment tree is built. 

Following are the steps for constructing a segment tree:

  1. Start from the leaves of the tree
  2. Recursively build the parents from the merge operation

2. Construction Of Segment Tree using Iterative Approach:

  • The iterative version of the segment tree basically uses the fact, that for an index i, left child = 2 * i and right child = 2 * i + 1 in the tree.
  • The parent for an index i in the segment tree array can be found by parent = i / 2. Thus, we can easily travel up and down through the levels of the tree one by one.
  • At first, we compute the maximum in the ranges while constructing the tree starting from the leaf nodes and climbing up through the levels one by one. We use the same concept while processing the queries for finding the maximum in a range.

Since there are (log n) levels in the worst case, so querying takes log n time. For update of a particular index to a given value we start updating the segment tree starting from the leaf nodes and update all those nodes which are affected by the update of the current node by gradually moving up through the levels at every iteration. Updates also takes log n time because there we have to update all the levels starting from the leaf node where we update the exact value at the exact index given by the user.

What is Dynamic Segment Tree?

A dynamic Segment tree is the one where the values of the array is not fixed i.e it has an extra operation where the value of the i’th index of the array can be changed. As per this change the segment tree performs dynamic changes to give the right result after the changes have been made.

It basically performs two operations:

  1. P V: Put the value V at position P.
  2. L R: Query for the values from L to R.

Querying On Segment Tree:

The below table lists the query type and its time complexity for a segment tree:

Query Type

Description

Time Complexity

Construction

Build a segment tree from an input array of Size n.

O(n)

Point Update

Update the value of an element at a specific index.

O(log n) – Height of the segment tree.

Range Query

Retrieve information about a specific range in the array (e.g., sum, min, max).

O(log n) – Height of the segment tree.

Range Update

Update all elements in a specific range of the array.

O(log n) – Height of the segment tree.

Lazy Propagation

Optimize range updates by deferring updates until necessary.

O(log n) for query, O(log n) for update.

Applications of Segment Trees in Competitive Programming:

In CP, there are many problems which requires querying range data of an array in fast time i.e. about O(log(n)), this is where segment tree comes handy, let us see the most common applications of segment tree during a CP contest:

1. Range Sum Queries (RSQ) with Point Updates:

Problem Requirement: Given Q number of Queries and an array of size N, the task is to process the queries of the form:

  1. L R: return the sum of the array values from index L to R
  2. i X: Update the value at index i to X

Brute force solution: For each query we can traverse form index L to R sum up those values in O(Q* N) total time

Segment Tree Solution: With a segment tree each query of the form 1 L R can be processed in O(Log N) hence giving a time complexity of O(Q * LogN)

2. Range Sum Queries with Range Updates:

Problem Requirement: Given Q number of Queries and an array of size N, your task is to process the queries of the form:

  1. L R: return the sum of the array values from index L to R
  2. L R X: Update the value from index L to R by addingX/subtractingX/square Rooting/etc to them

Brute force solution: For each query we can traverse form index L to R sum up those values in O(Q* N) total time.

Segment Tree Solution: With a segment tree each query can be processed in O(Log N) hence giving a time complexity of O(Q * LogN)

3. Range Minimum/Maximum Queries (RMQ):

Problem Requirement: Many problems require the user to quickly retrieve the minimum or the maximum value in a range i.e. for Q queries you have to get min/max value for range L to R of an array.

Brute force solution: For each query we can traverse form index L to R to find min/max in O(Q* N) total time

Segment Tree Solution: A segment tree data structure can process each query in O(Log N) hence giving a total time complexity of O(Q * LogN)

4. Range Minimum/Maximum Query with Node Update:

Problem Requirement: Given an array of size N, you have to perform Q queries of the following form:

  1. L R: Return the min/max value in the range L to R.
  2. i X: update the value at index i to X

Brute force solution: For each query of type 1 can be performed in O(N) time while query of type 2 can be performed in O(1) time, giving an overall complexity of O(Q * N)

Segment Tree Solution: A segment tree can be used to perform both the queries in O(log N) time giving an overall complexity of O(Q* LogN)

Interval Intersection and Union:

Interval intersection and union operations on a segment tree with updates involve maintaining information about intervals and efficiently performing intersection and union operations on them, while also supporting updates to the intervals. It performs 3 operations:

  1. Interval Intersection:
    • To find the intersection of two intervals, you need to consider the overlapping portion of the intervals.
    • For a segment tree, this involves traversing the tree and finding the common segments in the nodes
  2. Interval Union:
    • To find the union of two intervals, you need to combine the intervals while ensuring that the union covers both intervals.
    • In a segment tree, this involves updating the tree to represent the union of intervals.
  3. Updates:
    • Updates in a segment tree refer to modifying the intervals stored in the tree.
    • This can involve changing the value of a specific element or extending/shrinking an interval.

Advanced Topics and Variations for Segment Tree:

Persistent Segment Trees

Two-Dimensional Segment Tree

Alternative Data Structures for Segment Tree:

Sparse Table

Fenwick Tree

Practice Problems on Segment Tree:

Problem

Link

Range Minimum Query

Solve

GCDs of given index ranges in an Array

Solve

Sum of Query II

Solve

Queries for elements greater than K in the given index range

Solve

Queries for number of distinct elements in a subarray

Solve

Sum of maximum of all subarrays

Solve

Count elements which divide all numbers in range L-R

Solve

Kth smallest element in a subarray

Solve

Nitika and her queries

Solve

Element left after performing alternate OR & XOR operation

Solve

Increasing Decreasing Update Queries

Solve



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads