![]() |
VOOZH | about |
Given an array arr[] consisting of N 0s and a 2D array Q[][] consisting of queries of the following two types:
Input: arr[] = { 0, 0, 0, 0, 0 }, Q[][] = { { 1, 0, 2, 100 }, { 2, 1 }, { 1, 2, 3, 200 }, { 2, 2 }, {
4 } }
Output: 100 300 0
Explanation:
Query1: Incrementing all the array elements in the range [0, 2] by 100 modifies arr[] to { 100, 100, 100, 0, 0 }.
Query2: Print arr[1](=100)
Query3: Incrementing all the array elements in the range [2, 3] by 200 modifies arr[] to { 100, 100, 300, 200, 0 }.
Query4: Print arr[2](=300)
Query5: Print arr[4](=0)Input: arr[] = { 0, 0 }, Q[][] = { { 1, 0, 1, 100 }, { 2, 1 } }
Output: 100
Approach: The idea is to use the concept of Segment Tree to perform queries of type 2 and the difference array to perform queries of type 1. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Element at 1 is 100
Time Complexity: O(|Q| * log(N))
Auxiliary Space: O(N)