[백준 11660] 구간 합 구하기 5
- 💾 알고리즘/LeetCode_백준
- 2022. 6. 22. 10:55
🔗 문제
https://www.acmicpc.net/problem/11660
11660번: 구간 합 구하기 5
첫째 줄에 표의 크기 N과 합을 구해야 하는 횟수 M이 주어진다. (1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) 둘째 줄부터 N개의 줄에는 표에 채워져 있는 수가 1행부터 차례대로 주어진다. 다음 M개의 줄에는 네
www.acmicpc.net
💻 코드
package org.kyhslam.bakjun;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class p11660 {
public static void main(String[] args) throws Exception {
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 N = 4;
//int M = 3;
System.out.println("N = " + N);
System.out.println("M = " + M);
int[][] A = new int[N + 1][N + 1];
/*
int[][] A = {
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 4},
{0, 2, 3, 4, 5},
{0, 3, 4, 5, 6},
{0, 4, 5, 6, 7}
};
*/
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());
}
}
System.out.println("Arrays.toString(A) = " + Arrays.deepToString(A));
arrView(A);
//구간의 합
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];
}
}
arrView(D);
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());
int result = D[x2][y2] -D[x1-1][y2] - D[x2][y1-1] + D[x1-1][y1- 1];
System.out.println("result = " + result);
}
}
// index 1부터 시작
public static void arrView(int[][] arr) {
System.out.println("-- arrView start -- ");
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}
System.out.println("-- arrView end -- ");
}
}
'💾 알고리즘 > LeetCode_백준' 카테고리의 다른 글
[백준 17298] 오큰수 (1) | 2022.09.26 |
---|---|
[백준 1940] 주몽 - 투 포인터 (0) | 2022.06.27 |
[LeetCode] 561. Array Partition I (0) | 2022.06.07 |
[LeetCode] 15. 세수의 합 (0) | 2022.05.31 |
[LeetCode] 42. Trapping Rain Water (빗물 트래핑) (0) | 2022.05.16 |