Skip to content

Commit 3759458

Browse files
committed
Update mid is l + (r + l) / 2
1 parent c4952f9 commit 3759458

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

Segment Tree/LazyPropagation/LazyPropagation.playground/Contents.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class LazySegmentTree {
4141
return
4242
}
4343

44-
let middle = (leftBound + rightBound) / 2
44+
let middle = leftBound + (rightBound - leftBound) / 2
4545
leftChild = LazySegmentTree(array: array, leftBound: leftBound, rightBound: middle)
4646
rightChild = LazySegmentTree(array: array, leftBound: middle + 1, rightBound: rightBound)
4747
if let leftChild = leftChild, let rightChild = rightChild {
@@ -62,7 +62,7 @@ public class LazySegmentTree {
6262

6363
pushDown(round: self.rightBound - self.leftBound + 1, lson: leftChild, rson: rightChild)
6464

65-
let middle = (self.leftBound + self.rightBound) / 2
65+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
6666
var result = 0
6767

6868
if leftBound <= middle { result += leftChild.query(leftBound: leftBound, rightBound: rightBound) }
@@ -80,7 +80,7 @@ public class LazySegmentTree {
8080
guard let leftChild = leftChild else { fatalError("leftChild should not be nil") }
8181
guard let rightChild = rightChild else { fatalError("rightChild should not be nil") }
8282

83-
let middle = (self.leftBound + self.rightBound) / 2
83+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
8484

8585
if index <= middle { leftChild.update(index: index, incremental: incremental) }
8686
else { rightChild.update(index: index, incremental: incremental) }
@@ -100,7 +100,7 @@ public class LazySegmentTree {
100100

101101
pushDown(round: self.rightBound - self.leftBound + 1, lson: leftChild, rson: rightChild)
102102

103-
let middle = (self.leftBound + self.rightBound) / 2
103+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
104104

105105
if leftBound <= middle { leftChild.update(leftBound: leftBound, rightBound: rightBound, incremental: incremental) }
106106
if middle < rightBound { rightChild.update(leftBound: leftBound, rightBound: rightBound, incremental: incremental) }
@@ -126,4 +126,4 @@ for index in 2 ... 5 {
126126

127127

128128
sumSegmentTree.update(leftBound: 0, rightBound: 5, incremental: 2)
129-
print(sumSegmentTree.query(leftBound: 0, rightBound: 2))
129+
print(sumSegmentTree.query(leftBound: 0, rightBound: 2))

Segment Tree/LazyPropagation/README.markdown

+10-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ But that is common Segment Tree. By **Lazy Propagation**, we can implement to mo
2222
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:
2323

2424
```swift
25+
// Author: Artur Antonov
2526
public init(array: [T], leftBound: Int, rightBound: Int, function: @escaping (T, T) -> T) {
2627
self.leftBound = leftBound
2728
self.rightBound = rightBound
@@ -45,6 +46,7 @@ In position ①, it means the current node is *leaf* because its left bound data
4546
And then, we have a look for *interval query* operation:
4647

4748
```swift
49+
// Author: Artur Antonov
4850
public func query(leftBound: Int, rightBound: Int) -> T {
4951
if self.leftBound == leftBound && self.rightBound == rightBound {
5052
return self.value
@@ -97,7 +99,7 @@ public init(array: [Int], leftBound: Int, rightBound: Int) {
9799
return
98100
}
99101

100-
let middle = (leftBound + rightBound) / 2
102+
let middle = leftBound + (rightBound - leftBound) / 2
101103
leftChild = LazySegmentTree(array: array, leftBound: leftBound, rightBound: middle)
102104
rightChild = LazySegmentTree(array: array, leftBound: middle + 1, rightBound: rightBound)
103105
if let leftChild = leftChild, let rightChild = rightChild {
@@ -113,7 +115,7 @@ public func update(index: Int, incremental: Int) {
113115
guard let leftChild = leftChild else { fatalError("leftChild should not be nil") }
114116
guard let rightChild = rightChild else { fatalError("rightChild should not be nil") }
115117

116-
let middle = (self.leftBound + self.rightBound) / 2
118+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
117119

118120
if index <= middle { leftChild.update(index: index, incremental: incremental) }
119121
else { rightChild.update(index: index, incremental: incremental) }
@@ -198,7 +200,7 @@ public class LazySegmentTree {
198200
return
199201
}
200202

201-
let middle = (leftBound + rightBound) / 2
203+
let middle = leftBound + (rightBound - leftBound) / 2
202204
leftChild = LazySegmentTree(array: array, leftBound: leftBound, rightBound: middle)
203205
rightChild = LazySegmentTree(array: array, leftBound: middle + 1, rightBound: rightBound)
204206
if let leftChild = leftChild, let rightChild = rightChild {
@@ -219,7 +221,7 @@ public class LazySegmentTree {
219221

220222
pushDown(round: self.rightBound - self.leftBound + 1, lson: leftChild, rson: rightChild)
221223

222-
let middle = (self.leftBound + self.rightBound) / 2
224+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
223225
var result = 0
224226

225227
if leftBound <= middle { result += leftChild.query(leftBound: leftBound, rightBound: rightBound) }
@@ -237,7 +239,7 @@ public class LazySegmentTree {
237239
guard let leftChild = leftChild else { fatalError("leftChild should not be nil") }
238240
guard let rightChild = rightChild else { fatalError("rightChild should not be nil") }
239241

240-
let middle = (self.leftBound + self.rightBound) / 2
242+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
241243

242244
if index <= middle { leftChild.update(index: index, incremental: incremental) }
243245
else { rightChild.update(index: index, incremental: incremental) }
@@ -257,7 +259,7 @@ public class LazySegmentTree {
257259

258260
pushDown(round: self.rightBound - self.leftBound + 1, lson: leftChild, rson: rightChild)
259261

260-
let middle = (self.leftBound + self.rightBound) / 2
262+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
261263

262264
if leftBound <= middle { leftChild.update(leftBound: leftBound, rightBound: rightBound, incremental: incremental) }
263265
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
302304
---
303305

304306
*Written for Swift Algorithm Club by [Desgard_Duan](https://github.com/desgard)*
307+
308+

0 commit comments

Comments
 (0)