Need help with the problem uva-11402 ahoy pirates

https://ideone.com/3iafjF
I am getting tle with this solution. I have tried all optimizations possible. The only thing that bugs me is my push function. Still I think push functions worst complexity should be n^2 where n is the number of queries (<= 1000). There are other solutions (with square root decomposition etc) but I don’t understand why this code won’t pass.