MO Algorithm question. Help required!

Question link: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/sherlock-and-inversions/

Solution Link : https://ideone.com/XoFsHQ

Getting TLE!
I have applied mo’s Algorithm and segment tree.
How to optimize it further??

The best possible solution is to use iterative segment tree. I modified your code for it and it passed under the time limit .

link for


(https://ideone.com/j7kD2a)
2 Likes

Instead of segment tree use BIT/Fenwick tree

MY AC CODE https://ideone.com/kC1nTX