<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the length of the shortest non-empty <strong>subarray</strong> of </em><code>nums</code><em> with a sum of at least </em><code>k</code>. If there is no such <strong>subarray</strong>, return <code>-1</code>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [1,2], k = 4
<strong>Output:</strong> -1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> nums = [2,-1,2], k = 3
<strong>Output:</strong> 3
</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>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

class ST {
    int64_t n;
    vector<int64_t> tree;
public:
    ST(const vector<int64_t>& data) {
        int64_t N = data.size();
        n = N;
        tree.resize(2 * n);
        for (int64_t i = 0; i < n; i++) {
            tree[i + n] = data[i];
        }
        build();
    }

    void build() {  // build the tree
        for (int64_t i = n - 1; i > 0; --i) tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }

    void modify(int64_t p, int64_t value) {  // unordered_set value at position p
        for (tree[p += n] = value; p > 1; p >>= 1) tree[p>>1] = min(tree[p], tree[p^1]);
    }

    int64_t query(int64_t l, int64_t r) {  // sum on interval [l, r]
        r++;
        int64_t res = 1e12;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l&1) res = min(res, tree[l++]);
            if (r&1) res = min(res, tree[--r]);
        }
        return res;
    }

    // void reset(int64_t p) {  // unordered_set value at position p
    // 	for (tree[p += n] = 0; p > 1; p >>= 1) tree[p>>1] = tree[p] + tree[p^1];
    // }
};

class Solution {
public:
    int shortestSubarray(vector<int>& a, int k) {
        int64_t n = a.size();
        vector<int64_t> p(n);
        p[0] = a[0];
        for (int64_t i = 1; i < n; i++) p[i] = p[i - 1] + a[i];
        int64_t ans = INT_MAX;
        for (int64_t i = 0; i < n; i++) {
            if (p[i] >= k) ans = min(ans, i + 1);
        }

        // for (auto e : p) cout << e << " ";
        // cout << endl;
        // cout << "ans: " << ans << endl;
        
        vector<int64_t> data(n, 0);
        ST st(data);
        for (int64_t i = 0; i < n; i++) {
            int64_t beg = 0;
            int64_t end = i - 1;
            int64_t id = -1;
            while (beg <= end) {
                int64_t mid = (-beg + end) / 2 + beg;
                if ((int64_t)st.query(mid, i - 1) <= p[i] - k) {
                    id = mid;
                    beg = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
            // cout << "i: " << i << ", id: " << id << endl;
            if (id != -1) {
                ans = min(ans, i - id);
            }
            // cout << "ans: " << ans << endl;
            st.modify(i, p[i]);
        }
        if (ans == INT_MAX) ans = -1;
        return (int)ans;
    }
};
126940