Why is this approach giving WA while the other one is not ?

Have a look at this code.
Then at this code.

Can somebody please help me figure out why is my approach giving WA and the other one is not? This problem disturbed me a lot during the contest.

i dont know if i am answering this correctly but from what i see is that you have used cin and cout in ur code. try using scanf and printf as they are considerably faster…i remember a problem BINTREE in apr challenge where i used cin and cout and it took 10 sec to run…when i changed it to scanf and printf it took just 1.2 s…maybe it helps

You might have noticed that i am using a structure for IO. This structure is used to speed up input and output operation. In BINTREE without this structure i got 10.5sec and with this structure i got 0.5sec!!

//