문제 : https://www.acmicpc.net/problem/11660
[ 알고리즘풀이 ]
( 기존풀이 : https://travelbeeee.tistory.com/288 )
DP[ i ][ j ] : (1, 1) ~ (i , j) 까지 구간의 합. 이라고 정의하면 다음과 같은 점화식이 성립합니다.
DP[i][j] = DP[i - 1][j] + DP[i][j - 1] - DP[i - 1][j - 1] + map[i][j]
#include<iostream>
using namespace std;
int N, M, a, b, c, d, map[1025][1025] = {}, DP[1025][1025] = {};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
cin >> map[i][j];
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
DP[i][j] = DP[i - 1][j] + DP[i][j - 1] + map[i][j] - DP[i - 1][j - 1];
for (int i = 0; i < M; i++) {
cin >> a >> b >> c >> d;
cout << DP[c][d] - DP[c][b - 1] - DP[a - 1][d] + DP[a - 1][b - 1] << '\n';
}
return 0;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 10971 : 외판원 순회 2 - travelbeeee (0) | 2020.02.17 |
---|---|
[BOJ] 3085 : BOMBONI - travelbeeee (0) | 2020.02.17 |
[BOJ] 11660 : 구간 합 구하기 5 - travelbeeee (0) | 2020.02.17 |
[BOJ] 3985 : OREHNJACA - travelbeeee (0) | 2020.02.17 |
[BOJ] 17471 : 게리맨더링 - travelbeeee (0) | 2020.02.17 |