
1. “index” “value” in this query, set the value of arr[index] to value.
2. ‘L’ ‘R’ in this query, you must find i and j such that L is less than or equal to i, and j is less than or equal to R, and i is not equal to j, such that the sum of arr[i] and arr[j] is maximized. Then, print the sum of arr[i] and arr[j].
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘Q’ denoting the number of elements in the array and number of queries respectively.
The second line of each test case contains ‘N’ space-separated integers, denoting the elements in the array “Arr”.
The next ‘Q’ lines contain three space-separated integers.
If the first integer is 1, Denote type 1 query, next 2 space-separated integers denote “index” and “value”.
If the first integer is 2, Denote type 2 query, next 2 space-separated integers denote ‘L’ and ‘R’.
For each test case, return the list of integers, where the ith of them denotes the answer of the ith type 2 query.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 100000
1 <= Q <= 100000
1 <= Arr[i], value < 10^9
1 <= index, L < R <= N
Where ‘T’ denotes the number of test cases,
‘N’ denotes the number of elements in the array,
‘Q’ denotes the number of queries,
“value” and “index” denote index and value in the query of type 1,
‘L’ and ‘R’ denote the range limits in the query of type 2.
Time Limit: 1 sec
For each query of type 1, we can simply set Arr[index] = value.
For each query of type 2, we can simply iterate between ‘L’ and ‘R’ and store first highest in “maximum1” and second highest in “maximum2”. If the value Arr[i] is greater or equal to “maximum1” then do “maximum2” = “maximum1” and “maximum1” = Arr[i].
A Segment Tree is a data structure that allows us to efficiently perform range queries and update queries. Here in this problem in the segment tree, at any node, we store the two highest values of all the leaf nodes present in the subtree rooted at that node at any ode. Similarly, for maximum segment trees.
The first step in this approach is to build the segment tree in which each node stores the two highest values of all the leaf nodes present in the subtree rooted at that node.
Once we have built a segment tree, for each query of type 1, we will perform an update query in the segment tree that will set “value” at the leaf node that represents Arr[index]. For each query of type 2, we will perform a range query in the range ‘L’ to ‘R’ that will give us the two highest values in that range. Store the result of each query of type 2 in the array “answer” and return the array “answer”.
Algorithm:
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