@@ -97,7 +97,7 @@ public init(array: [Int], leftBound: Int, rightBound: Int) {
97
97
return
98
98
}
99
99
100
- let middle = leftBound + ( rightBound - leftBound ) / 2
100
+ let middle = ( leftBound + rightBound) / 2
101
101
leftChild = LazySegmentTree (array : array, leftBound : leftBound, rightBound : middle)
102
102
rightChild = LazySegmentTree (array : array, leftBound : middle + 1 , rightBound : rightBound)
103
103
if let leftChild = leftChild, let rightChild = rightChild {
@@ -113,7 +113,7 @@ public func update(index: Int, incremental: Int) {
113
113
guard let leftChild = leftChild else { fatalError (" leftChild should not be nil" ) }
114
114
guard let rightChild = rightChild else { fatalError (" rightChild should not be nil" ) }
115
115
116
- let middle = self . rightBound + (self .leftBound - self .rightBound ) / 2
116
+ let middle = (self .leftBound + self .rightBound ) / 2
117
117
118
118
if index <= middle { leftChild.update (index : index, incremental : incremental) }
119
119
else { rightChild.update (index : index, incremental : incremental) }
@@ -198,7 +198,7 @@ public class LazySegmentTree {
198
198
return
199
199
}
200
200
201
- let middle = leftBound + ( rightBound - leftBound ) / 2
201
+ let middle = ( leftBound + rightBound) / 2
202
202
leftChild = LazySegmentTree (array : array, leftBound : leftBound, rightBound : middle)
203
203
rightChild = LazySegmentTree (array : array, leftBound : middle + 1 , rightBound : rightBound)
204
204
if let leftChild = leftChild, let rightChild = rightChild {
@@ -219,8 +219,8 @@ public class LazySegmentTree {
219
219
220
220
pushDown (round : self .rightBound - self .leftBound + 1 , lson : leftChild, rson : rightChild)
221
221
222
- let middle = self .leftBound + ( self .rightBound - self . leftBound ) / 2
223
- var result: Int = 0
222
+ let middle = ( self .leftBound + self .rightBound ) / 2
223
+ var result = 0
224
224
225
225
if leftBound <= middle { result += leftChild.query (leftBound : leftBound, rightBound : rightBound) }
226
226
if rightBound > middle { result += rightChild.query (leftBound : leftBound, rightBound : rightBound) }
@@ -237,7 +237,7 @@ public class LazySegmentTree {
237
237
guard let leftChild = leftChild else { fatalError (" leftChild should not be nil" ) }
238
238
guard let rightChild = rightChild else { fatalError (" rightChild should not be nil" ) }
239
239
240
- let middle = self . rightBound + (self .leftBound - self .rightBound ) / 2
240
+ let middle = (self .leftBound + self .rightBound ) / 2
241
241
242
242
if index <= middle { leftChild.update (index : index, incremental : incremental) }
243
243
else { rightChild.update (index : index, incremental : incremental) }
@@ -252,12 +252,12 @@ public class LazySegmentTree {
252
252
return
253
253
}
254
254
255
- guard let leftChild = leftChild else { fatalError () }
256
- guard let rightChild = rightChild else { fatalError () }
255
+ guard let leftChild = leftChild else { fatalError (" leftChild should not be nil " ) }
256
+ guard let rightChild = rightChild else { fatalError (" rightChild should not be nil " ) }
257
257
258
258
pushDown (round : self .rightBound - self .leftBound + 1 , lson : leftChild, rson : rightChild)
259
259
260
- let middle = self . rightBound + (self .leftBound - self .rightBound ) / 2
260
+ let middle = (self .leftBound + self .rightBound ) / 2
261
261
262
262
if leftBound <= middle { leftChild.update (leftBound : leftBound, rightBound : rightBound, incremental : incremental) }
263
263
if middle < rightBound { rightChild.update (leftBound : leftBound, rightBound : rightBound, incremental : incremental) }
@@ -302,7 +302,3 @@ In fact, the operation of Segment Tree is far more than that. It can also be use
302
302
---
303
303
304
304
* Written for Swift Algorithm Club by [ Desgard_Duan] ( https://github.com/desgard ) *
305
-
306
-
307
-
308
-
0 commit comments