@@ -22,6 +22,7 @@ But that is common Segment Tree. By **Lazy Propagation**, we can implement to mo
22
22
At first, we reference the implement of ** Artur Antonov** about Segment Tree. This code contained * build* , * single update* and * interval query* three operation. The implement of * build* and * single update* operation is following:
23
23
24
24
``` swift
25
+ // Author: Artur Antonov
25
26
public init (array : [T], leftBound : Int , rightBound : Int , function : @escaping (T, T) -> T) {
26
27
self .leftBound = leftBound
27
28
self .rightBound = rightBound
@@ -45,6 +46,7 @@ In position ①, it means the current node is *leaf* because its left bound data
45
46
And then, we have a look for * interval query* operation:
46
47
47
48
``` swift
49
+ // Author: Artur Antonov
48
50
public func query (leftBound : Int , rightBound : Int ) -> T {
49
51
if self .leftBound == leftBound && self .rightBound == rightBound {
50
52
return self .value
@@ -97,7 +99,7 @@ public init(array: [Int], leftBound: Int, rightBound: Int) {
97
99
return
98
100
}
99
101
100
- let middle = ( leftBound + rightBound) / 2
102
+ let middle = leftBound + ( rightBound - leftBound ) / 2
101
103
leftChild = LazySegmentTree (array : array, leftBound : leftBound, rightBound : middle)
102
104
rightChild = LazySegmentTree (array : array, leftBound : middle + 1 , rightBound : rightBound)
103
105
if let leftChild = leftChild, let rightChild = rightChild {
@@ -113,7 +115,7 @@ public func update(index: Int, incremental: Int) {
113
115
guard let leftChild = leftChild else { fatalError (" leftChild should not be nil" ) }
114
116
guard let rightChild = rightChild else { fatalError (" rightChild should not be nil" ) }
115
117
116
- let middle = ( self .leftBound + self .rightBound ) / 2
118
+ let middle = self .leftBound + ( self .rightBound - self . leftBound ) / 2
117
119
118
120
if index <= middle { leftChild.update (index : index, incremental : incremental) }
119
121
else { rightChild.update (index : index, incremental : incremental) }
@@ -198,7 +200,7 @@ public class LazySegmentTree {
198
200
return
199
201
}
200
202
201
- let middle = ( leftBound + rightBound) / 2
203
+ let middle = leftBound + ( rightBound - leftBound ) / 2
202
204
leftChild = LazySegmentTree (array : array, leftBound : leftBound, rightBound : middle)
203
205
rightChild = LazySegmentTree (array : array, leftBound : middle + 1 , rightBound : rightBound)
204
206
if let leftChild = leftChild, let rightChild = rightChild {
@@ -219,7 +221,7 @@ public class LazySegmentTree {
219
221
220
222
pushDown (round : self .rightBound - self .leftBound + 1 , lson : leftChild, rson : rightChild)
221
223
222
- let middle = ( self .leftBound + self .rightBound ) / 2
224
+ let middle = self .leftBound + ( self .rightBound - self . leftBound ) / 2
223
225
var result = 0
224
226
225
227
if leftBound <= middle { result += leftChild.query (leftBound : leftBound, rightBound : rightBound) }
@@ -237,7 +239,7 @@ public class LazySegmentTree {
237
239
guard let leftChild = leftChild else { fatalError (" leftChild should not be nil" ) }
238
240
guard let rightChild = rightChild else { fatalError (" rightChild should not be nil" ) }
239
241
240
- let middle = ( self .leftBound + self .rightBound ) / 2
242
+ let middle = self .leftBound + ( self .rightBound - self . leftBound ) / 2
241
243
242
244
if index <= middle { leftChild.update (index : index, incremental : incremental) }
243
245
else { rightChild.update (index : index, incremental : incremental) }
@@ -257,7 +259,7 @@ public class LazySegmentTree {
257
259
258
260
pushDown (round : self .rightBound - self .leftBound + 1 , lson : leftChild, rson : rightChild)
259
261
260
- let middle = ( self .leftBound + self .rightBound ) / 2
262
+ let middle = self .leftBound + ( self .rightBound - self . leftBound ) / 2
261
263
262
264
if leftBound <= middle { leftChild.update (leftBound : leftBound, rightBound : rightBound, incremental : incremental) }
263
265
if middle < rightBound { rightChild.update (leftBound : leftBound, rightBound : rightBound, incremental : incremental) }
@@ -302,3 +304,5 @@ In fact, the operation of Segment Tree is far more than that. It can also be use
302
304
---
303
305
304
306
* Written for Swift Algorithm Club by [ Desgard_Duan] ( https://github.com/desgard ) *
307
+
308
+
0 commit comments