help in optimizing code...

question : BGQRS Problem - CodeChef

my solution : (N * sqrt(N)) : CodeChef: Practical coding for everyone

my solution was failing only the last case…i know that manyone have solved this using sqrt decomposition, so, it’s possible but i cannot able to optimize it further can anyone please help me !

Square root decomposition wasnt the intended solution and author wanted it to fail

However, one of the square root decomposition solution of @pkacpraz did pass. You can see his explantion here -chasethered.com