Class: DataStructuresRMolinari::SegmentTree::MaxValSegmentTree
- Inherits:
-
Object
- Object
- DataStructuresRMolinari::SegmentTree::MaxValSegmentTree
- Extended by:
- Forwardable
- Defined in:
- lib/data_structures_rmolinari/segment_tree.rb
Overview
A segment tree that for an array A(0…n) answers questions of the form “what is the maximum value in the subinterval A(i..j)?” in O(log n) time.
Instance Method Summary collapse
-
#initialize(template_klass, data) ⇒ MaxValSegmentTree
constructor
A new instance of MaxValSegmentTree.
-
#max_on(i, j) ⇒ Object
The maximum value in A(i..j).
Constructor Details
#initialize(template_klass, data) ⇒ MaxValSegmentTree
Returns a new instance of MaxValSegmentTree.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/data_structures_rmolinari/segment_tree.rb', line 59 def initialize(template_klass, data) data.must_be_a Enumerable @structure = template_klass.new( combine: ->(a, b) { [a, b].max }, single_cell_array_val: ->(i) { data[i] }, size: data.size, identity: -Shared::INFINITY ) end |
Instance Method Details
#max_on(i, j) ⇒ Object
The maximum value in A(i..j).
The arguments must be integers in 0…(A.size)
74 75 76 |
# File 'lib/data_structures_rmolinari/segment_tree.rb', line 74 def max_on(i, j) @structure.query_on(i, j) end |