Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathLazySegmentTree.java
More file actions
Latest commit
174 lines (156 loc) · 5.17 KB
/
Copy pathLazySegmentTree.java
File metadata and controls
174 lines (156 loc) · 5.17 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
packagecom.thealgorithms.datastructures.trees;
publicclassLazySegmentTree {
/**
* Lazy Segment Tree
*
* @see
* <a href="https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree/">
*/
staticclassNode {
privatefinalintstart;
privatefinalintend; // start and end of the segment represented by this node
privateintvalue; // value is the sum of all elements in the range [start, end)
privateintlazy; // lazied value that should be added to children nodes
Nodeleft;
Noderight; // left and right children
Node(intstart, intend, intvalue) {
this.start = start;
this.end = end;
this.value = value;
this.lazy = 0;
this.left = null;
this.right = null;
}
/**
* Update the value of this node with the given value diff.
*
* @param diff The value to add to every index of this node range.
*/
publicvoidapplyUpdate(intdiff) {
this.lazy += diff;
this.value += (this.end - this.start) * diff;
}
/**
* Shift the lazy value of this node to its children.
*/
publicvoidshift() {
if (lazy == 0) {
return;
}
if (this.left == null && this.right == null) {
return;
}
this.value += this.lazy;
if (this.left != null) {
this.left.applyUpdate(this.lazy);
}
if (this.right != null) {
this.right.applyUpdate(this.lazy);
}
this.lazy = 0;
}
/**
* Create a new node that is the sum of this node and the given node.
*
* @param left The left Node of merging
* @param right The right Node of merging
* @return The new Node.
*/
staticNodemerge(Nodeleft, Noderight) {
if (left == null) {
returnright;
}
if (right == null) {
returnleft;
}
Noderesult = newNode(left.start, right.end, left.value + right.value);
result.left = left;
result.right = right;
returnresult;
}
publicintgetValue() {
returnvalue;
}
publicNodegetLeft() {
returnleft;
}
publicNodegetRight() {
returnright;
}
}
privatefinalNoderoot;
/**
* Create a new LazySegmentTree with the given array.
*
* @param array The array to create the LazySegmentTree from.
*/
publicLazySegmentTree(int[] array) {
this.root = buildTree(array, 0, array.length);
}
/**
* Build a new LazySegmentTree from the given array in O(n) time.
*
* @param array The array to build the LazySegmentTree from.
* @param start The start index of the current node.
* @param end The end index of the current node.
* @return The root of the new LazySegmentTree.
*/
privateNodebuildTree(int[] array, intstart, intend) {
if (end - start < 2) {
returnnewNode(start, end, array[start]);
}
intmid = (start + end) >> 1;
Nodeleft = buildTree(array, start, mid);
Noderight = buildTree(array, mid, end);
returnNode.merge(left, right);
}
/**
* Update the value of given range with the given value diff in O(log n) time.
*
* @param left The left index of the range to update.
* @param right The right index of the range to update.
* @param diff The value to add to every index of the range.
* @param curr The current node.
*/
privatevoidupdateRange(intleft, intright, intdiff, Nodecurr) {
if (left <= curr.start && curr.end <= right) {
curr.applyUpdate(diff);
return;
}
if (left >= curr.end || right <= curr.start) {
return;
}
curr.shift();
updateRange(left, right, diff, curr.left);
updateRange(left, right, diff, curr.right);
Nodemerge = Node.merge(curr.left, curr.right);
curr.value = merge.value;
}
/**
* Get Node of given range in O(log n) time.
*
* @param left The left index of the range to update.
* @param right The right index of the range to update.
* @return The Node representing the sum of the given range.
*/
privateNodegetRange(intleft, intright, Nodecurr) {
if (left <= curr.start && curr.end <= right) {
returncurr;
}
if (left >= curr.end || right <= curr.start) {
returnnull;
}
curr.shift();
returnNode.merge(getRange(left, right, curr.left), getRange(left, right, curr.right));
}
publicintgetRange(intleft, intright) {
Noderesult = getRange(left, right, root);
returnresult == null ? 0 : result.getValue();
}
publicvoidupdateRange(intleft, intright, intdiff) {
updateRange(left, right, diff, root);
}
publicNodegetRoot() {
returnroot;
}
}