Algorithm/problem
백준 11660번 : 구간 합 구하기 5 - 누적합 C++
DingCoDing
2022. 3. 9. 14:31
반응형
https://www.acmicpc.net/problem/11660
11660번: 구간 합 구하기 5
첫째 줄에 표의 크기 N과 합을 구해야 하는 횟수 M이 주어진다. (1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) 둘째 줄부터 N개의 줄에는 표에 채워져 있는 수가 1행부터 차례대로 주어진다. 다음 M개의 줄에는 네
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n, m;
int psum[1025][1025];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int a;
cin >> a;
psum[i + 1][j + 1] = psum[i + 1][j] + psum[i][j + 1] - psum[i][j] + a;
}
}
for (int i = 0; i < m; i++) {
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << psum[x2][y2] - psum[x2][y1 - 1] - psum[x1 - 1][y2] + psum[x1-1][y1-1] << '\n';
}
return 0;
}
반응형