<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p>

<p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p>

<ul>
	<li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li>
	<li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li>
	<li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li>
	<li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li>
</ul>

<p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p>

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

<pre>
<strong>Input:</strong> obstacles = [1,2,3,2]
<strong>Output:</strong> [1,2,3,3]
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
- i = 0: [<u>1</u>], [1] has length 1.
- i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2.
- i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3.
- i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3.
</pre>

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

<pre>
<strong>Input:</strong> obstacles = [2,2,1]
<strong>Output:</strong> [1,2,1]
<strong>Explanation: </strong>The longest valid obstacle course at each position is:
- i = 0: [<u>2</u>], [2] has length 1.
- i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2.
- i = 2: [2,2,<u>1</u>], [1] has length 1.
</pre>

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

<pre>
<strong>Input:</strong> obstacles = [3,1,5,6,4,2]
<strong>Output:</strong> [1,1,2,3,2,2]
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
- i = 0: [<u>3</u>], [3] has length 1.
- i = 1: [3,<u>1</u>], [1] has length 1.
- i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid.
- i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid.
- i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid.
- i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2.
</pre>

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

<ul>
	<li><code>n == obstacles.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li>
</ul>

//m0

//[A] easy dp_ending[i] in O(n^2), now require O(nlogn) for 1964. Find the Longest Valid Obstacle Course at Each Position hard.


//[B] think of a tree:

//1. a tree actually building & forking the increasing subsequences from the root to a leaf.

//2. for [2, 6, 8, 3, 4, 5, 1], the 3 paths to any leaf are: sub1 = [2, 6, 8], sub2 = [2, 3, 4, 5], sub3 = [1] --> for process, see hiepit's sol.


//[C] implement [B] by a tree array:

//1. for eg., for node "3", once we found the parent node "2" by binary search,

//2. don't delete another forked branch ([2, 6, 8]) from the tree array.

//3. like lazy update / lazy propagation, just keep the old forked branch.

//4. we can still fork branches on any node of the tree array. --> the order is still correct.


//[D] final answer

//1. recommend not to use ans = final tree_array.size().

//2. for more clear semantics, ans = max(forked leaf index j) for all j,
//2.a. since it's more like enumerating LIS_dp_ending[j].


//[E] lower_bound or upper_bound?

//1. strictly increasing --> lower_bound(), 'cuz we replace the iterator's value.

//2. non-decreasing --> upper_bound() for replacing *it.


//[reference] hiepit's solution by greedy & binary search in O(nlogn):
//https://leetcode.com/problems/longest-increasing-subsequence/solutions/1326308/c-python-dp-binary-search-bit-segment-tree-solutions-picture-explain-o-nlogn

static auto _ = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
        int n = obstacles.size();

        vector<int> lis_ending(n);

        vector<int> tree_arr;
        for(int i=0;i<n;i++){
            const int h = obstacles[i];

            auto it = upper_bound(tree_arr.begin(), tree_arr.end(), h);

            //if empty, v.begin() == v.end()
            if(it == tree_arr.end()){
                tree_arr.push_back(h);

                lis_ending[i] = tree_arr.size();
            }else{
                *it = h;

                int j = it - tree_arr.begin();

                lis_ending[i] = j - (0-1);
            }
        }
        return lis_ending;
    }
};
120600