Official

D - 夏休みの集中特訓 / Intensive Summer Vacation Training Editorial by admin Editorial for printing

Gemini 3.8 Flash (High)

Overview

Given a sequence of positive integers \(A\) of length \(N\), this problem asks us to find the number of contiguous subarrays \((l, r)\) such that their length is at most \(K\) and the sum of their elements is at least \(X\). By utilizing the property that all elements are positive, we can solve this problem efficiently using the two-pointer technique.

Analysis

Naive Approach and Its Limitations

Consider checking whether the condition is satisfied for all intervals \((l, r)\) (\(1 \le l \le r \le N\)). There are approximately \(\frac{N^2}{2}\) possible intervals. Since \(N \le 10^6\) in this problem, the total number of intervals is about \(5 \times 10^{11}\). Checking every interval would far exceed the execution time limit (typically around 2 seconds), resulting in TLE (Time Limit Exceeded). Therefore, we need to count valid intervals more efficiently.

Utilizing Monotonicity

A key point in this problem is that all elements are positive (\(A_i \ge 1\)). Since every element is positive, if we fix the starting position \(l\), the interval sum increases monotonically as the ending position \(r\) extends to the right.

Therefore, for a fixed \(l\), let \(r_{\min}\) be the minimum right endpoint such that the sum is at least \(X\): - When \(r < r_{\min}\): The sum is less than \(X\) (does not satisfy the condition) - When \(r \ge r_{\min}\): The sum is at least \(X\) (satisfies the condition)

Furthermore, according to the problem statement, the length must be at most \(K\), so the upper bound for \(r\) is \(r_{\max} = \min(N, l + K - 1)\). Thus, for a fixed \(l\), the range of valid right endpoints \(r\) is \(r_{\min} \le r \le r_{\max}\), and the count can be determined immediately as \(\max(0, r_{\max} - r_{\min} + 1)\).

Speeding Up with the Two-Pointer Technique

When the starting position \(l\) is advanced by one (\(l \to l + 1\)), \(A_l\) is removed from the interval, causing the sum to decrease. Consequently, the minimum right endpoint \(r_{\min}'\) for \(l+1\) will never move to the left of the previous \(r_{\min}\) (\(r_{\min}' \ge r_{\min}\)).

By exploiting this monotonicity, we can apply the two-pointer technique, advancing the left endpoint \(l\) from \(1\) to \(N\) while also moving the right endpoint \(r\) only to the right. As a result, each element is visited at most twice (once added by \(r\) and once removed by \(l\)), allowing us to compute the total count in \(O(N)\) time.

Algorithm

We describe the algorithm using 0-based indexing (\(0\) to \(N-1\)).

  1. Initialize the variable current_sum (current interval sum) to \(0\), the right pointer r to \(0\), and the answer counter ans to \(0\).
  2. Iterate the left pointer l from \(0\) to \(N-1\):
    • While current_sum < X and r < N, add A[r] to current_sum (current_sum += A[r]) and advance r by \(1\).
    • Upon exiting the loop:
      • If current_sum >= X, the minimum right endpoint index where the sum is at least \(X\) is min_r = r - 1.
      • Due to the length limit, the maximum allowed right endpoint is max_r = min(N - 1, l + K - 1).
      • If min_r <= max_r, there are max_r - min_r + 1 valid right endpoints, so add this amount to ans.
      • If r reaches the end while current_sum < X, it is impossible to achieve a sum of at least \(X\) for any subsequent l, so terminate the search.
    • In preparation for the next iteration, remove the current left element from the interval by executing current_sum -= A[l].
  3. Output the final value of ans.

Complexity

  • Time Complexity: \(O(N)\)
    • Both the left pointer l and the right pointer r increase monotonically from \(0\) to \(N\), meaning the algorithm finishes in at most \(2N\) steps across the entire loop.
  • Space Complexity: \(O(N)\)
    • Only memory to store the sequence \(A\) of length \(N\) is required.

Implementation Notes

  • Beware of Overflow:

    • The sum of \(A_i\) can reach up to \(N \times \max(A_i) = 10^6 \times 10^9 = 10^{15}\). In addition, the maximum possible answer is \(\frac{N(N+1)}{2} \approx 5 \times 10^{11}\), which will overflow a 32-bit integer type (int). Use a 64-bit integer type (long long in C++) for the interval sum and the answer counter.
  • Index Handling:

    • Notice that immediately after exiting the while loop, r has been incremented by \(1\) right after the sum became at least \(X\), meaning the actual minimum right endpoint is r - 1.

      Source Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long k, x;
    if (!(cin >> n >> k >> x)) return 0;

    vector<long long> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    long long ans = 0;
    long long current_sum = 0;
    int r = 0;

    for (int l = 0; l < n; ++l) {
        while (r < n && current_sum < x) {
            current_sum += a[r];
            r++;
        }
        if (current_sum >= x) {
            int min_r = r - 1;
            long long max_r = min((long long)n - 1, (long long)l + k - 1);
            if (min_r <= max_r) {
                ans += (max_r - min_r + 1);
            }
        } else {
            break;
        }
        current_sum -= a[l];
    }

    cout << ans << "\n";

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: