Python ZKW segment tree solution
\nclass seg_tree:\n    def __init__(self, n):\n        self.n = n\n        self.tree = [0]*(n*2)\n        \n    def query(self, l, r):\n        l, r, ans = l+se
```\nclass seg_tree:\n    def __init__(self, n):\n        self.n = n\n        self.tree = [0]*(n*2)\n        \n    def query(self, l, r):\n        l, r, ans = l+self.n, r+self.n, 0\n        while l<r:\n            if l%2:\n                ans += self.tree[l]\n                l += 1\n            if r%2:\n                r -= 1\n                ans += self.tree[r]\n            l, r = l//2, r//2\n        return ans\n    \n    def update(self, idx):\n        idx += self.n\n        self.tree[idx] += 1\n        while idx>1:\n            idx //= 2\n            self.tree[idx] = self.tree[idx*2]+self.tree[idx*2+1]\n\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        rank = {x:i for i, x in enumerate(sorted(set(nums)))}\n        nums1, t1, nums2, t2 = [nums[0]], seg_tree(len(rank)), [nums[1]], seg_tree(len(rank))\n        t1.update(rank[nums[0]])\n        t2.update(rank[nums[1]])\n        for x in nums[2:]:\n            c1, c2 = t1.query(rank[x]+1, len(rank)), t2.query(rank[x]+1, len(rank))\n            if (c1>c2) or (c1==c2 and len(nums1)<=len(nums2)):\n                nums1.append(x)\n                t1.update(rank[x])\n            else:\n                nums2.append(x)\n                t2.update(rank[x])\n        return nums1+nums2\n```
[]