Number of inversions in an array of pairs of integers
You can visualize the problem like so (using a = [1,4,8], b = [5,7,2]):

As you can see, I have sorted a, with each element pointing to a position in b sorted in reverse. All we need to do now is iterate over a, counting how many previous indices were lower than us. We can can compute that with a Fenwick tree, all in $O(nlog(n))$. Here is a python implementation:
import bisect

def query(bit, i):
    ret = 0
    while i &gt; 0:
        ret += bit[i]
        i -= i &amp; -i
    return ret


def update(bit, i, delta):
    while i &lt; len(bit):
        bit[i] += delta
        i += i &amp; -i


def csFindInversions2(a, b):
    assert len(a) == len(b)

    def solve(a, b):
        tmp = sorted(b)
        lut = [
            len(b) - bisect.bisect(tmp, b[i])
            for i in sorted(range(len(a)), key=lambda i: a[i])
        ]
        bit = [0]*(len(a) + 1)
        ret = 0
        for i in lut:
            ret += query(bit, i)
            update(bit, i+1, 1)
        return ret
    return solve(a, b) + solve(b, a)

Note that this solution does not account for duplicates.