#C9250. Subarray Sum Queries Using Prefix Sums
Subarray Sum Queries Using Prefix Sums
Subarray Sum Queries Using Prefix Sums
This problem requires you to efficiently answer multiple queries on the sum of a subarray of an integer array using the prefix sums technique.
Given an array \( A[1 \ldots n] \), the prefix sums array \( P \) is defined by the equation:
\( P[i] = \sum_{j=1}^{i} A[j] \) with \( P[0] = 0 \).
For each query specified by two indices \( l \) and \( r \) (where indices are 1-indexed), the subarray sum is computed as:
\( \text{Sum} = P[r] - P[l-1] \).
Your task is to process the queries and output the corresponding subarray sums.
inputFormat
The first line contains two integers \( n \) and \( q \) representing the number of elements in the array and the number of queries, respectively.
The second line contains \( n \) space-separated integers representing the elements of the array.
Each of the following \( q \) lines contains two integers \( l \) and \( r \), denoting the range (inclusive) for a subarray sum query. It is guaranteed that \( 1 \leq l \leq r \leq n \).
outputFormat
For each query, output a single integer representing the sum of the subarray from index \( l \) to \( r \) (inclusive). Each result should be printed on a new line.
## sample6 3
1 2 3 4 5 6
1 3
2 5
1 6
6
14
21
</p>