cgy12306

[백준 BoJ] 11660 - 구간 합 구하기 5 본문

Algorithm/C++

[백준 BoJ] 11660 - 구간 합 구하기 5

cgy12306 2022. 1. 14. 16:26
// https://www.acmicpc.net/problem/11660
// 구간 합 구하기 5
#include<iostream>

using namespace std;
int arr[1026][1026], dp[1026][1026];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int N, M;
	cin >> N >> M;
	int sum = 0;
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			cin >> arr[i][j];
			sum += arr[i][j];
			dp[i][j] = sum;
		}
	}

	for (int m = 0; m < M; m++) {
		int x1, x2, y1, y2, ans = 0;

		cin >> x1 >> y1 >> x2 >> y2;
		for (int x = x1; x <= x2; x++) {
			if (y1 == 1) ans += dp[x][y2] - dp[x - 1][N];
			else ans += dp[x][y2] - dp[x][y1 - 1];
		}
		cout << ans << "\n";
	}
}
  • 누적 합

'Algorithm > C++' 카테고리의 다른 글

[백준 BoJ] 1976 - 여행 가자  (0) 2022.01.17
[백준 BoJ] 10800 - 컬러볼  (0) 2022.01.15
[백준 BoJ] 11659 - 구간 합 구하기 4  (0) 2022.01.14
[백준 BoJ] 11652 - 카드  (0) 2022.01.13
[백준 BoJ] 10825 - 국영수  (0) 2022.01.13
Comments