2291 糖果堆

来源:互联网 发布:数据库王珊第五版答案 编辑:程序博客网 时间:2024/05/14 20:26
题目描述 Description

【Shadow 1】第一题

WJMZBMR买了很多糖果,分成了N堆,排成一列。WJMZBMR说,如果Shadow能迅速求出第L堆到第R堆一共有多少糖果,就把这些糖果都给他。

现在给出每堆糖果的数量,以及每次询问的L和R,你需要帮助Shadow,把每次询问的结果求出来。注意,你不需要考虑糖果被Shadow取走的情况。

输入描述 Input Description

第1行,2的整数N,M,分别表示堆数和询问数量;

第2行,N个整数Ai,表示第i堆糖果的数量;

第3-(M+2)行,每行2个整数Li, Ri,表示第i个询问是[Li, Ri]。

输出描述 Output Description

M行,对于每个询问,输出对应的和。

样例输入 Sample Input
5 5
1 2 3 4 5
1 5
2 4
3 3
1 3
3 5
样例输出 Sample Output
15
9
3
6
12
数据范围及提示 Data Size & Hint

对于50%的数据,1≤N,M≤100;

对于100%的数据,1≤N,M≤100000,0≤Ai≤1000,1≤Li≤Ri≤N。



和数组

#include <iostream>#include <cstdio>using namespace std;long  map[100000]={0};int main(){long x,y;long sum=0;long t,ss=0;scanf("%ld%ld", &x, &y);int i;for(i=1;i<=x;i++){scanf("%ld", &t);ss+=t;map[i]=ss;}long m,n;while(y--){sum=0;scanf("%ld%ld", &m, &n);printf("%ld\n",map[n]-map[m-1]);}} 


1 1