前缀和列题AcWing795
import java.io.*;
//前缀和:改题目要求 求一串整数数列中l~r中的数的和
//可以提前算好前n项的和s[n],然后用s[n] - s[n - 1]
public class Main {
static int n,m,l,r;
static int N = 100010;
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int[] g = new int[N];
public static void main(String[] args) throws IOException {
String[] init = in.readLine().split(" ");
n = Integer.parseInt(init[0]);
m = Integer.parseInt(init[1]);
init = in.readLine().split(" ");
for(int i = 1;i <= n;i ++) {
g[i] = g[i - 1] + Integer.parseInt(init[i - 1]);
}
while(m-- > 0) {
init = in.readLine().split(" ");
l = Integer.parseInt(init[0]);
r = Integer.parseInt(init[1]);
out.write((g[r] - g[l - 1]) + "\n");
}
in.close();
out.flush();
}
}
前缀和例题:二维前缀和
二维前缀和-2