Range Sum Query
Given an array of N integers and Q queries, each query consists of two indices L and R (1-based indexing). For each query, calculate and print the sum of the array elements from index L to R, inclusive.
Input
The first line contains two integers N and Q, the size of the array and the number of queries. The second line contains N integers, the elements of the array. The next Q lines each contain two integers L and R, representing the start and end indices of a query range (1-based).
Output
For each query, print the sum of the array elements from index L to R on a new line.
Constraints
1 <= N <= 1000, 1 <= Q <= 1000, 0 <= array elements <= 1000, 1 <= L <= R <= N
Example 1
5 3 1 2 3 4 5 1 3 2 4 3 5
6 9 12
The array is [1, 2, 3, 4, 5]. Queries are for ranges [1,3], [2,4], and [3,5]. Sums are 1+2+3=6, 2+3+4=9, 3+4+5=12.
Example 2
4 2 10 20 30 40 1 1 1 4
10 100
The array is [10, 20, 30, 40]. Queries are for ranges [1,1] and [1,4]. Sums are 10 and 10+20+30+40=100.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 3 1 2 3 4 5 1 3 2 4 3 5
4 2 10 20 30 40 1 1 1 4