NOTE : Knowledge of Binary Indexed Trees is a prerequisite.
Problem Statement
Assume we need to solve the following problem. We have an array, A of length N with only non-negative values. We want to perform the following operations on this array:
Update value at a given position
Compute prefix sum of A upto i, i ≤ N
Search for a prefix sum (something like a
lower_bound
in the prefix sums array of A)
Basic Solution
Seeing such a problem we might think of using a Binary Indexed Tree (BIT) and implementing a binary search for type 3 operation. Its easy to see that binary search is possible here because prefix sums array is monotonic (only non-negative values in A).
The only issue with this is that binary search in a BIT has time complexity of O(log2(N)) (other operations can be done in O(log(N))). Even though this is naive,
Most of the times this would be fast enough (because of small constant of above technique). But if the time limit is very tight, we will need something faster. Also we must note that there are other techniques like segment trees, policy based data structures, treaps, etc. which can perform operation 3 in O(log(N)). But they are harder to implement and have a high constant factor associated with their time complexity due to which they might be even slower than O(log2(N)) of BIT.
Hence we need an efficient searching method in BIT itself.
Efficient Solution
We will make use of binary lifting to achieve O(log(N)) (well I actually do not know if this technique has a name but I am calling it binary lifting because the algorithm is similar to binary lifting in sparse tables).
What is binary lifting?
In binary lifting, a value is increased (or lifted) by powers of 2, starting with the highest possible power of 2, 2ceil(log(N)), down to the lowest power, 20.
So, we initialize the target position, pos = 0
and also maintain the corresponding prefix sum
. We increase (or lift) pos when the v
Implementation :
// This is equivalent to calculating lower_bound on prefix sums array
// LOGN = log(N)
int bit[N]; // BIT array
int bit_search(int v)
{
int sum = 0;
int pos = 0;
for(int i=LOGN; i>=0; i--)
{
if(pos + (1 << i) < N and sum + bit[pos + (1 << i)] < v)
{
sum += bit[pos + (1 << i)];
pos += (1 << i);
}
}
return pos + 1;
}
Taking this forward
You must have noted that proof of correctness of this approach relies on the property of the prefix sums array that it monotonic. This means that this approach can be used for with any operation that maintains the monotonicity of the prefix array, like multiplication of positive numbers, etc.