Given an array A of N integers. You have to answer two types of queries :
1. Update [l, r] – for every i in range from l to r update Ai with D(Ai), where D(Ai) represents the number of divisors of Ai
2. Query [l, r] – calculate the sum of all numbers ranging between l and r in array A.
Input is given as two integers N and Q, representing number of integers in array and number of queries respectively. Next line contains an array of n integers followed by Q queries where ith query is represented as typei, li, ri.
Input : 7 4 6 4 1 10 3 2 4 2 1 7 2 4 5 1 3 5 2 4 4 Output : 30 13 4
Explanation : First query is to calculate the sum of numbers from A1 to A7 which is 6 + 4
+ 1 + 10 + 3 + 2 + 4 = 30. Similarly, second query results into 13. For third query,
which is update operation, hence A3 will remain 1, A4 will become 4 and A5 will become 2.
Fourth query will result into A4 = 4.
Naive Approach :
A simple solution is to run a loop from l to r and calculate sum of elements in given range. To update a value, precompute the values of number of divisors of every number and simply do arr[i] = divisors[arr[i]].
Efficient Approach :
The idea is to reduce the time complexity for each query and update operation to O(logN). Use Binary Indexed Trees (BIT) or Segment Trees. Construct a BIT array and have two functions for query and update operation and precompute the number of divisors for each number. Now, for each update operation the key observation is that the numbers ‘1’ and ‘2’ will have ‘1’ and ‘2’ as their number of divisors respectively, so if it exists in the range of update query, they don’t need to be updated. We will use a set to store the index of only those numbers which are greater than 2 and use binary search to find the l index of the update query and increment the l index until every element is updated in range of that update query. If the arr[i] has only 2 divisors then after updating it, remove it from the set as it will always be 2 even after any next update query. For sum query operation, simply do query(r) – query(l – 1).
30 13 4
Time Complexity for answering Q queries will be O(Q * log(N)).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest interval in an Array that contains the given element X for Q queries
- Interval Tree using GNU Tree-based container
- Range Sum Queries and Update with Square Root
- Range and Update Sum Queries with Factorial
- Sum of even values and update queries on an array
- Perform append, update, delete and range sum queries on the given array
- Range Sum and Update in Array : Segment Tree using Stack
- Binary Indexed Tree : Range Update and Range Queries
- MongoDB Python | Insert and Update Data
- Range and Update Query for Chessboard Pieces
- Queries to find kth smallest element and point update : Ordered Set in C++
- Queries to update a given index and find gcd in range
- Rearrange and update array elements as specified by the given queries
- How to update single and multiple documents in MongoDB using Node.js ?
- Querying maximum number of divisors that a number in a given range has
- Index of kth set bit in a binary array with update queries
- Difference Array | Range update query in O(1)
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Multiplication on Array : Range update query in O(1)
- Python: Update Nested Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.