<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>

<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>

<ul>
	<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 &lt;= prizePositions[i] &lt;= 3</code> or <code>2 &lt;= prizePositions[i] &lt;= 4</code>.</li>
</ul>

<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>

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

<pre>
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
</pre>

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

<pre>
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
<strong>Output:</strong> 2
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes. 
</pre>

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

<ul>
	<li><code>1 &lt;= prizePositions.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= prizePositions[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>9</sup> </code></li>
	<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
</ul>

<p>&nbsp;</p>
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; 
}
.spoiler {overflow:hidden;}
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
</style>

class SegmentTree:
    def __init__(self, prizes: Dict[int, int], n: int) -> None:
        self.n = n
        self.tree = [0]*(4*n)
        self.prizes = prizes
        self._build(0, self.n-1, 1)
    
    def _build(self, s: int, e: int, idx: int) -> None:
        if s > e:
            return
        elif s == e:
            if s in self.prizes:
                self.tree[idx] = self.prizes[s]
        else:
            mid = (s+e)//2
            self._build(s, mid, 2*idx)
            self._build(mid+1, e, 2*idx+1)
            self.tree[idx] = max(self.tree[2*idx], self.tree[2*idx+1])
    
    def _query_helper(self, t_s: int, t_e: int, s: int, e: int, idx: int) -> int:
        if t_s <= s and t_e >= e:
            return self.tree[idx]
        elif t_s > e or t_e < s:
            return 0
        else:
            mid = (s+e)//2
            left = self._query_helper(t_s, t_e, s, mid, 2*idx)
            right = self._query_helper(t_s, t_e, mid+1, e, 2*idx+1)
            return max(left, right)
    
    def query(self, s: int, e: int) -> int:
        return self._query_helper(s, e, 0, self.n-1, 1)
        

class Solution:
    def search(self, prizePositions: List[int], start: int, k: int, n: int) -> int:
        lo, hi = start, n-1
        while lo < hi:
            mid = (lo+hi)//2
            if prizePositions[mid] - prizePositions[start] <= k:
                lo = mid+1
            else:
                hi = mid
        if lo >= n or prizePositions[lo] - prizePositions[start] > k:
            return lo-1
        else:
            return lo
    
    def maximizeWin(self, prizePositions: List[int], k: int) -> int:
        # 1. find the maximum prizes for every different starting values
        n = len(prizePositions)
        prizes = {}
        for i in range(n):
            if i==0 or prizePositions[i] != prizePositions[i-1]:
                end_idx = self.search(prizePositions, i, k, n)
                prizes[i] = end_idx - i + 1
        
        sg_tree = SegmentTree(prizes, n)
        idxs = list(prizes.keys())
        idxs.sort()
        # 2. for every index of the 1st segment, determine the maximum prize of the second segment
        # in this case, we need to use binary search to find the next index, and 
        # use segement tree to find the maximum value from random intervals
        ans = 0
        for idx in idxs:
            total1 = prizes[idx]
            next_idx = idx+total1
            total2 = sg_tree.query(next_idx, n-1)
            ans = max(ans, total1+total2)
        
        return ans
        
28800