티스토리 뷰

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

 

11660번: 구간 합 구하기 5

첫째 줄에 표의 크기 N과 합을 구해야 하는 횟수 M이 주어진다. (1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) 둘째 줄부터 N개의 줄에는 표에 채워져 있는 수가 1행부터 차례대로 주어진다. 다음 M개의 줄에는 네

www.acmicpc.net

 

  • dp[a][b] -> 기준점에서 (a,b) 점까지의 구간합

import java.util.*;

public class Main {
    public static int N, M;
    public static int[][] table;
    public static int[][] dp;
    
    public static void dynamic(int x1, int y1, int x2, int y2) {
        for(int i = x1; i <= x2; ++i) {
            for(int j = y1; j <= y2; ++j) {
                if(j-1 < y1 && i-1 < x1) dp[i][j] = table[i][j];
                else if(j-1 < y1) dp[i][j] = dp[i-1][j] + table[i][j];
                else if(i-1 < x1) dp[i][j] = dp[i][j-1] + table[i][j];
                else dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] + table[i][j];
            }
        }
    }
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		table = new int[N+1][N+1];
		
		for(int i = 1; i <= N; ++i)
		    for(int j = 1; j <= N; ++j)
		        table[i][j] = sc.nextInt();
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < M; ++i) {
		    int x1 = sc.nextInt();
		    int y1 = sc.nextInt();
		    int x2 = sc.nextInt();
		    int y2 = sc.nextInt();
		    
		    dp = new int[N+1][N+1]; // dp[a][b] -> 기준점에서 (a,b) 점까지의 합
		    dynamic(x1, y1, x2, y2);
		    
		    sb.append(dp[x2][y2]).append("\n");
		}
		
		System.out.print(sb);
	}
}

 

 

  • dp[a][b] -> (1, 1)에서 (a, b)까지의 구간합

import java.util.*;

public class Main {
    public static int N, M;
    public static int[][] table;
    public static int[][] dp;
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		table = new int[N+1][N+1];
		dp = new int[N+1][N+1]; // dp[a][b]는 (1, 1)에서 (a, b)까지의 구간합
		
		for(int i = 1; i <= N; ++i)
		    for(int j = 1; j <= N; ++j)
		        table[i][j] = sc.nextInt();
		
		for(int i = 1; i <= N; ++i) {
            for(int j = 1; j <= N; ++j) {
                if(j-1 < 1 && i-1 < 1) dp[i][j] = table[i][j];
                else if(j-1 < 1) dp[i][j] = dp[i-1][j] + table[i][j];
                else if(i-1 < 1) dp[i][j] = dp[i][j-1] + table[i][j];
                else dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] + table[i][j];
            }
        }
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < M; ++i) {
		    int x1 = sc.nextInt();
		    int y1 = sc.nextInt();
		    int x2 = sc.nextInt();
		    int y2 = sc.nextInt();
		    
		    int ans = dp[x2][y2] - dp[x2][y1-1] - dp[x1-1][y2] + dp[x1-1][y1-1];
		    sb.append(ans + "\n");
		}
		
		System.out.print(sb);
	}
}

'알고리즘 > 문제 풀이' 카테고리의 다른 글

[3차] 압축 with Java  (0) 2024.05.30
k진수에서 소수 개수 구하기 with Java  (0) 2024.05.29
[BOJ12865] 평범한 배낭  (0) 2023.09.25
[BOJ5014] 스타트링크  (1) 2023.09.25
[BOJ11726] 2xn 타일링  (0) 2023.09.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함