Given a size n in which initially all elements are 0. The task is to perform multiple multiple queries of following two types. The queries can appear in any order.
- toggle(start, end) : Toggle (0 into 1 or 1 into 0) the values from range ‘start’ to ‘end’.
- count(start, end) : Count the number of 1’s within given range from ‘start’ to ‘end’.
Input : n = 5 // we have n = 5 blocks toggle 1 2 // change 1 into 0 or 0 into 1 Toggle 2 4 Count 2 3 // count all 1's within the range Toggle 2 4 Count 1 4 // count all 1's within the range Output : Total number of 1's in range 2 to 3 is = 1 Total number of 1's in range 1 to 4 is = 2
A simple solutionfor this problem is to traverse the complete range for “Toggle” query and when you get “Count” query then count all the 1’s for given range. But the time complexity for this approach will be O(q*n) where q=total number of queries.
An efficient solution for this problem is to use Segment Tree with Lazy Propagation. Here we collect the updates until we get a query for “Count”. When we get the query for “Count”, we make all the previously collected Toggle updates in array and then count number of 1’s with in the given range.
Below is the implementation of above approach:
This article is contributed by Shashank Mishra ( Gullu ). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Binary array after M range toggle operations
- Array range queries over range queries
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Queries for decimal values of subarrays of a binary array
- Index of kth set bit in a binary array with update queries
- Binary Array Range Queries to find the minimum distance between two Zeros
- Range Update Queries to XOR with 1 in a Binary Array.
- Binary Indexed Tree : Range Updates and Point Queries
- Binary Indexed Tree : Range Update and Range Queries
- Array range queries to count the number of Fibonacci numbers with updates
- Count of divisors of product of an Array in range L to R for Q queries
- Queries for Count of divisors of product of an Array in given range | Set 2 (MO's Algorithm)
- Array Range Queries to count Powerful numbers with updates
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Queries to count array elements greater than or equal to a given number with updates
- Queries for count of array elements with values in given range with updates
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Find the Initial Array from given array after range sum queries