Problem 301671 · easy · Phase 03 Linear Management & Searching

Batch Interval Totals

prefix sums · range queries

A sensor logs one integer reading per minute in readings. Analysts send a whole batch of questions at once: queries[i] = [l, r] asks for the total of readings[l] through readings[r] inclusive (0-indexed). Return a list with one answer per query, in the same order.

Examples

Input:  readings = [3, 1, 4, 1, 5], queries = [[0, 2], [1, 4], [3, 3]]
Output: [8, 11, 1]
Explanation: 3+1+4 = 8, 1+4+1+5 = 11, and the single reading at index 3 is 1.

Input:  readings = [2, -2, 2], queries = [[0, 1]]
Output: [0]

Constraints

  • 1 <= len(readings) <= 10**5, 0 <= len(queries) <= 10**5
  • -10**4 <= readings[i] <= 10**4, 0 <= l <= r < len(readings)
  • Target complexity: O(n + q). Re-adding the range for every query is far too slow for the largest tests.

Goals

  • Precompute running totals once
  • Answer each inclusive range query in O(1)
Starting Python…