AcWing 795. 前缀和 JAVA
原题链接
简单
作者:
shy
,
2020-11-03 15:57:08
,
所有人可见
,
阅读 248
代码
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) nums[i] = in.nextInt();
int sum = 0;
int[] preSum = new int[n];
for (int i = 0; i < n; i++) {
sum += nums[i];
preSum[i] = sum;
}
for (int i = 0; i < m; i++) {
int left = in.nextInt(), right = in.nextInt();
int rangeSum = left == 1 ? preSum[right - 1] : preSum[right - 1] - preSum[left - 2];
System.out.println(rangeSum);
}
}
}
?