C - 果樹園の収穫計画 / Orchard Harvest Plan Editorial by admin Editorial for printing
Claude 4.5 OpusOverview
This is a problem where we need to find the total harvest obtained from fruit trees within a range. By using prefix sums, we can answer each query efficiently.
Analysis
Understanding the Problem
When harvesting for \(T\) hours from fruit trees \(L\) to \(R\), the total number of fruits that can be harvested is:
\[\text{Harvest} = (A_L + A_{L+1} + \cdots + A_R) \times T\]
In other words, we simply calculate the sum of the range and multiply by \(T\).
Problem with the Naive Approach
If we naively sum from \(A_L\) to \(A_R\) for each query, it takes \(O(N)\) time per query in the worst case.
- Since there are \(Q\) queries, the total becomes \(O(NQ)\)
- When \(N, Q \leq 2 \times 10^5\), up to \(4 \times 10^{10}\) calculations are needed, resulting in TLE (Time Limit Exceeded)
Solution: Prefix Sum
By precomputing the prefix sum, we can find the sum of any range in \(O(1)\).
Define the prefix sum \(S\) as follows: - \(S_0 = 0\) - \(S_i = A_1 + A_2 + \cdots + A_i\) (for \(i \geq 1\))
Then, the sum of range \([L, R]\) is: $\(A_L + A_{L+1} + \cdots + A_R = S_R - S_{L-1}\)$
Concrete Example
For array \(A = [3, 1, 4, 1, 5]\):
| \(i\) | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| \(S_i\) | 0 | 3 | 4 | 8 | 9 | 14 |
- Sum of range \([2, 4]\) = \(S_4 - S_1 = 9 - 3 = 6\) (\(A_2 + A_3 + A_4 = 1 + 4 + 1 = 6\) ✓)
- Multiplying by \(T = 2\) hours gives a harvest of \(6 \times 2 = 12\)
Algorithm
Preprocessing: Create the prefix sum array
prefix_sumprefix_sum[0] = 0prefix_sum[i] = prefix_sum[i-1] + A[i-1](note the 1-indexed conversion)
Query Processing: For each query \((L, R, T)\)
- Range sum =
prefix_sum[R] - prefix_sum[L-1] - Harvest = Range sum \(\times T\)
- Range sum =
Complexity
Time Complexity: \(O(N + Q)\)
- \(O(N)\) for prefix sum preprocessing
- \(O(1)\) per query response, \(O(Q)\) total
Space Complexity: \(O(N)\)
- The prefix sum array has size \(N + 1\)
Implementation Notes
Be careful with index offsets
- In the problem statement, fruit trees are 1-indexed (1 to \(N\))
- Python arrays are 0-indexed (0 to \(N-1\))
- Prepare \(N+1\) elements for the prefix sum array so that
prefix_sum[i]represents “the sum from \(A_1\) to \(A_i\)” for easier handling
Output optimization
- Accumulating results in a list and outputting all at once with
'\n'.join()is faster than callingprint\(Q\) times
- Accumulating results in a list and outputting all at once with
Input optimization
- Using
sys.stdin.readlineallows for faster reading of large inputs
- Using
Source Code
import sys
def main():
input = sys.stdin.readline
N, Q = map(int, input().split())
A = list(map(int, input().split()))
# Calculate prefix sum
prefix_sum = [0] * (N + 1)
for i in range(N):
prefix_sum[i + 1] = prefix_sum[i] + A[i]
# Answer each query
results = []
for _ in range(Q):
L, R, T = map(int, input().split())
# Calculate sum of range [L, R] using prefix sum
range_sum = prefix_sum[R] - prefix_sum[L - 1]
# Harvest for T hours
harvest = range_sum * T
results.append(harvest)
print('\n'.join(map(str, results)))
if __name__ == "__main__":
main()
This editorial was generated by claude4.5opus.
posted:
last update: