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
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Q11659_๊ตฌ๊ฐํฉ๊ตฌํ๊ธฐ4 {
public static void main1(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine(), " ");
int[] arr = new int[N + 1];
// {5, 4, 3, 2, 1}
for (int i = 1; i <= N; i++) {
arr[i] += arr[i - 1] + Integer.parseInt(st.nextToken()); // ๋์ ํฉ์ ๋ฏธ๋ฆฌ ๊ตฌํด ๋ํด์ค
}
// i=1, arr[1](5) = arr[1](x) + arr[0](x) + 5
// i=2, arr[2](9) = arr[2](x) + arr[1](5) + 4
// i=3, arr[3](12) = arr[3](x) + arr[2](9) + 3
// i=4, arr[4](14) = arr[4](x) + arr[3](12) + 2
// i=5, arr[5](15) = arr[5](x) + arr[4](14) + 1
// arr = {5, 9, 12, 14, 15}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine(), " ");
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
System.out.println(arr[end] - arr[start - 1]);
// 1~3 ๋ฒ์งธ ์ซ์์ ํฉ์ ๊ตฌํ๋ค๋ฉด
// arr[3](12) - arr[0](x) = 12
// 2~4 ๋ฒ์งธ ์ซ์์ ํฉ์ ๊ตฌํ๋ค๋ฉด
// arr[4](14) - arr[1](5) = 9
// 5~5 ๋ฒ์งธ ์ซ์์ ํฉ์ ๊ตฌํ๋ค๋ฉด
// arr[5](15) - arr[4](14) = 1
}
}
}
/*
----- ์์ ์
๋ ฅ 1 -----
5 3
5 4 3 2 1
1 3
2 4
5 5
----- ์์ ์ถ๋ ฅ 1 -----
12
9
1
*/
๐ฃ ๋ฐฑ์ค Q11659 - ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 4 (java)
This post is licensed under CC BY 4.0 by the author.