模版
import java.util.*;
import java.io.*;
import java.math.*;
class Main {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] sp;
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
void run() throws Exception {
log.flush();
}
public static void main(String[] agrs) throws Exception { new Main().run(); }
}
例子
795. 前缀和
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] sp;
int[] a, s;
int n, m;
int l, r;
void run() throws Exception {
sp = reader.readLine().split(" ");
n = Integer.parseInt(split[0]);
m = Integer.parseInt(split[1]);
a = new int[n + 1];
s = new int[n + 1];
sp = reader.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(sp[i - 1]);
}
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
}
while (m-- > 0) {
sp = reader.readLine().split(" ");
l = Integer.parseInt(split[0]);
r = Integer.parseInt(split[1]);
log.write(s[r] - s[l - 1] + "\n");
}
log.flush();
}
public static void main(String[] args) throws Exception { new Main().run(); }
}