4
4
*
5
5
* <p>Main inspiration: https://cp-algorithms.com/data_structures/sparse-table.html
6
6
*
7
+ * <p>Tested against: https://www.spoj.com/problems/RMQSQ
8
+ *
7
9
* <p>To run this file:
8
10
*
9
11
* <p>./gradlew run -Pmain=com.williamfiset.algorithms.datastructures.sparsetable.SparseTable
@@ -168,6 +170,7 @@ private long sumQuery(int l, int r) {
168
170
for (int p = P ; p >= 0 ; p --) {
169
171
int rangeLength = r - l + 1 ;
170
172
if ((1 << p ) <= rangeLength ) {
173
+ System .out .printf ("[%d, %d)\n " , l , l + (1 <<p ));
171
174
sum += dp [p ][l ];
172
175
l += (1 << p );
173
176
}
@@ -191,16 +194,50 @@ private long query(int l, int r, BinaryOperator<Long> fn) {
191
194
/* Example usage: */
192
195
193
196
public static void main (String [] args ) {
197
+ example3 ();
198
+ }
199
+
200
+ private static void example1 () {
194
201
long [] values = {2 , -3 , 4 , 1 , 0 , -1 , -1 , 5 , 6 };
195
202
196
203
// Initialize sparse table to do range minimum queries.
197
- SparseTable sparseTable = new SparseTable (values , Operation .MIN );
204
+ SparseTable sparseTable = new SparseTable (values , SparseTable . Operation .MIN );
198
205
199
206
// Prints: "Min value between [2, 7] = -1"
200
207
System .out .printf ("Min value between [2, 7] = %d\n " , sparseTable .query (2 , 7 ));
201
208
202
209
// Prints: "Index of min value between [2, 7] = 5". Returns the leftmost index in the
203
210
// event that there are duplicates.
204
- System .out .printf ("Index of min value between [2, 7] = %d\n " , sparseTable .queryIndex (2 , 7 ));
211
+ System .out .printf ("Index of min value between [2, 7] = %d\n " , sparseTable .queryIndex (2 , 7 ));
212
+ }
213
+
214
+ private static void example2 () {
215
+ long [] values = {4 , 2 , 3 , 7 , 1 , 5 , 3 , 3 , 9 , 6 , 7 , -1 , 4 };
216
+ System .out .println (values .length );
217
+
218
+ // Initialize sparse table to do range minimum queries.
219
+ SparseTable sparseTable = new SparseTable (values , SparseTable .Operation .MIN );
220
+
221
+ // Prints: "Min value between [2, 7] = -1"
222
+ System .out .printf ("Min value between [2, 7] = %d\n " , sparseTable .query (2 , 7 ));
223
+
224
+ // Prints: "Index of min value between [2, 7] = 5". Returns the leftmost index in the
225
+ // event that there are duplicates.
226
+ System .out .printf ("Index of min value between [2, 7] = %d\n " , sparseTable .queryIndex (2 , 7 ));
227
+ }
228
+
229
+ private static void example3 () {
230
+ long [] values = {4 , 2 , 3 , 7 , 1 , 5 , 3 , 3 , 9 , 6 , 7 , -1 , 4 ,1 ,1 ,1 ,1 ,2 ,1111 ,1 ,1 };
231
+ System .out .println (values [17 ]);
232
+
233
+ // Initialize sparse table to do range minimum queries.
234
+ SparseTable sparseTable = new SparseTable (values , SparseTable .Operation .SUM );
235
+
236
+ // Prints: "Min value between [2, 7] = -1"
237
+ System .out .printf ("Min value between [5, 17] = %d\n " , sparseTable .query (5 , 17 ));
238
+
239
+ // Prints: "Index of min value between [5, 17] = 5". Returns the leftmost index in the
240
+ // event that there are duplicates.
241
+ // System.out.printf("Index of min value between [5, 17] = %d\n", sparseTable.queryIndex(2, 7));
205
242
}
206
243
}
0 commit comments