题目
输入一个长度为 n 的整数序列。
接下来再输入 m 个询问,每个询问输入一对 l,r。
对于每个询问,输出原序列中从第 l 个数到第 r 个数的和。
输入 5 3 2 1 3 6 4 1 2 1 3 2 4
输出 3 6 10
public class prefix_calculate {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); //传入arr的长度数据
int m = in.nextInt(); //传入循环的次数
int[] a = new int[n+1];
int[] s = new int[n+1];
for (int i = 1; i <= n; i++) {a[i] = in.nextInt();} //原本数据arr:2,1,3,6,4
for (int i = 1; i <= n; i++) {s[i] = s[i - 1] + a[i];} //前缀和数据arr:2,3,6,12,16
//前缀和模板
//每次传入两个下标,拿到下标间包括下标的总和
while (m-- > 0) {
int l = in.nextInt(), r = in.nextInt();
System.out.println(s[r]-s[l-1]);
}
}
}



