Skip to content

Commit 433d1f9

Browse files
authored
Update Contents.swift
1 parent 5bdfb5d commit 433d1f9

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

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

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
//: Playground - noun: a place where people can play
2-
3-
// last checked with Xcode 9.0b4
4-
#if swift(>=4.0)
5-
print("Hello, Swift 4!")
6-
#endif
7-
8-
//protocol SegmentTree<T> {
9-
// func pushUp(lson: T, rson: T)
10-
// func pushDown(round: Int, lson: T, rson: T)
11-
//}
12-
131
public class LazySegmentTree {
142

153
private var value: Int
@@ -26,22 +14,20 @@ public class LazySegmentTree {
2614
private var lazyValue: Int
2715

2816
// MARK: - Push Up Operation
29-
// Description: 这里是 push up 操作,用来对 Segment Tree 向上更新
17+
// Description: pushUp() - update items to the top
3018
private func pushUp(lson: LazySegmentTree, rson: LazySegmentTree) {
3119
self.value = lson.value + rson.value
3220
}
3321

3422
// MARK: - Push Down Operation
35-
// Description: 这里是 push down 操作,用来对 Segment Tree 向下更新
36-
// Open Interface Function: 此处应该开放方法对齐进行 Override
23+
// Description: pushDown() - update items to the bottom
3724
private func pushDown(round: Int, lson: LazySegmentTree, rson: LazySegmentTree) {
38-
if lazyValue != 0 {
39-
lson.lazyValue += lazyValue
40-
rson.lazyValue += lazyValue
41-
lson.value += lazyValue * (round - (round >> 1))
42-
rson.value += lazyValue * (round >> 1)
43-
lazyValue = 0
44-
}
25+
guard lazyValue != 0 else { return }
26+
lson.lazyValue += lazyValue
27+
rson.lazyValue += lazyValue
28+
lson.value += lazyValue * (round - (round >> 1))
29+
rson.value += lazyValue * (round >> 1)
30+
lazyValue = 0
4531
}
4632

4733
public init(array: [Int], leftBound: Int, rightBound: Int) {
@@ -50,16 +36,17 @@ public class LazySegmentTree {
5036
self.value = 0
5137
self.lazyValue = 0
5238

53-
if leftBound == rightBound {
39+
guard leftBound != rightBound else {
5440
value = array[leftBound]
5541
return
5642
}
5743

58-
let middle = (leftBound + rightBound) / 2
44+
let middle = leftBound + (rightBound - leftBound) / 2
5945
leftChild = LazySegmentTree(array: array, leftBound: leftBound, rightBound: middle)
6046
rightChild = LazySegmentTree(array: array, leftBound: middle + 1, rightBound: rightBound)
61-
pushUp(lson: leftChild!, rson: rightChild!)
62-
47+
if let leftChild = leftChild, let rightChild = rightChild {
48+
pushUp(lson: leftChild, rson: rightChild)
49+
}
6350
}
6451

6552
public convenience init(array: [Int]) {
@@ -75,7 +62,7 @@ public class LazySegmentTree {
7562

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

78-
let middle = (self.leftBound + self.rightBound) / 2
65+
let middle = self.leftBound + (self.rightBound - self.leftBound) / 2
7966
var result: Int = 0
8067

8168
if leftBound <= middle { result += leftChild.query(leftBound: leftBound, rightBound: rightBound) }
@@ -86,14 +73,14 @@ public class LazySegmentTree {
8673

8774
// MARK: - One Item Update
8875
public func update(index: Int, incremental: Int) {
89-
if self.leftBound == self.rightBound {
76+
guard self.leftBound != self.rightBound else {
9077
self.value += incremental
9178
return
9279
}
9380
guard let leftChild = leftChild else { fatalError("leftChild should not be nil") }
9481
guard let rightChild = rightChild else { fatalError("rightChild should not be nil") }
9582

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

9885
if index <= middle { leftChild.update(index: index, incremental: incremental) }
9986
else { rightChild.update(index: index, incremental: incremental) }
@@ -113,7 +100,7 @@ public class LazySegmentTree {
113100

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

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

118105
if leftBound <= middle { leftChild.update(leftBound: leftBound, rightBound: rightBound, incremental: incremental) }
119106
if middle < rightBound { rightChild.update(leftBound: leftBound, rightBound: rightBound, incremental: incremental) }
@@ -140,8 +127,3 @@ for index in 2 ... 5 {
140127

141128
sumSegmentTree.update(leftBound: 0, rightBound: 5, incremental: 2)
142129
print(sumSegmentTree.query(leftBound: 0, rightBound: 2))
143-
144-
145-
146-
147-

0 commit comments

Comments
 (0)