AcWing 795. 前缀和 -- java
原题链接
简单
作者:
ice_73
,
2025-01-17 22:41:36
,
所有人可见
,
阅读 1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
/**
* @author iceewei
* @Description: 前缀和
* @date 2025/1/17
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(br);
st.nextToken();
int n = (int)st.nval;
st.nextToken();
int m = (int)st.nval;
int[] q = new int[n + 1];
for (int i = 1; i <= n; i++) {
st.nextToken();
q[i] = (int)st.nval;
}
int[] s = new int[n + 1];
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + q[i];
}
while (m-- > 0) {
st.nextToken();
int l = (int)st.nval;
st.nextToken();
int r = (int)st.nval;
System.out.println(s[r] - s[l - 1]);
}
}
}