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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<functional>
#include<cstdbool>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 987654321
#define LINF 321321321321
#define MAX 1100
int n, m;
int board[1025][1025];
int dp[1025][1025];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> board[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] - dp[i - 1][j - 1] + board[i][j];
}
}
for (int k = 0; k < m; k++) {
int y1, x1, y2, x2;
cin >> y1 >> x1 >> y2 >> x2;
cout << dp[y2][x2] - dp[y1 - 1][x2] - dp[y2][x1 - 1] + dp[y1 - 1][x1 - 1] << "\n";
}
return 0;
}
|
cs |
출처:https://www.acmicpc.net/problem/11660
로직엔 문제없었는데 시간초과가 뜨는걸 보니 입력이 너무많아서
ios_base::sync_with_stdio(false);
cin.tie(0);
을 추가해주고 정답처리 되었습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
[c++] 백준 2618번 경찰차 (dp) (0) | 2022.10.12 |
---|---|
[c++] 백준 1102번 발전소 (tsp) (0) | 2022.10.12 |
[c++] 1043번 거짓말 (유니온 파인드) (0) | 2022.09.27 |
[c++] 백준 1976번 여행 가자 (유니온-파인드) (0) | 2022.09.27 |
백준 17143번 낚시왕 (0) | 2021.05.23 |