Problem:
The Number of Inversions For a given sequence $A = \{a_0, a_1, ... a_{n-1}\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program: bubbleSort(A)
  cnt = 0 // the number of inversions
  for i = 0 to A.length-1
    for j = A.length-1 downto i+1
      if A[j] < A[j-1]
	swap(A[j], A[j-1])
	cnt++

  return cnt For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded. Input In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters. output Print the number of inversions in a line. Constraints  $ 1 \leq n \leq 200,000$  $ 0 \leq a_i \leq 10^9$  $a_i$ are all different  Sample Input 1 5
3 5 2 1 4 Sample Output 1 6 Sample Input 2 3
3 1 2 Sample Output 2 2

Solution (Python):
cnt = 0
def merge(L,R):
    global cnt
    n = len(L)+len(R)
    lenL = len(L)
    A = []
    i = j = 0
    L.append(-1)
    R.append(-1)
    for _ in range(n):
        if L[i] > R[j]:
            A.append(L[i])
            i += 1
        else:
            A.append(R[j])
            j += 1
            cnt += lenL-i
    return A

def mergeSort(A):
    if len(A)==1: return A
    m = len(A)//2
    return merge(mergeSort(A[:m]),mergeSort(A[m:])) 

if __name__=='__main__':
    n = int(input()) 
    A = list(map(int,input().split()))
    mergeSort(A[::-1])
    print(cnt)

Solution (Python):
def merge(a, left, mid, right):
    global cnt
    l = a[left:mid] + [10**9 + 1]
    r = a[mid:right] + [10**9 + 1]

    i = 0
    j = 0
    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            cnt += len(l) - 1 - i  
            a[k] = r[j]
            j += 1


def mergesort(a, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergesort(a, left, mid)
        mergesort(a, mid, right)
        merge(a, left, mid, right)


n = int(input())
a = list(map(int, input().split()))
cnt = 0
mergesort(a, 0, n)
print(cnt)

Solution (Python):
def merge(targ,first,mid,last):
    left = targ[first:mid] + [10 ** 9 + 1]
    right = targ[mid:last] + [10 ** 9 + 1]

    leftcnt = rightcnt = 0
    global ans
    for i in range(first,last):
        
        if left[leftcnt] <= right[rightcnt]:
            targ[i] = left[leftcnt]
            leftcnt += 1
        else:
            ans += len(left)-1-leftcnt
            targ[i] = right[rightcnt]
            rightcnt += 1

def mergesort(targ,first,last):
    if first + 1 == last:
        if targ[first] > targ[last]:
            
            global ans
            temp = targ[first]
            targ[first] = targ[last]
            targ[last] = temp
            ans += 1

    elif first == last:
        pass
    else:
        mid = (first + last) // 2
        mergesort(targ,first,mid)
        mergesort(targ,mid,last)
        merge(targ,first,mid,last)


ans = 0
num = int(input())
targ = [int(n) for n in input().split(' ')]
mergesort(targ,0,num-1)
print(ans)

Solution (Python):
import sys

sintinel = 1000000001

def merge_sort(A, l, r):
    if l + 1 < r:
        mid = (l + r) // 2
        cnt1 = merge_sort(A, l, mid)
        cnt2 = merge_sort(A, mid, r)
        cnt3 = merge(A, l, mid, r)
        return (cnt1 + cnt2 + cnt3)
    else:
        return 0

def merge(A, l, mid, r):
    n1 = len(A[l:mid])
    L = A[l:mid] + [sintinel]
    R = A[mid:r] + [sintinel]
    i = j = 0
    cnt = 0
    for k in range(l, r):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            cnt += n1 - i
            A[k] = R[j]
            j += 1
    return cnt

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

c = merge_sort(A, 0, n)
print(c)

Solution (Python):
def bubble_sort_revision(target_list):

    list_length = len(target_list)
    
    flag = True
    change_count = 0
    
    for top_index in range(1,list_length):
        for i in range(top_index, list_length)[::-1]:
            if target_list[i] < target_list[i - 1] and target_list[top_index] >= target_list[i]:
                tmp = target_list[i]
                target_list[i] = target_list[i - 1]
                target_list[i - 1] = tmp
                change_count += 1
                
    return change_count

def my_add(bit, i, x):
    while i <= len(bit) - 1:
        bit[i] += x
        i += int(bin(i & -i), 2)
        
def my_sum(bit, i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= int(bin(i & -i), 2)
    return s

def merge(l,r):
    global count
    l_index = 0
    r_index = 0
    merge_list = []
    l.append(pow(10,9) + 1)
    r.append(pow(10,9) + 1)

    len_l = len(l) - 1
    for k in range(len(l) + len(r) - 2):
        if l[l_index] < r[r_index]:
            merge_list.append(l[l_index])
            l_index += 1
            len_l -= 1
        else:
            count += len_l
            merge_list.append(r[r_index])
            r_index += 1

    return merge_list
    
def merge_sort(target_list):
    if len(target_list) == 1:
        return target_list
    
    mid = int(len(target_list) / 2)
    
    r = merge_sort(target_list[mid:])
    l = merge_sort(target_list[:mid])
    
    return merge(l, r)

def bit_solve(n, target_list):
    bit = [0 for i in range(max(target_list) + 2)]
    ans = 0
    for i in range(n):
        ans += i - my_sum(bit, target_list[i] + 1)
        my_add(bit, target_list[i] + 1, 1)
        print(ans, target_list[i], bit)
    return ans

if __name__ == "__main__":
    global count
    count = 0
    l = int(input())
    target_list = [int(i) for i in input().split()]
    merge_sort(target_list)
    print(count)

Solution (Python):
 
def Marge(A,left,mid,right):
    n1=mid-left
    n2=right-mid
    L=[]
    R=[]
    c=0
    for i in range(n1):
        L.append(A[left+i])
    for i in range(n2):
        R.append(A[mid+i])
    L.append(100000000000000000)
    R.append(100000000000000000)
    i=j=0
    for k in range(left,right):
        if L[i]<=R[j]:
            A[k]=L[i]
            i+=1
        else:
            A[k]=R[j]
            j+=1
            c+=n1-i
    return c
            
def MS(A,left,right,c):
    
    if left+1<right:
        mid=int((left+right)/2)
        c1=MS(A,left,mid,c)
        c2=MS(A,mid,right,c)
        c3=Marge(A,left,mid,right)
        return c1+c2+c3
    return 0
 
    
if __name__=="__main__":
    n=int(input())
    A=input().split(" ")
    cnt=0
    for i in range(n):
        A[i]=int(A[i])
    cnt=MS(A,0,n,0)
    print(cnt)

Solution (Python):

def merge(ns, ns1, ns2, n, mid):
    j = 0
    k = 0
    c = 0
    for i in range(n):
        if ns1[j] < ns2[k]:
            ns[i] = ns1[j]
            j += 1
            if j == mid:
                for l in range(n - mid - k):
                    ns[i + 1 + l] = ns2[k + l]
                return c
        else:
            ns[i] = ns2[k]
            k += 1
            c += mid - j
            if k == n - mid:
                for l in range(mid - j):
                    ns[i + 1 + l] = ns1[j + l]
                return c

def merge_sort(ns, n):
    if n <= 1:
        return 0
    elif n == 2:
        if ns[0] > ns[1]:
            k = ns[0]
            ns[0] = ns[1]
            ns[1] = k
            return 1
        else:
            return 0
    else:
        mid = n // 2
        ns1 = ns[:mid]
        ns2 = ns[mid:]
        c1 = merge_sort(ns1, mid)
        c2 = merge_sort(ns2, n - mid)
        c3 = merge(ns, ns1, ns2, n, mid)
        return c1 + c2 + c3

def compute_inversions(ns, n):
    return merge_sort(ns, n)

n = int(input())
ns = list(map(int, input().split()))

print(compute_inversions(ns, n))

Solution (Python):



import sys


def merge(_array, left, mid, right):
    count = 0
    array_left = _array[left:mid] + [1e9 + 1]
    array_right = _array[mid:right] + [1e9 + 1]
    left_length, right_length = mid - left, right - mid

    i, j = 0, 0

    for k in range(left, right):
        if array_left[i] <= array_right[j]:
            _array[k] = array_left[i]
            i += 1
        else:
            _array[k] = array_right[j]
            j += 1
            count += left_length - i
    return count


def merge_sort(_array, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        c1 = merge_sort(_array, left, mid)
        c2 = merge_sort(_array, mid, right)
        c3 = merge(_array, left, mid, right)
        return c1 + c2 + c3
    else:
        return 0


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    

    ans = merge_sort(array, 0, array_length)
    print(ans)

Solution (Python):
from collections import deque
def mergeSort(A,left,right):

    middle = (left + right) // 2
    if left + 1 < right:
        mergeSort(A, left, middle)
        mergeSort(A, middle, right)
        merge(A,left,right,middle)
    
def merge(A,left,right,middle):
    global count
        
    L = deque(A[left:middle])
    R = deque(A[middle:right])
    L.append(float("inf"))
    R.append(float("inf"))
    for i in range(left,right):
        
        if L[0] < R[0]:
            A[i] = L.popleft()
        else:
            A[i] = R.popleft()
            count += len(L) -1 
count = 0     
input()
A = list(map(int,input().split()))
mergeSort(A,0,len(A))
print(count)

Solution (Python):
from _collections import deque

SENIAL = 1000000000
def mergeSort(A,left,right):
    if left + 1 < right:
        middle = (left + right) // 2
        mergeSort(A, left, middle)
        mergeSort(A, middle, right)
        merge(A,left,right,middle)
    
def merge(A,left,right,middle):
    global count        
    L = deque(A[left:middle])
    R = deque(A[middle:right])
    L.append(SENIAL)
    R.append(SENIAL)
    for i in range(left,right):
        if L[0] < R[0]:
            A[i] = L.popleft()
        else:
            A[i] = R.popleft()
            count += len(L) -1 
count = 0     
input()
A = list(map(int,input().split()))
mergeSort(A,0,len(A))
print(count)

Solution (Python):
SENTINEL = 1000000001

def merge(A, l, m, r):
  L = A[l:m] + [SENTINEL]
  R = A[m:r] + [SENTINEL]
  i = 0
  j = 0
  cnt = 0
  for k in range(l, r):
    if L[i] <= R[j]:
      A[k] = L[i]
      i += 1
    else:
      A[k] = R[j]
      j += 1
      cnt += len(L) - 1 - i
  return cnt

def merge_sort(A, l, r):
  if l + 1 < r:
    m = (l + r) // 2
    cnt = 0
    cnt += merge_sort(A, l, m)
    cnt += merge_sort(A, m, r)
    cnt += merge(A, l, m, r)
    return cnt
  else:
    return 0

n = int(input())
A = list(map(int, input().split()))

print(merge_sort(A, 0, n))

Solution (Python):
import sys

a_max = 10000000000

def merge_sort(A):
	n = len(A)
	num_swap = 0

	if n > 1:
		m = n // 2
		num_swap0, A0 = merge_sort(A[0:m])
		num_swap1, A1 = merge_sort(A[m:])
		
		num_swap += num_swap0 + num_swap1

		n1 = len(A0)
		A0.append(a_max)
		A1.append(a_max)	
		i = 0
		j = 0		
		
		for k in range(n):
			if A0[i] < A1[j]:
				A[k] = A0[i]

				i += 1
			else:
				A[k] = A1[j]

				num_swap += n1 - i
				j += 1

	return num_swap, A

def bubble_sort(A):
	n = len(A)
	num_swap = 0

	for i in range(n):
		for j in range(n - 1, i, -1):
			if A[j] < A[j - 1]:
				A[j], A[j - 1] = A[j - 1], A[j]
				num_swap += 1

	return num_swap, A


fin = sys.stdin

fin.readline()
A = list(map(int, fin.readline().split()))

num_swap, B = merge_sort(A)


print(num_swap)

Solution (Python):
def merge(A, left, mid, right):

    n1 = mid-left
    n2 = right-mid
    
    
    
    
    
    
    L = A[left:mid]
    l = len(L)
    R = A[mid:right]
    L.append(float('inf'))
    R.append(float('inf'))
    i = 0
    j = 0
    global cnt

    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i = i+1
        
        else:
            A[k] = R[j]
            j = j+1
            cnt += l-i

def mergeSort(A, left, right):
    if left+1 < right:
         mid = (left+right) // 2
         mergeSort(A, left, mid)
         mergeSort(A, mid, right)
         merge(A, left, mid, right)
    else:
        return 0

n = input()
A = list(map(int, input().split()))
cnt = 0
mergeSort(A, 0, len(A))

print(str(cnt))

Solution (Python):
count = 0

def merge(A, left, mid, right):
  global count
  n1 = mid - left
  n2 = right - mid
  L = A[left:left+n1]
  R = A[mid:mid+n2]
  L.append(float("inf"))
  R.append(float("inf"))
  i = 0
  j = 0
  numLeft = len(L)-1 
  for k in range(left, right):
    if R[j] < L[i]:
      A[k] = R[j]
      j += 1
      count += numLeft-i 
    else:
      A[k] = L[i]
      i += 1

def mergeSort(A, left, right):
  if left + 1 < right:
    mid = int((left + right)/2)
    mergeSort(A, left, mid)
    mergeSort(A, mid, right)
    merge(A, left, mid, right)


def main():
  n = int(input())
  A = list(map(int, input().split()))
  mergeSort(A, 0, n)
  print(count)

if __name__ == "__main__":
  main()

Solution (Python):
def merge(A, left, mid, right):
    global cnt
    L = A[left:mid] + [1000000001]
    R = A[mid:right] + [1000000001]
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            cnt += j
            i += 1
        else:
            A[k] = R[j]
            j += 1
 
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)

n = int(input())
A = list(map(int, input().split()))
cnt = 0
mergeSort(A, 0, n)
print(str(cnt))

Solution (Python):
import sys

def solve():
    n = int(sys.stdin.readline().rstrip())
    a = list(map(int, sys.stdin.readline().split()))
    b = compress(a)

    ft = FenwickTree(max(b) + 1)
    ans = 0

    for bi in reversed(b):
        ans += ft.psum(bi + 1)
        ft.add(bi, 1)

    print(ans)

def compress(a):
    n = len(a)
    a = [(ai, i) for i, ai in enumerate(a)]
    a.sort()

    num = -1
    p = -1

    b = [None] * n

    for val, idx in a:
        if val > p:
            num += 1

        b[idx] = num
        p = val

    return b

class FenwickTree:
    def __init__(self, n):
        self.n = n
        self.b = [0] * (n + 1)

    def add(self, i, x):
        i += 1

        while i <= self.n:
            self.b[i] += x
            i += i & (-i)

    def psum(self, r):
        res = 0

        while r > 0:
            res += self.b[r]
            r -= r & (-r)

        return res

if __name__ == '__main__':
    solve()

Solution (Python):
import sys

inf = 1<<30
ans = 0

def solve():
    n = int(sys.stdin.readline().rstrip())
    a = list(map(int, sys.stdin.readline().split()))

    MergeSort(a)

    print(ans)

def MergeSort(a):
    if len(a) == 1:
        return a

    apre = MergeSort(a[:len(a)//2])
    asuf = MergeSort(a[len(a)//2:])

    res = merge(apre, asuf)

    return res

def merge(a, b):
    global ans
    na = len(a)
    nb = len(b)

    a.append(inf)
    b.append(inf)

    la = 0
    lb = 0

    res = []

    for i in range(na + nb):
        if a[la] > b[lb]:
            ans += na - la
            res.append(b[lb])
            lb += 1
        else:
            res.append(a[la])
            la += 1

    return res

if __name__ == '__main__':
    solve()

Solution (Python):


import math

def merge(left,right,mid,A):
    global cnt
    n1=mid-left
    n2=right-mid
    L=A[left:mid]
    R=A[mid:right]
    L.append(centi)
    R.append(centi)
    Li,Ri = 0, 0
    for k in range(left,right):
        if L[Li] <= R[Ri]:
            A[k]=L[Li]
            Li+=1
        else:
            A[k] =R[Ri]
            Ri+=1
            cnt += (n1)-Li
            
    
cnt = 0
centi = math.pow(10,10)

def mergesort(left, right , A):
    if left+1 < right:
        mid = (left+right)//2
        mergesort(left,mid,A)
        mergesort(mid,right,A)
        merge(left,right,mid,A)
        
        
Right=int(input())
N=list(map(int,input().split()))

mergesort(0,Right,N)
print(cnt)

Solution (Python):
def merge(A, left, mid, right):
    cnt_local = 0
    leftA = A[left:mid]
    rightA = A[mid:right]
    leftA.append(INF)
    rightA.append(INF)
    l = 0
    r = 0
    for i in range(left, right):
        if leftA[l] <= rightA[r]:
            A[i] = leftA[l]
            l += 1
        else:
            A[i] = rightA[r]
            r += 1
            cnt_local += len(leftA) - l - 1
    return cnt_local


def mergeSort(A, left, right):
    if right - left < 2:
        return 0
    mid = left + (right - left) // 2
    v1 = mergeSort(A, left, mid)
    v2 = mergeSort(A, mid, right)
    v3 = merge(A, left, mid, right)
    return v1 + v2 + v3


if __name__ == "__main__":
    INF = 10000000000
    n = int(input())
    S = list(map(int, input().split()))
    num = 0
    cnt = mergeSort(S, 0, len(S))
    print(cnt)

Solution (Python):
def merge(A, left, mid, right):
    global count
    
    L = A[left:mid] + [int(1e9)]
    R = A[mid:right] + [int(1e9)]
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            count += (mid-left-i)

def merge_sort(A, left, right):
    if left+1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

if __name__ == "__main__":
    n = int(input())
    A = list(map(int, input().strip().split()))
    count = 0
    merge_sort(A, 0, n)
    print(count)

Solution (Python):
def merge(S, left, mid, right):
	global cnt
	L = S[left:mid] + [float('inf')]
	R = S[mid:right] + [float('inf')]
	i,j = 0,0
	for k in range(left,right):
		if L[i] <= R[j]:
			S[k] = L[i]
			i += 1
		else:
			S[k] = R[j]
			j += 1
			cnt += mid - left - i


def merge_sort(S, left = 0, right = 200000):
	right = min(len(S),right)
	if left+1 < right:
		mid = (left+right) // 2
		cnt1 = merge_sort(S, left, mid)
		cnt2 = merge_sort(S, mid, right)
		cnt3 = merge(S, left, mid, right)


if __name__ == '__main__':
	n = int(input())
	A = [int(s) for s in input().split()]
	cnt = 0
	inv = merge_sort(A)
	print(cnt)

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())

count = 0
def merge(A, left, right, mid):
    L = [A[i] for i in range(left, mid)]
    R = [A[i] for i in range(mid, right)]
    L.append(10**9+1)
    R.append(10**9+1)
    i = 0
    j = 0
    global count
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            count += mid - left - i
    
def mergeSort(A, left, right):
    if right > left + 1:
        mid = int((right + left) / 2)
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, right, mid)
        

mergeSort(a, 0, n)
print count

Solution (Python):
MAX = 1000000001

def merge(A, left, mid, right, cnt):
    n1 = mid - left
    n2 = right - mid
    L = A[left:mid]
    R = A[mid:right]
    ls = len(L)
    L.append(MAX)
    R.append(MAX)
    
    i = 0
    j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            cnt += ls - i
            j = j + 1
    return cnt

def mergeSort(A, left, right, cnt):
    if left + 1 < right:
        mid = int((left + right) / 2)
        cnt = mergeSort(A, left, mid, cnt)
        cnt = mergeSort(A, mid, right, cnt)
        cnt = merge(A, left, mid, right, cnt)
    return cnt

def main():
    """ ????????? """
    num = int(input().strip())
    A = list(map(int,input().split()))

    cnt = mergeSort(A, 0, num, 0)
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
def merge(A, l, m, r):
    L = A[l:m] + [10 ** 9 + 1]
    R = A[m:r] + [10 ** 9 + 1]
    i, j, p, q = 0, 0, 0, 0
    for k in range(l, r):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
            q += p
        else:
            A[k] = R[j]
            j += 1
            p += 1
    global cnt
    cnt += q

def mergeSort(A, l, r):
    if r - l > 1:
        m = (l + r) // 2
        mergeSort(A, l, m)
        mergeSort(A, m, r)
        merge(A, l, m, r)

n = int(input())
A = list(map(int, input().split()))
cnt = 0
mergeSort(A, 0, n)
print(cnt)

Solution (Python):
n = int(input())
s = list(map(int,input().split()))
count = 0

def merge(a, left, mid, right):
    global count
    l = a[left:mid] + [float("inf")]
    r = a[mid:right] + [float("inf")]
    i = j = 0
    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1
            count += len(l) - i - 1

def mergeSort(a, left, right):
    if left + 1 < right:
        mid = right + (left - right)//2
        mergeSort(a, left, mid)
        mergeSort(a, mid, right)
        merge(a, left, mid, right)

mergeSort(s, 0, n)

print(count)

Solution (Python):
def count(A,left, right):
    if left >= right:
        return 0
    mid = (left + right) // 2
    cl = count(A, left, mid)
    cr = count(A, mid+1, right)
    L = A[left:mid + 1]
    R = A[mid+1:right+1]
    c, m, n, k= 0, 0, 0, left
    while m < len(L) and n < len(R):
        if L[m] > R[n]:
            A[k] = R[n]
            k += 1
            c += (len(L) - m)
            n += 1
        else:
            A[k] = L[m]
            m += 1
            k += 1
    while m < len(L):
        A[k] = L[m]
        m += 1
        k += 1
    while n < len(R):
        A[k] = R[n]
        k += 1
        n += 1 
    return cl + cr + c
N = int(input())
A = list(map(int, input().split()))
print(count(A, 0, N-1))

Solution (Python):



def merge(lst, left, mid, right):
    L = lst[left: mid]
    R = lst[mid: right]
    l_len = len(L)
    r_len = len(R)
    i = 0
    j = 0
    cnt = 0
    for k in range(left, right):
        if l_len <= i:
            lst[k] = R[j]
            j += 1
        elif r_len <= j:
            lst[k] = L[i]
            i += 1
        elif L[i] <= R[j]:
            lst[k] = L[i]
            i += 1
        else:
            lst[k] = R[j]
            cnt += l_len - i
            j += 1
    return cnt


def mergeSort(lst, left, right):
    if left + 1 < right:
        mid = (left + right) >> 1
        cnt = mergeSort(lst, left, mid)
        cnt += mergeSort(lst, mid, right)
        return merge(lst, left, mid, right) + cnt
    else:
        return 0


def inversions(lst, n):
    return mergeSort(lst, 0, n)


def countingSort(lst, n):
    cnt_lst = [0] * (max(lst)+1)

    for i in range(0, n):
        cnt_lst[lst[i]] += 1

    i = 0
    result = []
    for j, cnt in enumerate(cnt_lst):
        for _ in range(0, cnt):
            result.append(j)
    return result


if __name__ == "__main__":
    n = int(input())
    lst = [int(val) for val in input().split()]
    print(inversions(lst, n))

Solution (Python):

def merge(A,left,mid,right):
    '''n1 = mid - left
    n2 = right - mid
    L = []
    R = []
    for i in range(0,n1):
        L.append(A[left+i])
 
    for i in range(0,n2):
        R.append(A[mid+i])
    L.append(1000000001)
    R.append(1000000001)
    '''
    L = A[left:mid]+[1000000001]
    R = A[mid:right]+[1000000001]
 
    i = 0
    j = 0
    cnt = 0
    for k in range(left,right):
        if L[i] <= R[j]:
            A[k]=L[i]
            i += 1
        else:
            if L[i] < 1000000001:
                cnt += mid-left -i
            A[k] = R[j]
            j += 1
    return cnt
 
def mergeSort(A,left,right):
    if left+1 < right:
        mid = (left+right)//2
        v1 = mergeSort(A,left,mid)
        v2 = mergeSort(A,mid,right)
        v3 = merge(A,left,mid,right)
        return v1+v2+v3
    else:
        return 0
 
 
if __name__ == '__main__':
    n = (int)(input())
    a = list(map(int,input().split()))
     
    cnt = mergeSort(a,0,n)

    print(cnt)

Solution (Python):
input()
a = list(map(int, input().split()))

def merge_and_count(a, b):
    i, j = 0, 0
    count = 0
    m = []
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            m.append(a[i])
            i += 1
        else:
            count += len(a)-i
            m.append(b[j])
            j += 1

    if i < len(a):
        m += a[i:]
    else:
        m += b[j:]

    return (count, m)



def sort_and_count(l):
    if len(l) == 1:
        return 0, l
    else:
        a = l[0:len(l)//2]
        b = l[len(l)//2:]
        rA, a_ = sort_and_count(a)
        rB, b_ = sort_and_count(b)
        r, l_ = merge_and_count(a_, b_)
    return (rA+rB+r, l_)

print(sort_and_count(a)[0])

Solution (Python):
INF = float("inf")
cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left: mid] + [INF]
    R = A[mid: right] + [INF]
    i = j = 0
    limit = mid - left
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
            if i >= limit:
                A[k + 1: right] = R[j: -1]
                break
        else:
            cnt += limit - i
            A[k] = R[j]
            j += 1
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
n = int(input())
S = list(map(int, input().split()))
mergeSort(S, 0, n)
print(cnt)

Solution (Python):
INF = float("inf")
cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left: mid] + [INF]
    R = A[mid: right] + [INF]
    i = j = 0
    limit = mid - left
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            cnt += limit - i
            A[k] = R[j]
            j += 1
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
n = int(input())
S = list(map(int, input().split()))
mergeSort(S, 0, n)
print(cnt)

Solution (Python):
INF = float("inf")
cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left: mid] + [INF]
    R = A[mid: right] + [INF]
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            cnt += j
            i += 1
        else:
            A[k] = R[j]
            j += 1
def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
n = int(input())
S = list(map(int, input().split()))
mergeSort(S, 0, n)
print(cnt)

Solution (Python):


def merge(A, left, mid, right):
    global cnt
    n1 = mid - left
    n2 = right - mid
    L = A[left:left+n1]
    R = A[mid:mid+n2]
    L.append(float('inf'))
    R.append(float('inf'))
    
    i = 0
    j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            cnt += n1 - i
            j += 1


def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(A, left, mid)
        mergeSort(A, mid, right)
        merge(A, left, mid, right)
    

if __name__ == '__main__':

    n = int(input())
    A = [int(a) for a in input().split(" ")]
    cnt = 0
    mergeSort(A, 0, n)
    print(cnt)

Solution (Python):
count = 0

def merge(a, left, mid, right):
    global count

    l = a[left:mid]
    r = a[mid:right]
    l.append(10**9+1)
    r.append(10**9+1)

    i = 0
    j = 0
    for k in range(left, right):
        if l[i] <= r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1
            count += mid - left - i

def mergeSort(a, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        mergeSort(a, left, mid)
        mergeSort(a, mid, right)
        merge(a, left, mid, right)


if __name__ == '__main__':
    n = int(input())
    a = [int(x) for x in input().split()]

    mergeSort(a, 0, n)

    print(count)

Solution (Python):
INF = 10 ** 10

def merge(hoge, left, mid, right):
    c = 0
    n1 = mid - left
    n2 = right - mid
    Left = [hoge[left+i] for i in range(n1)]
    Right = [hoge[mid+i] for i in range(n2)]
    Left.append(INF)
    Right.append(INF)
    i = 0
    j = 0
    for k in range(left, right):
        if Left[i] <= Right[j]:
            hoge[k] = Left[i]
            i += 1
        else:
            hoge[k] = Right[j]
            j += 1
            c += n1 - i
    return c

def mergeSort(hoge, left, right):
    c = 0
    if left+1 < right:
        mid = (left + right) // 2
        c += mergeSort(hoge, left, mid)
        c += mergeSort(hoge, mid, right)
        c += merge(hoge, left, mid, right)
    return c

if __name__ == '__main__':
    _ = input()
    hoge = [int(x) for x in input().split()]
    print (mergeSort(hoge, 0, len(hoge)))

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1
  else:A[k]=R[j];j+=1;c+=m-l-i
 return c
def s(A,l,r):
 if l+1<r:
  m=(l+r)//2
  return s(A,l,m)+s(A,m,r)+g(A,l,m,r)
 else:return 0
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1
  else:A[k]=R[j];j+=1;c+=m-l-i
 return c
def s(A,l,r):
 if l+1<r:
  m=(l+r)//2
  return s(A,l,m)+s(A,m,r)+g(A,l,m,r)
 else:return 0
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1
  else:A[k]=R[j];j+=1;c+=m-l-i
 return c
def s(A,l,r):
 if l+1<r:
  m=(l+r)//2
  return s(A,l,m)+s(A,m,r)+g(A,l,m,r)
 else:return 0
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
c=0
n=int(input())
A=list(map(int,input().split()))
s(A,0,n)
print(c)

Solution (Python):
def g(A,l,m,r):
 global c
 L,R=A[l:m]+[1e9],A[m:r]+[1e9]
 i=j=0
 for k in range(l,r):
  if L[i]<R[j]:A[k]=L[i];i+=1;c+=j
  else:A[k]=R[j];j+=1
def s(A,l,r):
 if l+1<r:m=(l+r)//2;s(A,l,m);s(A,m,r);g(A,l,m,r)
if'__main__'==__name__:
 c=0
 n=int(input())
 A=list(map(int,input().split()))
 s(A,0,n)
 print(c)