FLIPCOIN lazy propagation help

can anyone tell me why this approach is giving me WA CodeChef: Practical coding for everyone
while this approach is giving me AC CodeChef: Practical coding for everyone
why the first approach is wrong thanks in advance
question link FLIPCOIN Problem - CodeChef

It seems that you have assumed coins are only flipped once from tails to heads. But a coin that has been flipped to heads can be flipped again, which makes it tails. Of course it can be flipped again in the future. Your lazy propagation should be designed to handle that.

@manaranjanfav

[Here’s][1] your code in which i made the required changes of 2 lines in update and query part each and got AC.

Basically what i did was, changed from

if(start!=end) 
     {
      lazy[2*index+1]=1;
      lazy[2*index+2]=1; 
     }

to

if(start!=end) 
     {
      lazy[2*index+1]=!lazy[2*index+1];
      lazy[2*index+2]=!lazy[2*index+2]; 
     }

Because in your code you are always assigning 1 to the child nodes while they can also be 0(head or tail and not only head or only tail) so i am using ! operator as,
!1 = 0 and !0 = 1 .

Upvote if this solves your query.
[1]: CodeChef: Practical coding for everyone

1 Like

hellow @meooow bhaiya can you explain where i am not flipping the heads to tail