NewSegmentTree returns a new instance of a SegmentTree. It takes an input array of integers representing Array, initializes and builds the SegmentTree.
(Array []int)
| 115 | // NewSegmentTree returns a new instance of a SegmentTree. It takes an input |
| 116 | // array of integers representing Array, initializes and builds the SegmentTree. |
| 117 | func NewSegmentTree(Array []int) *SegmentTree { |
| 118 | if len(Array) == 0 { |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | segTree := SegmentTree{ |
| 123 | Array: Array, |
| 124 | SegmentTree: make([]int, 4*len(Array)), |
| 125 | LazyTree: make([]int, 4*len(Array)), |
| 126 | } |
| 127 | |
| 128 | for i := range segTree.LazyTree { |
| 129 | //fill LazyTree with empty lazy nodes |
| 130 | segTree.LazyTree[i] = emptyLazyNode |
| 131 | } |
| 132 | |
| 133 | //starts with node 1 and interval [0, len(arr)-1] inclusive |
| 134 | segTree.Build(1, 0, len(Array)-1) |
| 135 | |
| 136 | return &segTree |
| 137 | } |