AcWing 795. 前缀和 Java
原题链接
简单
作者:
leo_0
,
2021-07-20 00:30:37
,
所有人可见
,
阅读 230
Java 代码
import java.util.*;
public class Main{
static int[] tmp;
static long res = 0;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = sc.nextInt();
}
int[] presum = new int[n+1];
presum[0] = 0;
for(int i = 1; i <= n; i++){
presum[i] = presum[i-1]+nums[i-1];
}
for(int i = 0; i < m; i++){
int l = sc.nextInt();
int r = sc.nextInt();
System.out.println(presum[r]-presum[l-1]);
}
}
}