<p>You are given an integer array <code>nums</code> containing <strong>distinct</strong> numbers, and you can perform the following operations <strong>until the array is empty</strong>:</p>

<ul>
	<li>If the first element has the <strong>smallest</strong> value, remove it</li>
	<li>Otherwise, put the first element at the <strong>end</strong> of the array.</li>
</ul>

<p>Return <em>an integer denoting the number of operations it takes to make </em><code>nums</code><em> empty.</em></p>

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

<pre>
<strong>Input:</strong> nums = [3,4,-1]
<strong>Output:</strong> 5
</pre>

<table style="border: 2px solid black; border-collapse: collapse;">
	<thead>
		<tr>
			<th style="border: 2px solid black; padding: 5px;">Operation</th>
			<th style="border: 2px solid black; padding: 5px;">Array</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">1</td>
			<td style="border: 2px solid black; padding: 5px;">[4, -1, 3]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">2</td>
			<td style="border: 2px solid black; padding: 5px;">[-1, 3, 4]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">3</td>
			<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">4</td>
			<td style="border: 2px solid black; padding: 5px;">[4]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">5</td>
			<td style="border: 2px solid black; padding: 5px;">[]</td>
		</tr>
	</tbody>
</table>

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

<pre>
<strong>Input:</strong> nums = [1,2,4,3]
<strong>Output:</strong> 5
</pre>

<table style="border: 2px solid black; border-collapse: collapse;">
	<thead>
		<tr>
			<th style="border: 2px solid black; padding: 5px;">Operation</th>
			<th style="border: 2px solid black; padding: 5px;">Array</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">1</td>
			<td style="border: 2px solid black; padding: 5px;">[2, 4, 3]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">2</td>
			<td style="border: 2px solid black; padding: 5px;">[4, 3]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">3</td>
			<td style="border: 2px solid black; padding: 5px;">[3, 4]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">4</td>
			<td style="border: 2px solid black; padding: 5px;">[4]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">5</td>
			<td style="border: 2px solid black; padding: 5px;">[]</td>
		</tr>
	</tbody>
</table>

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

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
</pre>

<table style="border: 2px solid black; border-collapse: collapse;">
	<thead>
		<tr>
			<th style="border: 2px solid black; padding: 5px;">Operation</th>
			<th style="border: 2px solid black; padding: 5px;">Array</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">1</td>
			<td style="border: 2px solid black; padding: 5px;">[2, 3]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">2</td>
			<td style="border: 2px solid black; padding: 5px;">[3]</td>
		</tr>
		<tr>
			<td style="border: 2px solid black; padding: 5px;">3</td>
			<td style="border: 2px solid black; padding: 5px;">[]</td>
		</tr>
	</tbody>
</table>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9&nbsp;</sup>&lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li>All values in <code>nums</code> are <strong>distinct</strong>.</li>
</ul>

class BIT:
    def __init__(self, size):
        self.size = size
        self.tree = [0] * (size + 1)
    
    def prefix_sum(self, i):
        sum_ = 0
        i += 1
        while i > 0:
            sum_ += self.tree[i]
            i -= i & (-i)
        return sum_
    
    def add(self, i, val):
        i += 1
        while i <= self.size:
            self.tree[i] += val
            i += i & (-i)


class Solution:
    def countOperationsToEmptyArray(self, nums):
        n = len(nums)
        bit = BIT(n)
        
        ids = list(range(n))
        ids.sort(key=lambda x: nums[x])
        
        res = 0
        cur = 0
        
        for id_ in ids:
            if cur <= id_:
                res += 1 + (id_ - bit.prefix_sum(id_)) - (cur - bit.prefix_sum(cur - 1))
            else:
                res += (n - bit.prefix_sum(n - 1)) + (id_ - bit.prefix_sum(id_)) - (cur - bit.prefix_sum(cur))
            bit.add(id_, 1)
            cur = id_
        
        return res

# ****** THIS Solution is TLE But no Problem the Sol'n is Good *****

# class SegmentTree:
#     def __init__(self, n):
#         self.n = n
#         self.lzy = [0] * (4 * n)
#         self.build(0, n - 1, 0)
    
#     def query(self, ind):
#         return self.query_range(ind, 0, self.n - 1, 0)
    
#     def update(self, x, y, add):
#         self.update_range(x, y, add, 0, self.n - 1, 0)
    
#     def build(self, l, r, i):
#         if l == r:
#             self.lzy[i] = l
#             return
        
#         m = (l + r) // 2
#         self.build(l, m, 2 * i + 1)
#         self.build(m + 1, r, 2 * i + 2)
    
#     def push_lazy(self, l, r, i):
#         if self.lzy[i] == 0:
#             return
#         if l != r:
#             self.lzy[2 * i + 1] += self.lzy[i]
#             self.lzy[2 * i + 2] += self.lzy[i]
#             self.lzy[i] = 0
    
#     def update_range(self, x, y, add, l, r, i):
#         self.push_lazy(l, r, i)
        
#         if r < x or l > y:
#             return
#         if l >= x and r <= y:
#             self.lzy[i] += add
#             self.push_lazy(l, r, i)
#             return
        
#         m = (l + r) // 2
#         self.update_range(x, y, add, l, m, 2 * i + 1)
#         self.update_range(x, y, add, m + 1, r, 2 * i + 2)
    
#     def query_range(self, ind, l, r, i):
#         self.push_lazy(l, r, i)
        
#         if l == r:
#             return self.lzy[i]
        
#         m = (l + r) // 2
#         if m >= ind:
#             return self.query_range(ind, l, m, 2 * i + 1)
#         else:
#             return self.query_range(ind, m + 1, r, 2 * i + 2)


# class Solution:
#     def countOperationsToEmptyArray(self, nums):
           
#         # Try Another Sort Method which is simple next time
#         # This is Using segment Tree and some updation using the start index

#         n = len(nums)
#         tree = SegmentTree(n)
        
#         val_and_ind = [(nums[j], j) for j in range(n)]
#         val_and_ind.sort()
        
#         all_ind = set(range(n))
        
#         result = 0
#         start_ind = 0
        
#         for j in range(n - 1):
#             original_ind = val_and_ind[j][1]
#             all_ind.remove(original_ind)
            
#             sorted_ind = tree.query(original_ind)
            
#             if sorted_ind < start_ind:
#                 cur_arr_ind = sorted_ind - start_ind + n - j
#             else:
#                 cur_arr_ind = sorted_ind - start_ind
            
#             result += cur_arr_ind
            
#             tree.update(original_ind, n - 1, -1)
            
#             next_ind = min(all_ind, key=lambda x: (x - original_ind) % n)
#             start_ind = tree.query(next_ind)
        
#         return result + n

1500