Segment tree is an efficient, but not always completely useful date structure. For example: if we have an array of length 8, it will take care of segments 1-2, 3-4, 5-6, 7-8, 1-4, 5-8 and 1-8. But many will be left out, such as 2-3, 2-4, 4-5, 6-7 etc. Is there an efficient data structure that takes care of all of the segments of input data?
Asked
Active
Viewed 57 times
1
-
What does it mean to “take care” of a segment? – Scott Hunter Apr 21 '18 at 18:36
-
I'll show what I mean with an example. If we are searching for a segment in an array with maximum sum, segment tree will not check the segment spanning from 6 to 7 or from 5 to 7 or some of the other segments that I have enlisted in my question. So it might happen that the segment tree won't give us a correct answer. Is there a data structure that checks all of the segments efficiently? – Hinko Pih Pih Apr 21 '18 at 18:43
-
A segment tree only works if you can compute the answer for a segment by using/combining the answers to other smaller segments. And this is a key part of how the segment tree works -- you don't always have the answer for the query segment, so you have to compute it using other segments in the tree. – k_ssb Apr 22 '18 at 00:11
1 Answers
2
No, its not true. It actually "take care" of every intervals.
For instance, in above segment tree, if you need to perform an query for range [4, 7], it will go to left subtree like [0, 4] -> [3, 4] -> [4, 4] and in right subtree [5, 9] -> [5, 7] and then aggregate the result of [4, 4] and [5, 7] and pass the result up to root.
I would suggest to simulate with pen and paper or use your debugger to see what happens under the hood of the recursion calls. Good luck!

Kaidul
- 15,409
- 15
- 81
- 150