#NEWLINE# def countSmaller( self , nums ) : #NEWLINE# #INDENT# '\n        Brute force: O(n^2)\n        Segment Tree\n        O(n lg n)\n        :type nums: List[int]\n        :rtype: List[int]\n        ' #NEWLINE# h = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( i , v ) in enumerate ( sorted ( nums ) ) : #NEWLINE# #INDENT# h [ v ] = i #NEWLINE# #UNINDENT# A = [ h [ v ] for v in nums ] #NEWLINE# n = len ( A ) #NEWLINE# st = SegmentTree ( n ) #NEWLINE# ret = [ ] #NEWLINE# for i in xrange ( ( n - 1 ) , ( - 1 ) , ( - 1 ) ) : #NEWLINE# #INDENT# ret . append ( st . query_less ( st . root , A [ i ] ) ) #NEWLINE# st . inc ( ret [ i ] , 1 ) #NEWLINE# #UNINDENT# return ret [ : : ( - 1 ) ]