Input: N = 5, arr[] = {1, 420, 69, 1434, 2023}, Q = 8, queries[][] = [[1, 1, 2], [2, 1], [2, 2], [2, 3], [1, 1, 4], [2, 0], [2, 2], [2, 4]]
Output: [6, 15, 1434, 1, 6, 7]
Explanation: In the beginning, arr[] = [1, 420, 69, 1434, 2023]
- 1st query is [1, 1, 2] means operation 1 is used so arr[1] = 4 + 2 + 0 = 6 and arr[2] = 6 + 9 = 15, after updating array arr = [1, 6, 15, 1434, 2023]
- 2nd query is [2, 1] means operation 2 is used and we need to print arr[1] = 6
- 3rd query is [2, 2] so, arr[2] = 15
- 4th query is [2, 3] so, arr[3] = 1434
- 5th query is [1, 1, 4] so, after updating the array arr = [1, 6, 6, 12, 7]
- 6th query is [2, 0] so, arr[0] = 1
- 7th query is [2, 2] so, arr[2] = 6
- 8th query is [2, 4] so, arr[4] = 7
So in the final, we’ll return [6, 15, 1434, 1, 6, 7]
For input arr = [1, 420, 69, 1434, 2023] (N = 5)
Digitsum of every element till it is greater equal to 10
- 1 -> [1]
- 420 -> [420, 6]
- 69 -> [69, 15, 6]
- 1434 -> [1434, 12, 3]
- 2023 -> [2023, 7]
Initially, we will make segment tree for the given array in which the nodes contains value 0.
(1, 0, 4, 0)
/ \
(2, 0, 2, 0) (3, 3, 4, 0)
/ \ / \
(4, 0, 1, 0) (5, 2, 2, 0) (6, 3, 3, 0) (7, 4, 4, 0)
/ \
(8, 0, 0, 0) (9, 1, 1, 0)
Each nodes denotes (node_number, start, end, value), where (start, end) is (0, N-1) at the start
Now, after 1st query that is [1, 1, 2] tree will look like:
(1, 0, 4, 0)
/ \
(2, 0, 2, 0) (3, 3, 4, 0)
/ \ / \
(4, 0, 1, 0) (5, 2, 2, 1) (6, 3, 3, 0) (7, 4, 4, 0)
/ \
(8, 0, 0, 0) (9, 1, 1, 1)
2nd query is [2, 1] so we will traverse the tree from root to the node which has (start, end) equivalent to (1, 1) and calculate the total sum of value. So root to leaf path for (1, 1) from above tree is 1 -> 2 -> 4 -> 9 and sum of its values are: 0 + 0 + 0 + 1 = 1 and for arr[1] = 420, we know digitsum list of 420 that is: [420, 6] so the 1st index value is 6 which we will print.
3rd query is [2, 2] so the path from root to leaf having (start, end) = (2, 2) is: 1 -> 2-> 5 and sum of its values are: 0 + 0 + 1 = 1 and for arr[2] = 69, we know digitsum list of 69 that is: [69, 15, 6] and 1st index of the list is 15 which we will print.
Similarly, for the 4th query which is [2, 3] the root to leaf path will be: 1 -> 3 -> 6 and sum of values are: 0 + 0 + 0 = 0 and for arr[3] = 1434, list of digitsum is:
[1434, 12, 3] in which the 0th index is 1434 which we will print.
For 5th query which is [1, 1, 4] which means we have to increment the values of nodes by 1 which lies under (1, 4) so the tree becomes:
(1, 0, 4, 0)
/ \
(2, 0, 2, 0) (3, 3, 4, 1)
/ \ / \
(4, 0, 1, 0) (5, 2, 2, 2) (6, 3, 3, 0) (7, 4, 4, 0)
/ \
(8, 0, 0, 0) (9, 1, 1, 2)
Now for the 6th query which is [2, 0] the path will be: 1 -> 2 -> 4 -> 8 and the sum of the values are: 0 + 0 + 0 + 0 = 0, and for arr[0] = 1, the digitsum list is: [1], so for 0th index we will print 1.
For the 7th query which is [2, 2] the path will be: 1 -> 2 -> 5 and the sum of the values are: 0 + 0 + 2 = 2, and for arr[2] = 69, the digitsum list is: [69, 15, 6], so for the 2nd index we will print 6.
For the 8th query which is [2, 4], the path will be: 1 -> 3 -> 4 and the sum of the values are: 0 + 1 + 0 = 1, and for the arr[4] = 2023, the digitsum list id: [2023, 7], so for the 1st index we will print 7.