<p>You are given a <strong>1-indexed</strong> array of integers <code>nums</code> of length <code>n</code>.</p>

<p>We define a function <code>greaterCount</code> such that <code>greaterCount(arr, val)</code> returns the number of elements in <code>arr</code> that are <strong>strictly greater</strong> than <code>val</code>.</p>

<p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, append <code>nums[1]</code> to <code>arr1</code>. In the second operation, append <code>nums[2]</code> to <code>arr2</code>. Afterwards, in the <code>i<sup>th</sup></code> operation:</p>

<ul>
	<li>If <code>greaterCount(arr1, nums[i]) &gt; greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to <code>arr1</code>.</li>
	<li>If <code>greaterCount(arr1, nums[i]) &lt; greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to <code>arr2</code>.</li>
	<li>If <code>greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])</code>, append <code>nums[i]</code> to the array with a <strong>lesser</strong> number of elements.</li>
	<li>If there is still a tie, append <code>nums[i]</code> to <code>arr1</code>.</li>
</ul>

<p>The array <code>result</code> is formed by concatenating the arrays <code>arr1</code> and <code>arr2</code>. For example, if <code>arr1 == [1,2,3]</code> and <code>arr2 == [4,5,6]</code>, then <code>result = [1,2,3,4,5,6]</code>.</p>

<p>Return <em>the integer array</em> <code>result</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,3,3]
<strong>Output:</strong> [2,3,1,3]
<strong>Explanation:</strong> After the first 2 operations, arr1 = [2] and arr2 = [1].
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is zero in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
In the 4<sup>th</sup> operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.
After 4 operations, arr1 = [2,3] and arr2 = [1,3].
Hence, the array result formed by concatenation is [2,3,1,3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,14,3,1,2]
<strong>Output:</strong> [5,3,1,2,14]
<strong>Explanation:</strong> After the first 2 operations, arr1 = [5] and arr2 = [14].
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
In the 4<sup>th</sup> operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 &gt; 1). Hence, append nums[4] to arr1.
In the 5<sup>th</sup> operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 &gt; 1). Hence, append nums[5] to arr1.
After 5 operations, arr1 = [5,3,1,2] and arr2 = [14].
Hence, the array result formed by concatenation is [5,3,1,2,14].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,3,3,3]
<strong>Output:</strong> [3,3,3,3]
<strong>Explanation:</strong> At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3].
Hence, the array result formed by concatenation is [3,3,3,3].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class SegmentTree:
    def __init__(self, n):
        self.n = n
        self.Seg = [0] * (4 * n)

    def query(self, i, sL, sR, qL, qR):
        if sL > qR or sR < qL:
            return 0
        if sL >= qL and sR <= qR:
            return self.Seg[i]

        left_sum = self.query(2 * i + 1, sL, (sL + sR) // 2, qL, qR)
        right_sum = self.query(2 * i + 2, (sL + sR) // 2 + 1, sR, qL, qR)

        return left_sum + right_sum

    def update(self, i, sL, sR, idx):
        if sL > idx or sR < idx:
            return
        if sL == sR:
            self.Seg[i] += 1
            return

        mid = (sL + sR) // 2

        if mid >= idx:
            self.update(2 * i + 1, sL, mid, idx)
        else:
            self.update(2 * i + 2, mid + 1, sR, idx)

        self.Seg[i] = self.Seg[2 * i + 1] + self.Seg[2 * i + 2]


class Solution:
    def coordinate_compression(self, nums):
        st = set(nums)
        mp = {x: i for i, x in enumerate(sorted(st))}
        return mp

    def greater_count(self, Seg, num, mp):
        return Seg.query(0, 0, Seg.n - 1, mp[num] + 1, Seg.n - 1)

    def resultArray(self, nums):
        compressed_indices = self.coordinate_compression(nums)

        arr1, arr2 = [nums[0]], [nums[1]]
        Seg1, Seg2 = SegmentTree(len(compressed_indices)), SegmentTree(len(compressed_indices))
        Seg1.update(0, 0, Seg1.n - 1, compressed_indices[nums[0]])
        Seg2.update(0, 0, Seg2.n - 1, compressed_indices[nums[1]])

        for i in range(2, len(nums)):
            n1 = self.greater_count(Seg1, nums[i], compressed_indices)
            n2 = self.greater_count(Seg2, nums[i], compressed_indices)

            if n1 > n2:
                arr1.append(nums[i])
                Seg1.update(0, 0, Seg1.n - 1, compressed_indices[nums[i]])
            elif n1 < n2:
                arr2.append(nums[i])
                Seg2.update(0, 0, Seg2.n - 1, compressed_indices[nums[i]])
            else:
                if len(arr2) < len(arr1):
                    arr2.append(nums[i])
                    Seg2.update(0, 0, Seg2.n - 1, compressed_indices[nums[i]])
                else:
                    arr1.append(nums[i])
                    Seg1.update(0, 0, Seg1.n - 1, compressed_indices[nums[i]])

        ans = arr1 + arr2
        return ans

48600