-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumofsubarrays.py
More file actions
55 lines (40 loc) · 1.03 KB
/
sumofsubarrays.py
File metadata and controls
55 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''Given an array of integers, answer queries of the form: [i, j] : Print the sum of array elements from A[i] to A[j], both inclusive.
Input Format
First line of input contains N - size of the array. The next line contains N integers - elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains 2 space separated indexes: i and j.
Constraints
30 points
1 <= N,Q <= 10000
70 points
1 <= N,Q <= 500000
General Constraints
-109 <= A[i] <= 109
0 <= i <= j <= N-1
Output Format
For each query, print the sum of array elements from A[i] to A[j], both inclusive, separated by newline.
Sample Input 0
10
1 30 13 -4 -5 12 -53 -12 43 100
4
0 5
1 7
2 3
7 9
Sample Output 0
47
-19
9
131
'''
n = int(input())
arr = list(map(int, input().split()))
t = int(input())
sumarr = dict()
sumarr[0] = arr[0]
for i in range(1,len(arr)):
sumarr[i] = sumarr[i-1]+arr[i]
for _ in range(t):
a, b = list(map(int, input().split()))
try:
print(sumarr[b] - sumarr[a-1])
except:
print(sumarr[b])