I am trying to learn Mo’s algorithm and am trying to solve various problems with it but as of now failing badly. The problem I am trying is Powerful Arrays from Codeforces. I am pretty sure the query is sorted correctly but its still giving me TLE verdict. How can I optimize it to run quicker. The submission in question: Submission.
Nope ,it should start from right. (I dont know if its in ur case as it depends on question) Assume that the first query is (2,4) so by moving left pointer first u actually remove the things that were never added which can cause problems in most of sums.Thats why we shoupd move right ,then left.