


1. UPDATE_INDEX(IND, VAL) - It updates the value of ARR[IND] to VAL.
2. SUM\_OF\_RANGE(l, r) - It returns the sum of the subarray ARR[l] to ARR[r] i.e. ARR[l] + ARR[l+1] + ARR[l+1] + ….. + ARR[r-1] + ARR[r].
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains integer ‘N’, which denotes the number of elements in the array.
The second line contains ‘N’ integers denoting the elements of the array ‘ARR’.
The third line of each test case contains integer ‘Q’, which denotes the number of queries for the test case.
For each query, the first line contains an integer ‘TYPE_OF_QUERY’.
If ‘TYPE_OF_QUERY’ is equal to 0, the next line contains two integers ‘IND’, and ‘VAL’, which denotes the index to be updated and the value with which the index is to be updated.
If ‘TYPE_OF_QUERY’ is equal to 1, the next line contains two integers ‘l’, and ‘r’, denoting the leftmost element of the sub-array and the rightmost element of the sub-array.
For each test case, If ‘TYPE_OF_QUERY’ is equal to 1, return the sum of the given range. Otherwise, update accordingly.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= Q <= 10^4
1 <= N <= 10^4
-10^3 <= ARR[i] <= 10^3
TYPE_OF_QUERY = {0, 1}
0 <= IND <= N
-100 <= VAL <= 100
0 <= l <= r <= N
Time Limit: 1 sec
The idea is to iterate through each subarray to find the sum.
For the update query, simply update the value at the given index.
For the sum query, iterate through the given subarray and add all the given indices.
The steps are as follows:
Update(‘IND’, ‘VAL’):
SUM_OF_RANGE(‘l’, ‘r’):
The idea is to divide the given array into blocks of the size square root of ‘N’, where ‘N’ is the size of the given array. The number of elements in each block is the square root of ‘N’ and the number of blocks is equal to the square root of ‘N’. This technique is known as square root decomposition.
For the queries,
Define a class ‘SQRT_DECOMPOSITION':
The idea is to pre-process the given array and break it down and represent it in the form of a tree.
For the update query, a subtree of the tree will be modified.
For the sum query, we will iterate over the subtrees and the sum will be calculated.
The data structure which provides these functionalities is known as a segment tree. It is also used for a number of queries like finding the minimum, maximum, GCD, etc in log(N) time.
How to create the segment tree?
The segment tree is represented in the form of an array. Each node contains information answers for a range or subarray. For any index ‘i’(not a leaf node), its children are at index 2*i and 2*i + 1 respectively.
For the queries,
Define a class segment tree:
Declare an array segmentTree of size 4*N as 4*N size is enough to store all the nodes.
The bottom-up approach is used to build a segment tree. An iterative approach can also be used but generally, the bottom-up approach is used. It takes O(N) time.
Define a recursive function 'BUILD_SEGMENT_TREE', that takes 3 arguments - 'CURRENT_INDEX', 'LOW', 'HIGH' that denotes the index at which we are storing the sum of the range 'LOW' to 'HIGH', and 'LOW' and 'HIGH' are the ranges of the indexes of the current subtree respectively.
The steps are as follows:
Define a function 'UPDATE_SEGMENT_TREE' that takes 5 arguments - 'INDEX', 'VAL', 'CURRENT_INDEX', 'LOW', and 'HIGH', that denotes the index to update, value to update in the index, the current node of the tree, and 'LOW' and 'HIGH' are the ranges of the indexes of the current subtree respectively which returns an integer value for the index 'INDEX'.
The steps are as follows:
Define a function FIND_SUM_OF_RANGE that takes five arguments - 'INDEX', 'LOW', 'HIGH', ‘REQUIEDL’, ‘REQUIEDH’ which denotes the current index, lowest and highest index of the current subtree, and lowest and highest index for the range sum.
The steps are as follows:
Ninja and Time
Ninja and Time
Ninja and Time
Ninja and Time
Ninja and Time
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Range Minimum Query
Range Minimum Query
Range Minimum Query
Range Minimum Query
Ninja and Tree
Distinct Queries on Tree