2291 candy heap, 2291 candy

Source: Internet
Author: User

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;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.