Home ๐Ÿฃ ๋ฐฑ์ค€ Q11660 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5 (java)
Post
Cancel

๐Ÿฃ ๋ฐฑ์ค€ Q11660 - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5 (java)

๋ฌธ์ œ




แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 09 19

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 09 36



ํ’€์ด ์ˆœ์„œ


  1. A[][] : ๊ฐ’๋“ค์„ ์ž…๋ ฅ๋ฐ›์•„ 2์ฐจ์› ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€๋‹ค.
    1. D[][] : (1,1) ~ (i, j)๊นŒ์ง€์˜ ๊ตฌ๊ฐ„์˜ ํ•ฉ์„ ๋ฐฐ์—ด์— ๋‹ค์‹œ ๋„ฃ๋Š”๋‹ค.
  2. (x1, y1), (x2, y2) : ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ์ž…๋ ฅ๋ฐ›๊ณ 
  3. ๊ตฌ๊ฐ„์˜ ํ•ฉ์„ ๊ตฌํ•ด๋†“์€ D[][]๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด ํ•„์š”ํ•œ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.



์ฝ”๋“œ



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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Q11660_๊ตฌ๊ฐ„ํ•ฉ๊ตฌํ•˜๊ธฐ5 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");

		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());

		int A[][] = new int[N+1][N+1];
		for (int i = 1; i <= N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
            for (int j = 1; j <= N; j++) {
                A[i][j] = Integer.parseInt(st.nextToken());
            }
		}

		int D[][] = new int[N+1][N+1];
        for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				// ๊ตฌ๊ฐ„์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
				D[i][j] = D[i][j-1] + D[i-1][j] - D[i-1][j-1] + A[i][j];
			}
		}

		for (int i = 0; i < M; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int x1 = Integer.parseInt(st.nextToken());
			int y1 = Integer.parseInt(st.nextToken());
			int x2 = Integer.parseInt(st.nextToken());
			int y2 = Integer.parseInt(st.nextToken());
			// ๊ตฌ๊ฐ„ ํ•ฉ ๋ฐฐ์—ด๋กœ ์งˆ์˜์— ๋‹ต๋ณ€ํ•˜๊ธฐ
			// (x2, y2)  - (ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์˜ ์„ธ๋กœ) - (ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์˜ ๊ฐ€๋กœ) + (๊ฐ€๋กœ, ์„ธ๋กœ๋กœ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„)
			int result = D[x2][y2] - D[x1-1][y2] - D[x2][y1-1] + D[x1-1][y1-1];
			System.out.println(result);
		}
	}
}

/*
----- ์˜ˆ์ œ ์ž…๋ ฅ 1 -----
4 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
2 2 3 4
3 4 3 4
1 1 4 4
----- ์˜ˆ์ œ ์ถœ๋ ฅ 1 -----
27
6
64

 */



๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ด



๊ตฌ๊ฐ„์˜ ํ•ฉ์„ ๊ตฌํ•ด ๋ฐฐ์—ด์— ์ฑ„์›Œ๋„ฃ๋Š” ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋Š”

1
2
3
4
5
6
7
int D[][] = new int[N+1][N+1];
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
		// ๊ตฌ๊ฐ„์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
			 D[i][j] = D[i][j-1] + D[i-1][j] - D[i-1][j-1] + A[i][j];
			}
		}


แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 58 09


๊ตฌ๊ฐ„์˜ ํ•ฉ์œผ๋กœ ์ฑ„์›Œ์ ธ ์žˆ๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ํ•„์š”ํ•œ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋Š”


1
2
3
// ๊ตฌ๊ฐ„ ํ•ฉ ๋ฐฐ์—ด๋กœ ์งˆ์˜์— ๋‹ต๋ณ€ํ•˜๊ธฐ
// (x2, y2)  - (ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์˜ ์„ธ๋กœ) - (ํ•„์š”์—†๋Š” ๋ถ€๋ถ„์˜ ๊ฐ€๋กœ) + (๊ฐ€๋กœ, ์„ธ๋กœ๋กœ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„)
int result = D[x2][y2] - D[x1-1][y2] - D[x2][y1-1] + D[x1-1][y1-1];


แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-03-01 แ„‹แ…ฉแ„’แ…ฎ 10 58 24



This post is licensed under CC BY 4.0 by the author.

๐Ÿฃ ๋ฐฑ์ค€ Q2018 - ์ˆ˜๋“ค์˜ ํ•ฉ5 (java)

/Data structure/ ๐Ÿ’ฌ Priority Queue, Heap