Class: DataStructuresRMolinari::MaxValSegmentTree
- Inherits:
-
Object
- Object
- DataStructuresRMolinari::MaxValSegmentTree
- Extended by:
- Forwardable
- Defined in:
- lib/data_structures_rmolinari.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(data) ⇒ MaxValSegmentTree
constructor
A new instance of MaxValSegmentTree.
-
#max_on(i, j) ⇒ Object
The maximum value in A(i..j).
Constructor Details
#initialize(data) ⇒ MaxValSegmentTree
Returns a new instance of MaxValSegmentTree.
36 37 38 39 40 41 42 43 |
# File 'lib/data_structures_rmolinari.rb', line 36 def initialize(data) @structure = SegmentTreeTemplate.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)
49 50 51 |
# File 'lib/data_structures_rmolinari.rb', line 49 def max_on(i, j) @structure.query_on(i, j) end |