AcWing 795. JAVA:前缀和
原题链接
简单
作者:
ARM
,
2020-08-03 16:05:31
,
所有人可见
,
阅读 339
java 代码
import java.io.*;
import java.lang.Integer;
class Main{
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] str = buf.readLine().split(" ");
String[] strNums = buf.readLine().split(" ");
int n = Integer.valueOf(str[0]);
int k = Integer.valueOf(str[1]);
int[] nums = new int[n + 1];
int cnt = 0;
for(int i = 1; i <= n; ++i){
cnt += Integer.valueOf(strNums[i - 1]);
nums[i] = cnt;
}
for(int i = 1; i <= k; ++i){
String[] kn = buf.readLine().split(" ");
int a = Integer.valueOf(kn[0]);
int b = Integer.valueOf(kn[1]);
System.out.println(nums[b] - nums[a - 1]);
}
}
}