The document discusses segment trees and their use in efficiently solving range sum queries on an array. Segment trees allow querying the sum of elements between any indices l and r in O(log n) time by pre-processing the array to build a tree structure where each node represents the sum of a sub-range of the array. The tree is built bottom-up by dividing the array into halves recursively and storing the sum at each node. This allows querying a range by traversing log n levels of the tree to find the relevant node sums.
Here are my slides for my preparation class for possible students for the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Here are my slides for my preparation class for possible students for the Master in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
Course 4 for the Algorithm Design and Complexity course at the Faculty of Engineering in Foreign Languages - Politehnica University of Bucharest, Romania
Abstract: This workshop teaches basic algorithms in whiteboarding interviews. All the code examples are in Python and the course has dual purpose teaching basic Python programming.
Course: Intro to Computer Science (Malmö Högskola)
Some classic data structures within computer science modeling: stacks, queues, vectors, linked-lists, graphs
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
Course 4 for the Algorithm Design and Complexity course at the Faculty of Engineering in Foreign Languages - Politehnica University of Bucharest, Romania
Abstract: This workshop teaches basic algorithms in whiteboarding interviews. All the code examples are in Python and the course has dual purpose teaching basic Python programming.
Course: Intro to Computer Science (Malmö Högskola)
Some classic data structures within computer science modeling: stacks, queues, vectors, linked-lists, graphs
1. Segment Trees
Problems
Segment Trees
League of Programmers
ACA, IIT Kanpur
League of Programmers Segment Trees
2. Segment Trees
Problems
Outline
1 Segment Trees
2 Problems
League of Programmers Segment Trees
3. Segment Trees
Problems
A Simple Problem
Problem Statement
We have an array a[0 . . . n-1].
League of Programmers Segment Trees
4. Segment Trees
Problems
A Simple Problem
Problem Statement
We have an array a[0 . . . n-1].
League of Programmers Segment Trees
5. Segment Trees
Problems
A Simple Problem
Problem Statement
We have an array a[0 . . . n-1].
We should be able to
1 Find the sum of elements l to r
League of Programmers Segment Trees
6. Segment Trees
Problems
A Simple Problem
Problem Statement
We have an array a[0 . . . n-1].
We should be able to
1 Find the sum of elements l to r
2 Change in the value of a specied element of the array a[i]=x
League of Programmers Segment Trees
7. Segment Trees
Problems
A Simple Problem
Possible Solutions
League of Programmers Segment Trees
8. Segment Trees
Problems
A Simple Problem
Possible Solutions
Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
League of Programmers Segment Trees
9. Segment Trees
Problems
A Simple Problem
Possible Solutions
Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
League of Programmers Segment Trees
10. Segment Trees
Problems
A Simple Problem
Possible Solutions
Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates
League of Programmers Segment Trees
11. Segment Trees
Problems
A Simple Problem
Possible Solutions
Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates
What if the number of query and updates are equal?
League of Programmers Segment Trees
12. Segment Trees
Problems
A Simple Problem
Possible Solutions
Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates
What if the number of query and updates are equal?
Can we perform both the operations in O(log n) time once
given the array?
League of Programmers Segment Trees
13. Segment Trees
Problems
Segment Trees
League of Programmers Segment Trees
14. Segment Trees
Problems
Segment Trees
Representation of the tree
League of Programmers Segment Trees
15. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
League of Programmers Segment Trees
16. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
League of Programmers Segment Trees
17. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
Number each node in the tree level by level
League of Programmers Segment Trees
18. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
Number each node in the tree level by level
Observe that for each node the left child is 2*i and right child
is 2*i+1
League of Programmers Segment Trees
19. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
Number each node in the tree level by level
Observe that for each node the left child is 2*i and right child
is 2*i+1
For each node the parent is i/2
League of Programmers Segment Trees
20. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
Number each node in the tree level by level
Observe that for each node the left child is 2*i and right child
is 2*i+1
For each node the parent is i/2
Just use an array to represent the tree, operate on indices to
access parents and children
League of Programmers Segment Trees
21. Segment Trees
Problems
Segment Trees
Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents some merging of the leaf nodes
Number each node in the tree level by level
Observe that for each node the left child is 2*i and right child
is 2*i+1
For each node the parent is i/2
Just use an array to represent the tree, operate on indices to
access parents and children
Note: An important feature of the tree segments is that they
use linear memory: standard tree segments requires about 4n
memory elements to work on an array of size n
League of Programmers Segment Trees
22. Segment Trees
Problems
Solution
League of Programmers Segment Trees
23. Segment Trees
Problems
Solution
We start with a segment [0 . . . n-1]. and every time we divide
the current period of two (if it has not yet become a segment
of length), and then calling the same procedure on both
halves, and for each such segment we store the sum on it.
League of Programmers Segment Trees
24. Segment Trees
Problems
Solution
We start with a segment [0 . . . n-1]. and every time we divide
the current period of two (if it has not yet become a segment
of length), and then calling the same procedure on both
halves, and for each such segment we store the sum on it.
In other words, we calculate and remember somewhere sum of
the elements of the array, ie segment a[0 . . . n-1]. Also
calculate the amount of the two halves of the array:
a[0 . . . n/2] and a[n/2 + 1 . . . n − 1]. Each of the two halves, in
turn, divide in half and count the amount to keep them, then
divide in half again, and so on until it reaches the current
segment length 1
League of Programmers Segment Trees
25. Segment Trees
Problems
Solution
We start with a segment [0 . . . n-1]. and every time we divide
the current period of two (if it has not yet become a segment
of length), and then calling the same procedure on both
halves, and for each such segment we store the sum on it.
In other words, we calculate and remember somewhere sum of
the elements of the array, ie segment a[0 . . . n-1]. Also
calculate the amount of the two halves of the array:
a[0 . . . n/2] and a[n/2 + 1 . . . n − 1]. Each of the two halves, in
turn, divide in half and count the amount to keep them, then
divide in half again, and so on until it reaches the current
segment length 1
The number of vertices in the worst case is estimated at
n + n /2 + n /4 + n /8 + · · · + 1 2 n
League of Programmers Segment Trees
26. Segment Trees
Problems
Solution
We start with a segment [0 . . . n-1]. and every time we divide
the current period of two (if it has not yet become a segment
of length), and then calling the same procedure on both
halves, and for each such segment we store the sum on it.
In other words, we calculate and remember somewhere sum of
the elements of the array, ie segment a[0 . . . n-1]. Also
calculate the amount of the two halves of the array:
a[0 . . . n/2] and a[n/2 + 1 . . . n − 1]. Each of the two halves, in
turn, divide in half and count the amount to keep them, then
divide in half again, and so on until it reaches the current
segment length 1
The number of vertices in the worst case is estimated at
n + n /2 + n /4 + n /8 + · · · + 1 2 n
The height of the tree is the value of the segments O(log n).
League of Programmers Segment Trees
27. Segment Trees
Problems
Implementation
Building the tree
League of Programmers Segment Trees
28. Segment Trees
Problems
Implementation
Building the tree
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves of the tree, then on the basis of these
values to calculate the nodes of the previous level as the sum
of the two leaves, then similarly calculate values for one more
level, etc. Convenient to describe the operation recursively.
League of Programmers Segment Trees
29. Segment Trees
Problems
Implementation
Building the tree
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves of the tree, then on the basis of these
values to calculate the nodes of the previous level as the sum
of the two leaves, then similarly calculate values for one more
level, etc. Convenient to describe the operation recursively.
Time to do this?
League of Programmers Segment Trees
30. Segment Trees
Problems
Implementation
Building the tree
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves of the tree, then on the basis of these
values to calculate the nodes of the previous level as the sum
of the two leaves, then similarly calculate values for one more
level, etc. Convenient to describe the operation recursively.
Time to do this?
League of Programmers Segment Trees
31. Segment Trees
Problems
Implementation
Building the tree
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves of the tree, then on the basis of these
values to calculate the nodes of the previous level as the sum
of the two leaves, then similarly calculate values for one more
level, etc. Convenient to describe the operation recursively.
Time to do this?
O(n) since each node in the tree is modied once and uses
only a max of 2 nodes (already computed) for computation.
League of Programmers Segment Trees
32. Segment Trees
Problems
Implementation
Query Request
League of Programmers Segment Trees
33. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
League of Programmers Segment Trees
34. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
League of Programmers Segment Trees
35. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
League of Programmers Segment Trees
36. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
League of Programmers Segment Trees
37. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
If its in one of the child, query on that child
League of Programmers Segment Trees
38. Segment Trees
Problems
Implementation
Query Request
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
If its in one of the child, query on that child
If its in both the child, do query on both of them
League of Programmers Segment Trees
39. Segment Trees
Problems
Implementation
Query Request
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
League of Programmers Segment Trees
40. Segment Trees
Problems
Implementation
Query Request
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
League of Programmers Segment Trees
41. Segment Trees
Problems
Implementation
Query Request
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
It is.
At any level of the tree, the maximum number of segments that
could call our recursive function when processing a request is 4.
League of Programmers Segment Trees
42. Segment Trees
Problems
Implementation
Query Request
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
It is.
At any level of the tree, the maximum number of segments that
could call our recursive function when processing a request is 4.
So, only O(log n) running time.
League of Programmers Segment Trees
43. Segment Trees
Problems
Implementation
Renewal Request
League of Programmers Segment Trees
44. Segment Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
League of Programmers Segment Trees
45. Segment Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
League of Programmers Segment Trees
46. Segment Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
League of Programmers Segment Trees
47. Segment Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
League of Programmers Segment Trees
48. Segment Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
The nodes from i th leaf node to the way upto the root of the
tree.
Then it is clear that the update request can be implemented as
a recursive function: it sends the current node of the tree
lines, and this function performs a recursive call from one of
his two sons (the one that contains the position i in its
segment), and after that - counts the value of the sum in the
current node in the same way as we did in the construction of
a tree of segments (ie, the sum of the values for the two sons
of the current node).
League of Programmers Segment Trees
49. Segment Trees
Problems
Implementation
Problem
League of Programmers Segment Trees
50. Segment Trees
Problems
Implementation
Problem
You are given an array. You have queries which ask for the
maximum element in the range l to r. And you have updates
which increase the value of a particular element in the array
with some value val.
League of Programmers Segment Trees
51. Segment Trees
Problems
Implementation
Problem
You are given an array. You have queries which ask for the
maximum element in the range l to r. And you have updates
which increase the value of a particular element in the array
with some value val.
But seriously, how to code?
League of Programmers Segment Trees
52. Segment Trees
Problems
Implementation
Problem
You are given an array. You have queries which ask for the
maximum element in the range l to r. And you have updates
which increase the value of a particular element in the array
with some value val.
But seriously, how to code?
Lets look at a code
League of Programmers Segment Trees
53. Segment Trees
Problems
Outline
1 Segment Trees
2 Problems
League of Programmers Segment Trees
54. Segment Trees
Problems
Problems
Links:
1 http://www.spoj.pl/problems/GSS1/
2 http://www.spoj.pl/problems/GSS3/
3 http://www.spoj.pl/problems/HORRIBLE/
4 http://www.spoj.pl/problems/BRCKTS/
5 http://www.spoj.pl/problems/HELPR2D2/
6 http://www.spoj.pl/problems/KFSTD/
7 http://www.spoj.pl/problems/FREQUENT/
8 http://www.spoj.pl/problems/LITE/
League of Programmers Segment Trees