c++ - Finding Maximum of an interval in array using binary trees -
i'm studying binary trees! , have problem in homework. have use binary trees solve problem here problem : given list of integers. need answer number of questions of form: "what maximum value of elements of list content between index , index b?". example :
input : 10 2 4 3 5 7 19 3 8 6 7 4 1 5 3 6 8 10 3 9 output: 7 19 8 19 time limits , memory (language: c + +)
time: 0.5s on 1ghz machine. memory: 16000 kb
constraints
1 <= n <= 100000, n number of elements in list.
1 <= a, b <= n, a, b limits of range.
1 <= <= 10 000, number of intervals.
please not give me solution hint ! !
as discussed in comments, make things simple, can add entries array make size power of two, binary tree has same depth leaves. doesn't matter elements add list, won't use these computed values in actual algorithm.
in binary tree, have compute maxima in bottom-up manner. these values tell maximum of whole range these nodes representing; major idea of tree.
what remains splitting query such tree nodes, represent original interval using less nodes size of interval. figure out "the pattern" of intervals tree nodes represent. figure out way split input interval few nodes possible. maybe start trivial solution: split input in leave nodes, i.e. single elements. figure out how can "combine" multiple elements interval using inner nodes tree. find algorithm doing not using tree (since require linear time in number of elements, whole idea of tree make logarithmic).
Comments
Post a Comment