1081 子段求和

基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和。
 
例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1。3 + 7 + 9 = 19,输出19。
Input
第1行:一个数N,N为数组的长度(2 <= N <= 50000)。
第2 至 N + 1行:数组的N个元素。(-10^9 <= N[i] <= 10^9)
第N + 2行:1个数Q,Q为查询的数量。
第N + 3 至 N + Q + 2行:每行2个数,i,l(1 <= i <= N,i + l <= N)
Output
共Q行,对应Q次查询的计算结果。
Input示例
5
1
3
7
9
-1
4
1 2
2 2
3 2
1 5
Output示例
4
10
16
19

 

 

前缀和就能处理.....

 

代码:

 1 #include <vector>
 2 #include <map>
 3 #include <set>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstdlib>
 9 #include <string>
10 #include <cstring>
11 #include <queue>
12 #include <stack>
13 using namespace std;
14 
15 typedef long long ll;
16 ll a[50050];
17 
18 int main()
19 {
20     memset(a,0,sizeof(a));
21     int n,q;
22     scanf("%d",&n);
23     a[0]=0;
24     for(int i=1; i<=n; i++){
25         scanf("%lld",&a[i]);
26         a[i]+=a[i-1];
27     }
28     scanf("%d",&q);
29     int x,y;
30     for(int i=1; i<=q; i++){
31         scanf("%d%d",&x,&y);
32         printf("%lld\n",a[x+y-1]-a[x-1]);
33     }
34 }

 

转载于:https://www.cnblogs.com/wangmengmeng/p/5467064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值