-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
126 lines (122 loc) · 4.33 KB
/
Copy pathcompression.diff
File metadata and controls
126 lines (122 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--- input/input.go
+++ output/output.go
@@ -24,48 +24,16 @@
func (s *SegmentTree) Propagate(node int, leftNode int, rightNode int) {
if s.LazyTree[node] != emptyLazyNode {
//add lazy node value multiplied by (right-left+1), which represents all interval
- //this is the same of adding a value on each node
- s.SegmentTree[node] += (rightNode - leftNode + 1) * s.LazyTree[node]
-
- if leftNode == rightNode {
- //leaf node
- s.Array[leftNode] += s.LazyTree[node]
- } else {
- //propagate lazy node value for children nodes
- //may propagate multiple times, children nodes should accumulate lazy node value
- s.LazyTree[2*node] += s.LazyTree[node]
- s.LazyTree[2*node+1] += s.LazyTree[node]
- }
-
- //clear lazy node
- s.LazyTree[node] = emptyLazyNode
+ { … 15 line(s) … ⟦tj:275c6a6ade52bfab833e0cef116197e2⟧ }
}
-}
// Query returns the sum of elements of the array in the interval [firstIndex, leftIndex].
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Query(node int, leftNode int, rightNode int, firstIndex int, lastIndex int) int {
if (firstIndex > lastIndex) || (leftNode > rightNode) {
//outside the interval
- return 0
- }
-
- //propagate lazy tree
- s.Propagate(node, leftNode, rightNode)
-
- if (leftNode >= firstIndex) && (rightNode <= lastIndex) {
- //inside the interval
- return s.SegmentTree[node]
- }
-
- //get sum of left and right nodes
- mid := (leftNode + rightNode) / 2
-
- leftNodeSum := s.Query(2*node, leftNode, mid, firstIndex, min.Int(mid, lastIndex))
- rightNodeSum := s.Query(2*node+1, mid+1, rightNode, max.Int(firstIndex, mid+1), lastIndex)
-
+ { … 17 line(s) … ⟦tj:c4903fc762fa139e9c248aa15c751f78⟧ }
return leftNodeSum + rightNodeSum
-}
// Update updates the elements of the array in the range [firstIndex, lastIndex]
// with the new value provided and recomputes the sum of different ranges.
@@ -73,65 +41,24 @@
func (s *SegmentTree) Update(node int, leftNode int, rightNode int, firstIndex int, lastIndex int, value int) {
//propagate lazy tree
s.Propagate(node, leftNode, rightNode)
-
- if (firstIndex > lastIndex) || (leftNode > rightNode) {
- //outside the interval
- return
+ { … 19 line(s) … ⟦tj:6ee939f1c046fd3425d7d5626ca90d0c⟧ }
}
- if (leftNode >= firstIndex) && (rightNode <= lastIndex) {
- //inside the interval
- //accumulate the lazy node value
- s.LazyTree[node] += value
- s.Propagate(node, leftNode, rightNode)
- } else {
- //update left and right nodes
- mid := (leftNode + rightNode) / 2
-
- s.Update(2*node, leftNode, mid, firstIndex, min.Int(mid, lastIndex), value)
- s.Update(2*node+1, mid+1, rightNode, max.Int(firstIndex, mid+1), lastIndex, value)
-
- s.SegmentTree[node] = s.SegmentTree[2*node] + s.SegmentTree[2*node+1]
- }
-}
-
// Build builds the SegmentTree by computing the sum of different ranges.
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Build(node int, left int, right int) {
if left == right {
//leaf node
- s.SegmentTree[node] = s.Array[left]
- } else {
- //get sum of left and right nodes
- mid := (left + right) / 2
-
- s.Build(2*node, left, mid)
- s.Build(2*node+1, mid+1, right)
-
- s.SegmentTree[node] = s.SegmentTree[2*node] + s.SegmentTree[2*node+1]
+ { … 9 line(s) … ⟦tj:f74716ead13f10781d7285f33c1f4964⟧ }
}
-}
// NewSegmentTree returns a new instance of a SegmentTree. It takes an input
// array of integers representing Array, initializes and builds the SegmentTree.
func NewSegmentTree(Array []int) *SegmentTree {
if len(Array) == 0 {
return nil
- }
-
- segTree := SegmentTree{
- Array: Array,
- SegmentTree: make([]int, 4*len(Array)),
- LazyTree: make([]int, 4*len(Array)),
- }
-
- for i := range segTree.LazyTree {
- //fill LazyTree with empty lazy nodes
- segTree.LazyTree[i] = emptyLazyNode
- }
-
- //starts with node 1 and interval [0, len(arr)-1] inclusive
- segTree.Build(1, 0, len(Array)-1)
-
+ { … 16 line(s) … ⟦tj:bc0a487fe05ff9be2e0639b82c25be15⟧ }
return &segTree
-}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (4496 bytes): call tinyjuice_retrieve with token "2b0546900ac8781d41cad05f73b52964"]
\ No newline at end of file