9
9
package com .williamfiset .algorithms .datastructures .segmenttree ;
10
10
11
11
public class RangeQueryPointUpdateSegmentTree {
12
+ // TODO(william): make the members of this class private
12
13
13
- // Tree values.
14
- // TODO(william): make these members private
14
+ // Tree segment values.
15
15
Integer [] t ;
16
16
17
17
// The number of values in the original input values array.
@@ -33,7 +33,6 @@ public RangeQueryPointUpdateSegmentTree(int[] values) {
33
33
t = new Integer [N ];
34
34
35
35
buildTree (0 , 0 , n - 1 , values );
36
-
37
36
// System.out.println(java.util.Arrays.toString(values));
38
37
// System.out.println(java.util.Arrays.toString(t));
39
38
}
@@ -116,39 +115,37 @@ private long sumQuery2(int i, int tl, int tr, int l, int r) {
116
115
}
117
116
}
118
117
119
- public void set (int i , int value ) {
120
- // update(i, 0, n-1, value);
121
- }
122
-
118
+ // Updates the segment tree to reflect that index `i` in the original `values` array was updated
119
+ // to `newValue`.
123
120
public void update (int i , int newValue ) {
124
121
update (0 , i , 0 , n - 1 , newValue );
125
122
}
126
123
127
124
/**
128
- * Update a point in the segment tree by doing a binary search, updating the leaf node and
129
- * re-computing all the segment values on the callback.
125
+ * Update a point segment to a new value and update all affected segments.
126
+ *
127
+ * <p>Do this by performing a binary search to find the interval containing the point, then update
128
+ * the leaf segment with the new value, and re-compute all affected segment values on the
129
+ * callback.
130
130
*
131
131
* @param at the index of the current segment in the tree
132
- * @param to the target position to update left endpoint for the range query
133
- * @param tl the left endpoint that the of the current segment
134
- * @param tr the right endpoint that the of the current segment
135
- * @param r the target right endpoint for the range query
132
+ * @param pos the target position to update
133
+ * @param tl the left segment endpoint
134
+ * @param tr the right segment endpoint
135
+ * @param newValue the new value to update
136
136
*/
137
- private void update (int at , int to , int tl , int tr , int newValue ) {
138
- if (tl > tr ) {
139
- return ;
140
- }
141
- if (tl == tr ) { // or `tl == to && tr == to`
137
+ private void update (int at , int pos , int tl , int tr , int newValue ) {
138
+ if (tl == tr ) { // `tl == pos && tr == pos` might be clearer
142
139
t [at ] = newValue ;
143
140
return ;
144
141
}
145
142
int tm = (tl + tr ) / 2 ;
146
- // Dig into the left segment
147
- if (to <= tm ) {
148
- update (2 * at + 1 , to , tl , tm , newValue );
149
- // Dig into the right segment
143
+ // The point index `pos` is contained within the left segment [tl, tm]
144
+ if (pos <= tm ) {
145
+ update (2 * at + 1 , pos , tl , tm , newValue );
146
+ // The point index `pos` is contained within the right segment [tm+1, tr]
150
147
} else {
151
- update (2 * at + 2 , to , tm + 1 , tr , newValue );
148
+ update (2 * at + 2 , pos , tm + 1 , tr , newValue );
152
149
}
153
150
// Re-compute the segment value of the current segment on the callback
154
151
t [at ] = t [2 * at + 1 ] + t [2 * at + 2 ];
@@ -164,15 +161,5 @@ public static void main(String[] args) {
164
161
System .out .println (st .sumQuery (1 , 1 ));
165
162
System .out .println (st .sumQuery (0 , 1 ));
166
163
System .out .println (st .sumQuery (0 , 2 ));
167
-
168
- // for (int i = 1; i < 500; i++) {
169
- // // System.out.println();
170
- // int[] values = new int[i];
171
- // java.util.Arrays.fill(values, 1);
172
- // RangeQueryPointUpdateSegmentTree st = new RangeQueryPointUpdateSegmentTree(values);
173
- // }
174
- // for (int i = 1; i < 20; i++) {
175
- // System.out.printf("%d -> %d\n", i, nextPowerOf2(i));
176
- // }
177
164
}
178
165
}
0 commit comments