1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 1


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/11660

image

image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ดค์„๋•Œ, ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ฐ”๋กœ ๊ฐ์ด ์žกํžˆ์ง€๋Š” ์•Š์•˜๋‹ค.
๊ทธ๋ž˜์„œ ์ด์ „์— ํ’€์–ด๋ณธ ๋ˆ„์ ํ•ฉ ๋ฌธ์ œ๋“ค์„ ๋– ์˜ฌ๋ฆฌ๋ฉฐ ์ฐฌ์ฐฌํžˆ ๊ณ ๋ฏผํ•ด ๋ณด์•˜๋‹ค.
๊ฒฐ๊ตญ ๋ชจ๋“  ๋ˆ„์ ํ•ฉ ๋ฌธ์ œ๋“ค์€ ์ด์ „๊นŒ์ง€์˜ ์ˆซ์ž๋“ค์„ ๋”ํ•œ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์ด ์žˆ์–ด์•ผํ•˜๊ณ , ๊ทธ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์— ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š”์ง€๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค.

๊ทธ๋ž˜์„œ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด์—๋Š” ์ด์ „ ์ขŒํ‘œ๊ฐ’๊นŒ์ง€์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•œ ํ›„, ๋„ฃ๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ณ„์‚ฐ์„ ํ• ๋•Œ๋Š” ํ˜„์žฌ (x2, y2)๊ฐ’์—์„œ (x1 - 1, y2)์™€ (x2, y1 - 1)์„ ๋นผ๊ณ  (x1 - 1, y1 - 1)๋ฅผ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿฌ์ž ๋‹ค์Œ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์„ฑ๊ณต์ ์œผ๋กœ ๊ตฌํ˜„์ด ๋˜์—ˆ๋‹ค.


๐Ÿ“Œ Code

package PS;

import java.io.*;
import java.util.*;

public class N11660 {
    static int[][] numbers;

    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());

        numbers = new int[N + 1][N + 1];

        for (int i = 0; i <= N; i++) {
            numbers[i][0] = 0;
            numbers[0][i] = 0;
        }
        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= N; j++) {
                MakePrefixSum(i, j, Integer.parseInt(st.nextToken()));
            }
        }
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            PrefixSum(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }
    }

    static void MakePrefixSum(int x, int y, int value) {
        numbers[x][y] = numbers[x][y - 1] + numbers[x - 1][y] - numbers[x - 1][y - 1] + value;
    }

    static void PrefixSum(int x2, int y2, int x1, int y1) {
        int result = numbers[x1][y1] - numbers[x2 - 1][y1] - numbers[x1][y2 - 1] + numbers[x2 - 1][y2 - 1];
        System.out.println(result);
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜