Python Hard
\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        arr1 = SortedList([nums[0]])\n    
```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        arr1 = SortedList([nums[0]])\n        a1 = [nums[0]]\n        arr2 = SortedList([nums[1]])\n        a2 = [nums[1]]\n        N = len(nums)\n        \n        def greaterCount(arr, val):\n            index = bisect_right(arr, val)\n            return len(arr) - index\n        for i in range(2, N):\n            val = nums[i]\n            index = bisect_right(arr1, val)\n            i1 = len(arr1) - index\n            index = bisect_right(arr2, val)\n            i2 = len(arr2) - index\n          \n\n            if i1 > i2: \n                arr1.add(val)\n                a1.append(val)\n            elif i1 < i2:\n                arr2.add(val)\n                a2.append(val)\n            elif i1 == i2:\n                if len(arr1) <= len(arr2):\n                    arr1.add(val)\n                    a1.append(val)\n                else:\n                    arr2.add(val)\n                    a2.append(val)\n                        \n                         \n        return a1 + a2\n                        \n        \n        \n```
['Python3']