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

【模板】前缀和

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

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

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

全部评论

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务