*You are given a binary array consists of 0's and 1's , and q queries . q can be large !*

*in each query you are given a certain range [L , R].*

suppose a[] = {1,0,0,1}

L = 1 , R = 3 .

do toggling , resultant array = {0,1,1,1}

L = 1 , R =4

count all one's , ans = 3.

you have to either toggle bits in the given range i.e make 0 = 1 and 1 = 0 .

and on another query you are about to count all 1's in the range of [L,R]. **** **The problem gives a feel of segment tree + lazy propogation . but how to do toggling in segment tree .**

how should we update the lazy tree !

Any idea !

Just use the trick that inverting a bit

xcan be done by taking -x+ 1.hey .. sorry but i couldnt understand a single thing .. it would be very helpful if u elaborate it quite a bit.

What I am saying is that toggling a bit is the same thing as multiplying it with -1 and then adding 1. The nice thing about this is that there are lazy segment trees that support doing addition and multiplication and sum/max/min on intervals, so for me solving this problem would just be applying a very general lazy segment tree.

I think that if you want to learn lazy segment trees then first try to implement a general lazy segment tree that support addition, multiplication, sum/min/max. After that you can solve nearly all segment tree problems using that one tree, so it is quite useful.

Link

i have seen it before posting. but how to use segment tree with lazy is not explaimed clearly there. if u could understand please let me know .

Lazy tags store if each range should be toggled. When a range is toggled, set

`tree[node] = en-st+1 - tree[node];`

which uses the fact that toggling a bit value x is the same as doing`x=1-x`

.so segment tree is storing the count of 1's in the range [l,r] ? and when we do lazy propogation , what value will we propogate ?

yes

I wrote a code for a similar problem. https://pastebin.com/R7iqy1sq Reply if you don't understand.