<p>You are given a <strong>0-indexed</strong> array of non-negative integers <code>nums</code>. For each integer in <code>nums</code>, you must find its respective <strong>second greater</strong> integer.</p>

<p>The <strong>second greater</strong> integer of <code>nums[i]</code> is <code>nums[j]</code> such that:</p>

<ul>
	<li><code>j &gt; i</code></li>
	<li><code>nums[j] &gt; nums[i]</code></li>
	<li>There exists <strong>exactly one</strong> index <code>k</code> such that <code>nums[k] &gt; nums[i]</code> and <code>i &lt; k &lt; j</code>.</li>
</ul>

<p>If there is no such <code>nums[j]</code>, the second greater integer is considered to be <code>-1</code>.</p>

<ul>
	<li>For example, in the array <code>[1, 2, 4, 3]</code>, the second greater integer of <code>1</code> is <code>4</code>, <code>2</code> is <code>3</code>,&nbsp;and that of <code>3</code> and <code>4</code> is <code>-1</code>.</li>
</ul>

<p>Return<em> an integer array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the second greater integer of </em><code>nums[i]</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> nums = [2,4,0,9,6]
<strong>Output:</strong> [9,6,6,-1,-1]
<strong>Explanation:</strong>
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
Thus, we return [9,6,6,-1,-1].
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,3]
<strong>Output:</strong> [-1,-1]
<strong>Explanation:</strong>
We return [-1,-1] since neither integer has any integer greater than it.
</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>
</ul>

IDENTITY = float('-inf')

class MaxSegmentTree:

    def __init__(self, data):
        self.n = len(data)
        self.data = data
        self.tree = [None] * (4 * self.n)
        self._build(1, 0, self.n - 1)

    def _basefn(self, val):
        return val

    def _build(self, i, tl, tr):
        if tl == tr:
            self.tree[i] = self._basefn(self.data[tl])
        else:
            tm = (tl + tr) // 2
            self._build(2 * i, tl, tm)
            self._build(2 * i + 1, tm + 1, tr)
            self.tree[i] = self._combine(self.tree[2 * i], self.tree[2 * i + 1])

    def _combine(self, a, b):
        return max(a, b)

    def _queryRecurse(self, i, tl, tr, l, r, meetThisVal):
        if tr < l or tl > r:
            return IDENTITY  # No intersection

        if tl == tr:
            return self.tree[i] if self.tree[i] > meetThisVal else IDENTITY

        tm = (tl + tr) // 2

        if tl >= l and tr <= r:
            leftMax = self.tree[2 * i]
            if leftMax > meetThisVal:
                return self._queryRecurse(2 * i, tl, tm, l, r, meetThisVal)
            return self._queryRecurse(2 * i + 1, tm + 1, tr, l, r, meetThisVal)

        leftResult = self._queryRecurse(2 * i, tl, tm, l, r, meetThisVal)
        if leftResult != IDENTITY:
            return leftResult

        rightResult = self._queryRecurse(2 * i + 1, tm + 1, tr, l, r, meetThisVal)
        return rightResult if rightResult != IDENTITY else IDENTITY

    def update(self, index, newVal) -> None:
        self._updateRecurse(1, 0, self.n - 1, index, newVal)

    def _updateRecurse(self, i, tl, tr, index, newVal):
        if tl == tr:
            self.tree[i] = self._basefn(newVal)
        else:
            tm = (tl + tr) // 2
            if index <= tm:
                self._updateRecurse(2 * i, tl, tm, index, newVal)
            else:
                self._updateRecurse(2 * i + 1, tm + 1, tr, index, newVal)
            self.tree[i] = self._combine(self.tree[2 * i], self.tree[2 * i + 1])

    def queryEarliest(self, l, r, meetThisVal) -> int:
        return self._queryRecurse(1, 0, self.n - 1, l, r, meetThisVal)

class Solution:
    def secondGreaterElement(self, nums: List[int]) -> List[int]:

        # CAN USE 2 STACKS FOR O(n) time, or some sparse table + binary search (more like splitting in half) (I think) instead of a ST

        stack = [0] # holds indices, mono decreasing of vals, find first bigger on right
        firstBiggerOnRight = {}
        for i in range(1, len(nums)):
            num = nums[i]
            while stack and num > nums[stack[-1]]:
                firstBiggerOnRight[stack[-1]] = i
                stack.pop()
            stack.append(i)
        
        for index in stack:
            firstBiggerOnRight[index] = -1

        st = MaxSegmentTree(nums)

        res = []
        for i in range(len(nums)):
            firstOnRight = firstBiggerOnRight[i]
            if firstOnRight == -1:
                res.append(-1)
                continue
            nextRight = st.queryEarliest(firstOnRight + 1, len(nums), nums[i])
            res.append(nextRight if nextRight != -inf else -1)
        
        return res
            

1328