<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>

<p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>

<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>

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

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

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution {    
    void build_segment_tree(vector<int>& st, int k, vector<int>& nums, int a, int b) {
        if (a>b) return;
        if (a==b) {
            st[k]=a;
        }
        else {
            int m=a+(b-a)/2;
            build_segment_tree(st, 2*k+1, nums, a, m);
            build_segment_tree(st, 2*k+2, nums, m+1, b);
            if (nums[st[2*k+1]]<=nums[st[2*k+2]]) {
                st[k]=st[2*k+1];
            }
            else {
                st[k]=st[2*k+2];
            }
        }
    }
    int query(vector<int>& st, int k, int x, int y, vector<int>& nums, int a, int b) {
        if (x>b || y<a) {
            return -1;
        }
        if (a<=x && y<=b) {
            return st[k];
        }
        int m=x+(y-x)/2;
        int l=query(st, 2*k+1, x, m, nums, a, b);
        int r=query(st, 2*k+2, m+1, y, nums, a, b);
        
        int res=INT_MAX;
        if (l>-1 && r>-1) return nums[l]<=nums[r]?l:r;
        if (l>-1) return l;
        else return r;
    }
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        int n=nums.size();
        //int m=pow(2, int(log2(n))+2); //m=4*n
        vector<int> st(4*n);
        build_segment_tree(st, 0, nums, 0, n-1);

        vector<int> ans;
        int l=-1;
        for (int i=0; i<k; i++) {
            l=query(st, 0, 0, n-1, nums, l+1, n-k+i);
            ans.push_back(nums[l]);
        }
        
        return ans;
    }
};
128900