题解 | #【模板】前缀和#

【模板】前缀和

http://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf

import java.util.; import java.io.; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = in.readLine()) != null){ String[] arr = str.split(" "); int n = Integer.parseInt(arr[0]); int q = Integer.parseInt(arr[1]); String[] res = in.readLine().split(" "); long[] dest = new long[n+1]; long sum = 0; for(int i = 0;i < n;i++){ sum += Integer.parseInt(res[i]); dest[i+1] = sum; } for(int i = 0;i < q;i ++){ String[] temp = in.readLine().split(" "); int l = Integer.parseInt(temp[0]); int r = Integer.parseInt(temp[1]); System.out.println(dest[r] - dest[l-1]); } } } }

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

ResourceUtilization:差不多但是估计不够准确,一面没考虑到增长人口,另一方面也没考虑到能上大学的人数比例,不过我猜肯定只多不少
点赞 评论 收藏
分享
05-12 17:28
已编辑
门头沟学院 硬件开发
ldf李鑫:不说公司名祝你以后天天遇到这样的公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务