LearnPy Hub
← Arena
AdvancedRating 1700

Count Inversions Fast

Count the pairs i < j with aᵢ > aⱼ. Use merge sort or a Fenwick tree so that n up to 10⁵ is fine.

Input

n, then n integers.

Output

One integer.

Sample 1

Input
5
2 4 1 3 5
Output
3

Sample 2

Input
5
5 4 3 2 1
Output
10
solution.py
Output appears here
Count Inversions Fast · Python Arena problem · LearnPy Hub