티스토리 뷰
https://www.acmicpc.net/problem/11441
코드
import java.io.*;
import java.util.*;
public class Main {
static int[] arr;
static int[] dp;
static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
arr = new int[n + 1];
dp = new int[n + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 1; i <= n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
dp[i] = dp[i - 1] + arr[i];
}
int m = Integer.parseInt(br.readLine());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
sb.append(solution(s, e) + "\n");
}
System.out.println(sb);
}
static int solution(int s, int e) {
return dp[e] - dp[s - 1];
}
}