using upper_bound I have used to calculate position of first element greater than i 'th element ,thus calculating number of voilations by i 'th element, than inserting it in position of first element greater than i 'th element.
This method is not supposed to work.It has a complexity of O(n^2).The test cases for the problem are bad
It should not be the best way to do it as mentioned by @ranjith1604. But the mistake in your solution is an integer overflow. You can read more about it here.