D - 夏休みの集中特訓 / Intensive Summer Vacation Training Editorial by admin
Claude Opus 5 (High)Overview
Given a sequence \(A\) of \(N\) positive integers, the problem asks us to count the number of contiguous subsequences (subarrays) of length at most \(K\) whose sum is at least \(X\). This can be solved in \(O(N)\) using prefix sums and the two-pointer technique, or in \(O(N \log N)\) using binary search.
Analysis
Naive Approach
If we try all pairs \((l, r)\) that satisfy the conditions, there are \(N\) ways to choose \(l\) and at most \(K\) ways to choose \(r\), leading to \(O(NK)\) pairs to inspect. Furthermore, calculating the subarray sum with a loop each time would take \(O(N K^2)\).
Since \(N \le 10^6\), even \(O(NK)\) would result in up to \(10^{12}\) operations in the worst case, which will exceed the time limit.
Accelerating Subarray Sums: Prefix Sums
First, subarray sums can be computed efficiently using prefix sums. Defining $\(S_0 = 0,\quad S_i = A_1 + A_2 + \cdots + A_i\)\( gives \)\(\sum_{i=l}^{r} A_i = S_r - S_{l-1}\)\( allowing any subarray sum to be computed in \)O(1)\( time. This reduces the overall complexity to \)O(NK)$, but it is still not fast enough.
Key Observation: Monotonicity
Notice the constraint \(1 \le A_i\) (all elements are positive). For a fixed \(l\), as \(r\) increases, the subarray sum \(S_r - S_{l-1}\) is monotonically increasing.
In other words, for a fixed \(l\), if we define \(f(l)\) as the “minimum \(r\) such that \(S_r - S_{l-1} \ge X\)”:
- If \(r \ge f(l)\), the sum is at least \(X\) (satisfies the condition).
- If \(r < f(l)\), the sum is strictly less than \(X\) (does not satisfy the condition).
Therefore, for a fixed \(l\), taking into account both the length restriction \(r \le l + K - 1\) and the array boundary \(r \le N\), the number of valid subarrays is
\[\max(0,\ \min(N,\, l+K-1) - f(l) + 1)\]
Once \(f(l)\) is found, we can count the valid subarrays for each \(l\) in \(O(1)\).
Finding \(f(l)\): Two Pointers
Furthermore, as \(l\) increases by \(1\), the left endpoint of the interval shifts to the right. Consequently, to make the sum at least \(X\), the right endpoint \(r\) must also shift to the right (or at least, it will never decrease). That is, the following monotonicity holds:
\[f(1) \le f(2) \le \cdots \le f(N)\]
Therefore, as we iterate \(l\) from \(1\) to \(N\), we can compute \(f(l)\) simply by advancing \(r\) forward without ever rewinding it. This is the two-pointer technique. The total number of advances of \(r\) across the entire algorithm is bounded by \(O(N)\).
(Finding \(f(l)\) for each \(l\) using binary search also takes \(O(N \log N)\), which is well within the time limit.)
Example
Consider \(N=5, K=3, X=6, A=(1,2,3,4,5)\), where \(S = (0,1,3,6,10,15)\).
- \(l=1\): The minimum \(r\) with sum at least \(6\) is \(3\) (\(1+2+3=6\)). The upper bound for \(r\) is \(\min(5, 1+3-1)=3\). Thus, \(1\) subarray.
- \(l=2\): The minimum \(r\) is \(4\) (\(2+3+4=9\)). The upper bound is \(4\). Thus, \(1\) subarray.
- \(l=3\): The minimum \(r\) is \(4\) (\(3+4=7\)). The upper bound is \(5\). Thus, \(2\) subarrays (\(r=4,5\)).
- \(l=4\): The minimum \(r\) is \(5\) (\(4+5=9\)). The upper bound is \(5\). Thus, \(1\) subarray.
- \(l=5\): Since \(A_5=5 < 6\), no such \(r\) exists. \(0\) subarrays.
Total: \(5\) subarrays.
Algorithm
- Construct the prefix sums \(S_0, S_1, \dots, S_N\).
- Initialize
r = 1. - For \(l = 1, 2, \dots, N\) in order:
- If
r < l, setr = l(align with the left endpoint so the interval is not empty). - While
r <= NandS[r] - S[l-1] < X, incrementrby \(1\). After the loop terminates,rrepresents \(f(l)\) (or \(N+1\) if none exists). - Let
hi = min(N, l + K - 1). Ifr <= hi, addhi - r + 1to the answer.
- If
- Output the answer.
Since \(r\) only increases monotonically, the total number of iterations of the inner while loop is \(O(N)\).
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Implementation Notes
Beware of integer overflow: Since \(A_i \le 10^9\) and \(N \le 10^6\), the total sum can reach up to around \(10^{15}\). Since \(X \le 10^{15}\) is also large, prefix sums must be handled using 64-bit integers (
long longin C++). The final answer can also reach around \(N \times K \approx 10^{12}\), so it must also be stored in a 64-bit integer.Ensure \(r\) does not fall behind \(l\): The value of \(r\) from the previous \(l\) may be smaller than the new \(l\), so the check
if (r < l) r = l;is necessary (otherwise, empty or negative-length intervals might be counted).When \(r\) exceeds \(N\): If the right endpoint reaches past the end without the sum reaching \(X\), then
r = N+1. In this case,r <= hibecomes false, naturally contributing \(0\) to the count.Fast I/O: Because \(N \le 10^6\) means large input sizes, in C++ it is safe to either untie
cin(ios::sync_with_stdio(false)) or read all input at once withfreadand parse it manually. In Python, usesys.stdin.buffer.read().Positivity of \(A_i\) is essential: If negative values were allowed, the monotonicity would break, and the two-pointer technique could not be used. Keep in mind that this solution relies heavily on the “all elements are positive” constraint.
Source Code
#include <bits/stdc++.h>
using namespace std;
static char buf[1 << 25];
int main() {
size_t len = fread(buf, 1, sizeof(buf) - 1, stdin);
buf[len] = '\0';
char *p = buf;
auto readULL = [&]() -> unsigned long long {
while (*p && (*p < '0' || *p > '9')) ++p;
unsigned long long v = 0;
while (*p >= '0' && *p <= '9') { v = v * 10 + (unsigned long long)(*p - '0'); ++p; }
return v;
};
long long N = (long long)readULL();
long long K = (long long)readULL();
long long X = (long long)readULL();
vector<long long> S(N + 1, 0);
for (long long i = 1; i <= N; i++) {
long long a = (long long)readULL();
S[i] = S[i - 1] + a;
}
long long ans = 0;
long long r = 1;
for (long long l = 1; l <= N; l++) {
if (r < l) r = l;
while (r <= N && S[r] - S[l - 1] < X) r++;
long long hi = min(N, l + K - 1);
if (r <= hi) ans += hi - r + 1;
}
printf("%lld\n", ans);
return 0;
}
This editorial was generated by claude5opus-high.