2291 candy heap, 2291 candy
DescriptionDescription
[Shadow 1] Question 1
WJMZBMR bought a lot of candy and divided it into N heaps, which are arranged into a column. WJMZBMR said that if Shadow can quickly find the total number of sweets from the L heap to the R heap, give them all.
Now, we provide the quantity of candy in each heap and the L and R values of each query. You need to help Shadow to obtain the result of each query. Note that you do not need to consider the situation where the candy is removed by Shadow.
Input description
Input Description
The integer N and M in the second row represent the number of heaps and the number of inquiries, respectively;
Line 1, N integer Ai, indicating the number of candy heap I;
The row 3rd-(M + 2) has two integers, Li and Ri, indicating that the I-th query is [Li, Ri].
Output description
Output Description
M rows. For each query, the corresponding sum is output.
Sample Input
Sample Input5 51 2 3 4 51 52 43 31 33 5 sample output
Sample Output1593612 data range and prompt
Data Size & Hint
For 50% of the data, 1 ≤ N, M ≤ 100;
For 100% of the data, 1 ≤ N, M ≤ 100000,0 ≤ Ai ≤ 1000,1 ≤ Li ≤ Ri ≤ N.
CATEGORY tag
Tags click here to expand
Prefix and
#include<iostream>#include<cstdio>using namespace std;int a[100001];int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]=a[i-1]+a[i]; } for(int i=1;i<=m;i++) { int l,r; scanf("%d%d",&l,&r); printf("%d\n",a[r]-a[l-1]); } return 0;}