AcWing 795. 前缀和JAVA
原题链接
简单
作者:
理想二旬.
,
2021-05-02 17:29:32
,
所有人可见
,
阅读 271
JAVA 代码
import java.util.Scanner;
import java.io.BufferedInputStream;
class Main{
private int N = 100010;
public static void main(String[] args){
Scanner in = new Scanner(new BufferedInputStream(System.in));
int n = in.nextInt();
int m = in.nextInt();
//存放接收进来的数组
int[] arr = new int[n + 1];
//存放前缀和数组
int[] newArr = new int[n + 1];
//为了下面写代码方便将它设为0
newArr[0] = 0;
//原数组初始化
for(int i = 1; i < n + 1; i++)
arr[i] = in.nextInt();
//前缀和数组初始化
for(int i = 1; i < n + 1; i++)
newArr[i] = newArr[i - 1] + arr[i];
//读下面的数 计算
while(m-- > 0){
int l = in.nextInt();
int r = in.nextInt();
System.out.println(newArr[r] - newArr[l - 1]);
}
}
}