Binary Indexed Tree Range Updates and Point QueriesIntroduction:In this article, we are going about the Range Updates and Point Queries of Binary Indexed Tree. But before that, we have to know what a binary index tree is. We can say that a binary index tree is a type of data structure that helps us store the data in the form of an array. With the help of this, we can calculate the Sum of the prefix of the array element. In this data structure, we can represent all the positive numbers in the form of a Sum of power of 2. Now, let's take an example and understand the range update and range queries. Let's say we have an array that contains an N integer. By taking this array, we have to perform the operation below.
In this section, we have to perform the update operation of the array element which have the index, and the range of the index starts from L to R. After that, we have to add an x element to each element whose index range starts from L to R. This operation is known as the Range update.
In this section, we have to find the Sum of the index of the array element whose range starts from L to R. This is an example of a range query. Now, we are going to perform the above two operations with the help of a binary-indexed tree. We have the array, which contains n number of elements. All the elements present in this array have the initial value of 0. Q represents the Number of queries. Solution ApproachIn this section, we are going to solve the update and range queries in the binary-indexed tree. We can solve the range sum query with the help of the Sum of the prefixes. Let's say we have to find the Sum of an array whose range starts from L to R. We can find this Sum by subtracting the prefix sum [0, L-1] from the prefix sum [0, R]. We can calculate the prefix by taking the help of binary indexed trees in O(log N) time. Suppose there is a range update query whose range is from L to R, and we have to find the Sum of the prefix whose range is from 0 to M. Case-1: In this case, we have to find the Sum that may not affect the range by updating the range from L to R. Case-2: In this case, we have to increase the Sum of the range to (x*M - x*(L-1)). Then, we have to update the range from L to R. Case-3: In this case, we have to increase the Sum of the range to (x*R - x*(L-1)). Then, we have to update the range from L to R. Algorithm-To solve this problem, we have to follow the below steps. These are as follows. Step-1: First, we have to write the main function. Inside that main function, we have to create the variable that stores the array and the Number of elements present in that array. After that, we have to create two binary indexes, and then we have to initialize that variable to zero. Step-2: Now we have to call "rangeUpdateQuery()" and "rangeSumQuery()" functions based on the type of query. Step-3 Then, we have to create the function rangeSumQuery() that accepts two parameters, such as binary indexes of the binary tree. This functions the Sum of the element of the array whose ranges from L to R. Inside that function, we have perform prefix sum [0, R] and [0, L-1] and subtract the prefix sum [0, L-1] from the prefix sum [0, R] to get the required Sum of the range [L, R]. Step-4 Then we have to create rangeUpdateQuery(), which updates both the binary indexed tree such that one binary indexed tree can be used to get the value at any index, and the other can be used to get the value of (x*(L-1)) after each update query. Implementation in C++ code:Code: Output: Explanation: In the above code, we have created two functions such as rangeUpdateQuery() and rangeSumQuery(), with the help of C++ code. This code helps us to solve the above-described problem. Implementation in Java:Code: Output: Explanation: In the above code, we have implemented two methods in Java, such as update() and getElement(). With the help of these two methods, we can update the index of the array, and we can also fetch the element present in the updated index value. Algorithm Complexity: Time complexity: The time complexity for above approach is O(Q * log(N)) Here, 'Q' and 'N' are the Number of queries and the Number of elements in the array. Space complexity: The time complexity of the above problem is O(N). Here, 'N' is the total number of elements in the given array. |