Upcoming SlideShare
×

# 06 segment trees

418

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
418
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
46
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 06 segment trees

1. 1. Segment Trees Problems Segment Trees League of Programmers ACA, IIT KanpurLeague of Programmers Segment Trees
2. 2. Segment Trees ProblemsOutline 1 Segment Trees 2 Problems League of Programmers Segment Trees
3. 3. Segment Trees ProblemsA Simple Problem Problem Statement We have an array a[0 . . . n-1]. League of Programmers Segment Trees
4. 4. Segment Trees ProblemsA Simple Problem Problem Statement We have an array a[0 . . . n-1]. League of Programmers Segment Trees
5. 5. Segment Trees ProblemsA 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. 6. Segment Trees ProblemsA 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. 7. Segment Trees ProblemsA Simple Problem Possible Solutions League of Programmers Segment Trees
8. 8. Segment Trees ProblemsA 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. 9. Segment Trees ProblemsA 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. 10. Segment Trees ProblemsA 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. 11. Segment Trees ProblemsA 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. 12. Segment Trees ProblemsA 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. 13. Segment Trees ProblemsSegment Trees League of Programmers Segment Trees
14. 14. Segment Trees ProblemsSegment Trees Representation of the tree League of Programmers Segment Trees
15. 15. Segment Trees ProblemsSegment Trees Representation of the tree Leaf Nodes are the elements in the array. League of Programmers Segment Trees
16. 16. Segment Trees ProblemsSegment 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. 17. Segment Trees ProblemsSegment 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. 18. Segment Trees ProblemsSegment 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. 19. Segment Trees ProblemsSegment 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. 20. Segment Trees ProblemsSegment 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. 21. Segment Trees ProblemsSegment 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. 22. Segment Trees ProblemsSolution League of Programmers Segment Trees
23. 23. Segment Trees ProblemsSolution 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. 24. Segment Trees ProblemsSolution 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. 25. Segment Trees ProblemsSolution 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. 26. Segment Trees ProblemsSolution 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. 27. Segment Trees ProblemsImplementation Building the tree League of Programmers Segment Trees
28. 28. Segment Trees ProblemsImplementation 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. 29. Segment Trees ProblemsImplementation 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. 30. Segment Trees ProblemsImplementation 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. 31. Segment Trees ProblemsImplementation 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. 32. Segment Trees ProblemsImplementation Query Request League of Programmers Segment Trees
33. 33. Segment Trees ProblemsImplementation 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. 34. Segment Trees ProblemsImplementation 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. 35. Segment Trees ProblemsImplementation 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. 36. Segment Trees ProblemsImplementation 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. 37. Segment Trees ProblemsImplementation 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. 38. Segment Trees ProblemsImplementation 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. 39. Segment Trees ProblemsImplementation 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. 40. Segment Trees ProblemsImplementation 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 doesnt look O(log n) League of Programmers Segment Trees
41. 41. Segment Trees ProblemsImplementation 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 doesnt 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. 42. Segment Trees ProblemsImplementation 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 doesnt 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. 43. Segment Trees ProblemsImplementation Renewal Request League of Programmers Segment Trees
44. 44. Segment Trees ProblemsImplementation Renewal Request Given an index i and the value of x. What to do? League of Programmers Segment Trees
45. 45. Segment Trees ProblemsImplementation 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. 46. Segment Trees ProblemsImplementation 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. 47. Segment Trees ProblemsImplementation 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. 48. Segment Trees ProblemsImplementation 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. 49. Segment Trees ProblemsImplementation Problem League of Programmers Segment Trees
50. 50. Segment Trees ProblemsImplementation 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. 51. Segment Trees ProblemsImplementation 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. 52. Segment Trees ProblemsImplementation 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. 53. Segment Trees ProblemsOutline 1 Segment Trees 2 Problems League of Programmers Segment Trees
54. 54. Segment Trees ProblemsProblems 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
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.