#K52347. Subarray Sums Query
Subarray Sums Query
Subarray Sums Query
You are given an array of integers and a list of queries. Each query contains two indices, \(l\) and \(r\) (1-indexed), and you need to compute the sum of the subarray from index \(l\) to \(r\) (inclusive). The subarray sum can be calculated using the formula:
\(S(l, r) = \text{prefix}[r] - \text{prefix}[l-1]\)
where \(\text{prefix}[i]\) is the prefix sum of the array up to the \(i\)-th element. Your task is to output the result for each query.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array.
- The third line contains an integer \(q\), the number of queries.
- Each of the following \(q\) lines contains two space-separated integers \(l\) and \(r\) indicating a query for the subarray sum from index \(l\) to \(r\) (1-indexed).
outputFormat
For each query, output the sum of the subarray on a separate line. The output should be printed to standard output (stdout).
## sample5
1 2 3 4 5
3
1 3
2 5
1 5
6
14
15
</p>