-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMain.java
66 lines (55 loc) · 1.72 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Authored by : suin8
//Co-authored by : -
//Link : http://boj.kr/d42746a577ac40cf8921ca94bbed14d6
import java.util.*;
import java.io.*;
public class Main {
static int[] num = new int[100010];
static int[] sum = new int[100010];
public static void main(String[] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
int M = rd.nextInt();
// 1부터 i까지 합을 구해놓는다.
for(int i = 1;i <= N;i++) {
num[i] = rd.nextInt();
sum[i] = sum[i - 1] + num[i];
}
// end까지의 합에서 begin전까지의 합을 빼면 그 중간 값들의 합이 나온다.
// 매번 시행마다 더하면 시간초과
for(int i = 0;i < M;i++) {
int begin = rd.nextInt();
int end = rd.nextInt();
System.out.println(sum[end] - sum[begin - 1]);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}