':type nums: List[int]
:rtype: List[int]'

def countSmaller(self, nums):

    def binarySearch(A, target, compare):
        (start, end) = (0, (len(A) - 1))
        while (start <= end):
            mid = (start + ((end - start) / 2))
            if compare(target, A[mid]):
                end = (mid - 1)
            else:
                start = (mid + 1)
        return start
    class BIT(object, ):
        def __init__(self, n):
            self.__bit = ([0] * n)
        def add(self, i, val):
            while (i < len(self.__bit)):
                self.__bit[i] += val
                i += (i & (- i))
        def query(self, i):
            ret = 0
            while (i > 0):
                ret += self.__bit[i]
                i -= (i & (- i))
            return ret
    (sorted_nums, places) = (sorted(nums), ([0] * len(nums)))
    for (i, num) in enumerate(nums):
        places[i] = binarySearch(sorted_nums, num, (lambda x, y: (x <= y)))
    (ans, bit) = (([0] * len(nums)), BIT((len(nums) + 1)))
    for i in reversed(xrange(len(nums))):
        ans[i] = bit.query(places[i])
        bit.add((places[i] + 1), 1)
    return ans
