<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>

#include <vector>
#include <algorithm>
#include <climits>

class SegmentTree {
private:
    std::vector<long long> tree;
    int size;

    long long combine(long long a, long long b) {
        return std::max(a, b);
    }

    long long neutral_element() {
        return LLONG_MIN;
    }

public:
    SegmentTree(const std::vector<long long>& arr) {
        size = arr.size();
        tree.resize(size * 2, 0);

        for (int i = 0; i < size; ++i) {
            tree[size + i] = arr[i];
        }

        for (int i = size - 1; i > 0; --i) {
            tree[i] = combine(tree[2 * i], tree[2 * i + 1]);
        }
    }

    void update(int index, long long value) {
        index += size;
        tree[index] += value;
        while (index > 1) {
            index /= 2;
            tree[index] = combine(tree[2 * index], tree[2 * index + 1]);
        }
    }

    long long query(int left, int right) {
        left += size;
        right += size;
        long long result = neutral_element();

        while (left <= right) {
            if (left % 2 == 1) {
                result = combine(result, tree[left]);
                left++;
            }
            if (right % 2 == 0) {
                result = combine(result, tree[right]);
                right--;
            }
            left /= 2;
            right /= 2;
        }

        return result;
    }
};

class Solution {
public:
    int shortestSubarray(std::vector<int>& nums, int k) {
        int N = nums.size();
        std::vector<long long> prefix(1, 0);

        for (int num : nums) {
            prefix.push_back(prefix.back() + num);
        }

        SegmentTree s_tree(prefix);

        auto good = [&](int i, int j, long long target) {
            long long range_max = s_tree.query(i, j);
            return range_max >= target;
        };

        auto find = [&](int idx, long long target) {
            int left = idx, right = N, ans = INT_MAX;

            while (left <= right) {
                int mid = (left + right) / 2;

                if (good(left, mid, target)) {
                    ans = std::min(ans, mid);
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }

            return ans == INT_MAX ? -1 : ans;
        };

        int ans = INT_MAX;

        for (int start = 0; start < N; ++start) {
            int end = find(start, k + prefix[start]);
            if (end != -1) {
                ans = std::min(ans, end - start);
            }
        }

        return ans == INT_MAX ? -1 : ans;
    }
};

128780