题解 | 【模板】前缀和
【模板】前缀和
https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf
import java.util.Scanner; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), q = in.nextInt(); int i = 1; long []a = new long[n + 1];//使用long,因为前缀和的数据范围较大 while (i <= n) { long temp = in.nextLong(); a[i] = temp + a[i++ -1]; //执行顺序是从左到右 } i = 1; while (i++ <= q) { int l = in.nextInt(), r = in.nextInt(); System.out.println(a[r] - a[l - 1]); } } }