Build prefix array in one pass. Answer each query with one subtraction. Watch for 1-indexed vs 0-indexed arrays. In CSES problems, indices are typically 1-based. Adjust your array or use prefix[0] = 0 as a sentinel. Time: preprocessing + per query. Space: for the prefix array. The implementation follows directly from the recurrence. Each line of code corresponds to part of the mathematical formula. Walk through a small example step by step to verify your understanding.
Time complexity: .
Space complexity: .