<p>There is a one-dimensional garden on the x-axis. The garden starts at the point <code>0</code> and ends at the point <code>n</code>. (i.e., the&nbsp;length of the garden is <code>n</code>).</p>

<p>There are <code>n + 1</code> taps located at points <code>[0, 1, ..., n]</code> in the garden.</p>

<p>Given an integer <code>n</code> and an integer array <code>ranges</code> of length <code>n + 1</code> where <code>ranges[i]</code> (0-indexed) means the <code>i-th</code> tap can water the area <code>[i - ranges[i], i + ranges[i]]</code> if it was open.</p>

<p>Return <em>the minimum number of taps</em> that should be open to water the whole garden, If the garden cannot be watered return <strong>-1</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png" style="width: 525px; height: 255px;" />
<pre>
<strong>Input:</strong> n = 5, ranges = [3,4,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
</pre>

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

<pre>
<strong>Input:</strong> n = 3, ranges = [0,0,0,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Even if you activate all the four taps you cannot water the whole garden.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>ranges.length == n + 1</code></li>
	<li><code>0 &lt;= ranges[i] &lt;= 100</code></li>
</ul>

class Solution {
public:
    #define MAXN 10001
    #define INFTY 1000000000
    int dp[MAXN], T[4 * MAXN];

    struct segment {
        int X, Y;
    } S[MAXN];

    static bool compareSegments(segment A, segment B) {
        return A.X < B.X;
    }

    void Build(int lo, int hi, int v) {
        if (lo == hi) {
            T[v] = INFTY;
            return;
        }
        int mid = (lo + hi) / 2;
        Build(lo, mid, 2 * v + 1);
        Build(mid + 1, hi, 2 * v + 1);
        T[v] = min(T[2 * v + 1], T[2 * v + 2]);
    }

    void Update(int lo, int hi, int v, int i) {
        if (lo == hi) {
            T[v] = dp[i];
            return;
        }
        int mid = (lo + hi) / 2;
        if (i <= mid)
          Update(lo, mid, 2 * v + 1, i);
        else 
          Update(mid + 1, hi, 2 * v + 2, i);
        T[v] = min(T[2 * v + 1], T[2 * v + 2]);
    }

    int Query(int lo, int hi, int v, int qlo, int qhi) {
        if (qlo <= lo && hi <= qhi)
          return T[v];
        if (qhi < lo || qlo > hi)
          return INFTY;
        int mid = (lo + hi) / 2;
        int x = Query(lo, mid, 2 * v + 1, qlo, qhi);
        int y = Query(mid + 1, hi, 2 * v + 2, qlo, qhi);
        return min(x, y);
    }

    int lastWithNotGreaterStart(int i, int lo, int hi) {
        while (lo < hi) {
            int mid = (lo + hi + 1) / 2;
            if (S[mid].X <= S[i].Y)
              lo = mid;
            else 
              hi = mid - 1;
        }
        return S[lo].X <= S[i].Y ? lo : -1;
    }

    int minTaps(int N, vector<int>& ranges) {
        N = ranges.size();
        for (int i = 0; i < N; i++) {
            S[i].X = i - ranges[i];
            S[i].Y = i + ranges[i];
        }
        sort(S, S + N, compareSegments);
        Build(0, N - 1, 0);
        for (int i = N - 1; i >= 0; i--)
          dp[i] = S[i].Y >= N - 1 ? 1 : INFTY;
        for (int i = N - 1; i >= 0; i--) {
            if (dp[i] == INFTY) {
                int right = lastWithNotGreaterStart(i, i + 1, N - 1);
                if (right != -1)
                  dp[i] = Query(0, N - 1, 0, i + 1, right) + 1;
            }
            Update(0, N - 1, 0, i);
        }
        int ans = INFTY;
        for (int i = 0; i < N; i++)
          if (S[i].X <= 0)
            ans = min(ans, dp[i]);
        return ans == INFTY ? -1 : ans;
    }
};
17111