Official

C - 果樹園の収穫計画 / Orchard Harvest Plan Editorial by admin Editorial for printing

gemini-3-flash-preview

Overview

This problem requires us to compute the total harvest from fruit trees numbered \(L\) through \(R\), given the harvest speeds \(A_i\) of \(N\) fruit trees. Specifically, we need to efficiently calculate the sum of harvest speeds in the specified range \([L, R]\) and multiply it by the harvest time \(T\).

Analysis

Let’s first consider a naive approach where, for each query, we simply sum up the harvest amounts from the \(L\)-th to the \(R\)-th fruit tree.

For a single query, up to \(N\) additions may occur. Since there are \(Q\) queries, the overall time complexity is \(O(NQ)\). Given the constraints \(N, Q \leq 2 \times 10^5\), the worst case requires approximately \(4 \times 10^{10}\) operations, which will not fit within the time limit.

Now, let’s transform the total harvest formula: $\(\text{Total Harvest} = \sum_{i=L}^{R} (A_i \times T) = T \times \sum_{i=L}^{R} A_i\)\( From this formula, we can see that if we can quickly compute "the sum of \)A_i\( over the range \)[L, R]$,” we can answer each query efficiently.

Algorithm

Prefix Sum

We use the prefix sum technique to efficiently compute the sum of a contiguous subarray.

  1. Preprocessing: Prepare an array \(P\) of length \(N+1\) that stores the cumulative sum of \(A_i\) from the \(1\)-st to the \(i\)-th element as follows:

    • \(P[0] = 0\)
    • \(P[1] = A_1\)
    • \(P[2] = A_1 + A_2\)
    • \(P[i] = P[i-1] + A_i\) This can be constructed in \(O(N)\) with a single pass through the array.
  2. Query Processing: The sum over the range \([L, R]\), \(\sum_{i=L}^{R} A_i\), can be computed using the difference of prefix sums: $\(\sum_{i=L}^{R} A_i = P[R] - P[L-1]\)\( This allows us to compute the total speed for each query in \)O(1)$.

  3. Final Calculation: Multiplying the computed total speed by \(T\) gives the answer for that query: $\(\text{Answer} = (P[R] - P[L-1]) \times T\)$

Complexity

  • Time Complexity: \(O(N + Q)\)
    • Building the prefix sum takes \(O(N)\), and each query is processed in \(O(1)\), so the overall complexity is \(O(N + Q)\), which is sufficiently fast.
  • Space Complexity: \(O(N)\)
    • The prefix sum array \(P\) requires \(O(N)\) memory.

Implementation Notes

  • Fast I/O: In Python, when there is a large amount of input data, reading all input at once using sys.stdin.read().split() is faster than calling input() repeatedly.

  • Index Adjustment: In the problem statement, fruit trees are numbered starting from \(1\) (1-indexed). By creating the prefix sum array \(P\) with size \(N+1\) and setting \(P[0]=0\), we can intuitively compute the range \([L, R]\) sum as \(P[R] - P[L-1]\).

  • Output Optimization: Instead of calling print for each query, storing results in a list and outputting them all at once using join at the end reduces the output overhead.

    Source Code

import sys

def solve():
    # 入力を一度にすべて読み込み、空白で分割してリストに格納します。
    # これは大量の入力を高速に処理するための一般的な手法です。
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # 果樹の本数 N と クエリの数 Q を取得します。
    N = int(input_data[0])
    Q = int(input_data[1])
    
    # 累積和(Prefix Sum)を格納する配列を用意します。
    # P[i] は 1 番目から i 番目までの果樹の収穫速度 A_i の合計を保持します。
    # P[0] = 0, P[1] = A_1, P[2] = A_1 + A_2, ...
    P = [0] * (N + 1)
    for i in range(N):
        # 各果樹の収穫速度 A_i は input_data[2] から N 個分並んでいます。
        P[i+1] = P[i] + int(input_data[i+2])
        
    # 各クエリの答えを格納するリストです。
    results = []
    
    # クエリの情報は input_data の N+2 番目から始まります。
    # 各クエリは L, R, T の 3 つの整数で構成されています。
    current_idx = N + 2
    for _ in range(Q):
        # 範囲 [L, R] と 収穫時間 T を取得します。
        L = int(input_data[current_idx])
        R = int(input_data[current_idx+1])
        T = int(input_data[current_idx+2])
        current_idx += 3
        
        # 範囲 [L, R] の果樹の収穫速度の合計は P[R] - P[L-1] で求められます。
        # 総収穫量は (収穫速度の合計) × (収穫時間 T) です。
        sum_A = P[R] - P[L-1]
        total_harvest = sum_A * T
        
        # 答えを文字列としてリストに追加します。
        results.append(str(total_harvest))
        
    # すべてのクエリの答えを改行で結合して一気に出力します。
    sys.stdout.write('\n'.join(results) + '\n')

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: