<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 Segment_tree {
    vector<int> tree, data;
    int length;


    void _build(int current_idx, int left, int right) {
        if(left == right) {
            tree[current_idx] = left;
            return; 
        }
        int mid = (left + right) / 2;
        _build(2 * current_idx + 1, left, mid);
        _build(2 * current_idx + 2, mid + 1, right);
        
        tree[current_idx] = data[tree[2 * current_idx + 1]] <= data[tree[2 * current_idx + 2]] ? tree[2 * current_idx + 1] : tree[2 * current_idx + 2];
        return;
    }

    int _get_min(int current_idx, int left, int right, int q_left, int q_right) {
        if(q_left > right || q_right < left) return -1;
        if(left >= q_left && right <= q_right) return tree[current_idx];
        int mid = (left + right) / 2;
        int lhs = _get_min(2 * current_idx + 1, left, mid, q_left, q_right);
        int rhs = _get_min(2 * current_idx + 2, mid + 1, right, q_left, q_right);
        if(lhs == -1) return rhs;
        if(rhs == -1) return lhs;
        return data[lhs] <= data[rhs] ? lhs : rhs;
    }
    public:
        Segment_tree(){};
        void init(int sz, vector<int>& nums) {
            length = sz;
            tree.resize(4 * length);
            data = nums;
            _build(0, 0, length);
        }
         
        int get_min(int q_left, int q_right) {
            return _get_min(0, 0, length, q_left, q_right);  
        } 
};

class Solution {
public:
    Segment_tree st;
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        int n = nums.size(), left_idx = -1;
        vector<int> answer;
        st.init(n - 1, nums);
        while(n - 1 - left_idx >= k && k > 0) {
            int idx = st.get_min(left_idx + 1, n - k);
            answer.push_back(nums[idx]);
            left_idx = idx;
            k--;
        }
        while(k > 0) {
            answer.push_back(++left_idx);
            k--;
        }
        return answer;
    }
};

133000