Range Sum QueryEasy

Range Sum Query

EasyPractice library · +10 XPprefix sumsrange queries

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

Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests

Example 1

Input
5 3
1 2 3 4 5
1 3
2 4
3 5
Output
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

Input
4 2
10 20 30 40
1 1
1 4
Output
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