My approach was to visit all inversion count pair and count how many subarrays these pair contribute. Visiting every pair requires \$\mathcal{O}(n^2)\$ time, but I want an optimized version of this, something like \$\mathcal{O}(n \log n)\$.
Can I do something with the Fenwick tree? Inversion in an array Fenwick tree
e.g. if arr = [1,3,4,2]
, then total inversion count for all subarrays = 5
.
number = int(input("Input number := "))
main_list = list(map(int,input().split()))
answer = 0
for i in range(number):
for j in range(number):
if i<j and main_list[i]>main_list[j]:
k = (i+1) * (number-j)
answer += k
print(answer)
i < j
why not put j inrange(i + 1, n)
? \$\endgroup\$n
tonumber
in several places, butk = (i+1) * (n-j)
remained unchanged. It also changedl
tomain_list
, but didn't change all occurrences, and mistyped one occurrence asmail_list
. Moreover, it changed spacing (a PEP review comment) and added a prompt to aninput()
that never existed. The edit should never have been approved. \$\endgroup\$