RMQSQ - Range Minimum Query
You are given a list of N numbers and Q queries. Each query is specified by two numbers i and j; the answer to each query is the minimum number between the range [i, j] (inclusive).
Note: the query ranges are specified using 0-based indexing.
The first line contains N, the number of integers in our list (N <= 100,000). The next line holds N numbers that are guaranteed to fit inside an integer. Following the list is a number Q (Q <= 10,000). The next Q lines each contain two numbers i and j which specify a query you must answer (0 <= i, j <= N-1).
For each query, output the answer to that query on its own line in the order the queries were made.
1 4 1
Algo - Segment tree
Yeah exactly Errichto's video on sparse table is excellent. Done with sparse table.
Solved using Square Root Decomposition, Segment tree and Sparse Table.
0.13 sec using sparse table and 0.07 using segment tree. Using segment tree gives better result as Q<N here. Although answering query takes O(1) in sparse table but building it takes o(nlog(n)) so overal complexity is O(nlog(n) +Q) for sparse table. But for segment tree building the tree takes O(4n) and answering each query takes O(log(n)) so overall O(n+Qlog(n)). Since Q<N so segment tree gives better time. And for space no doubt segment tree is better as it takes linear space. So my sparse table took 13 MB and segment tree only 5.3 MB. So better use segment tree if query<Size of array.
O(nlgn) preprocessing and O(1) query time using sparse Table :D
Tried in 3 Ways .
Those getting wrong answer at case 5 using SQRT decomposition just check if the left and right query both belongs to same block or not.
I tried solving it using square root decomposition & got Wrong Answer. Pls tell me where I'm wrong.
Anyone is struggling in this problem should watch Erricto's video. Much more detailed way of explanation using Sparse Table