Skip to content

Commit 03fde46

Browse files
committed
백준 11660번 구간 합 구하기 5
문제 링크: https://www.acmicpc.net/problem/11660
1 parent 5de3a79 commit 03fde46

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
3+
4+
def solve():
5+
N, M = map(int, sys.stdin.readline().strip().split(' '))
6+
maps = [[0 for _ in range(N + 1)]]
7+
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
8+
9+
for _ in range(N):
10+
line = list(map(int, sys.stdin.readline().strip().split(' ')))
11+
maps.append([0] + line)
12+
13+
for idx_row in range(1, N + 1):
14+
for idx_col in range(1, N + 1):
15+
dp[idx_row][idx_col] = dp[idx_row - 1][idx_col] + dp[idx_row][idx_col - 1] - dp[idx_row - 1][idx_col - 1] + maps[idx_row][idx_col]
16+
17+
for _ in range(M):
18+
y1, x1, y2, x2 = map(int, sys.stdin.readline().strip().split(' '))
19+
v = map_sum(dp, y1, x1, y2, x2)
20+
print(v)
21+
22+
23+
def map_sum(dp, y1, x1, y2, x2):
24+
rs, rc = y1 - 1, x1 - 1
25+
return dp[y2][x2] - (dp[y2][rc] + dp[rs][x2]) + dp[rs][rc]
26+
27+
28+
if __name__ == '__main__':
29+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
4 3
2+
1 2 3 4
3+
2 3 4 5
4+
3 4 5 6
5+
4 5 6 7
6+
2 2 3 4
7+
3 4 3 4
8+
1 1 4 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
27
2+
6
3+
64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2 4
2+
1 2
3+
3 4
4+
1 1 1 1
5+
1 2 1 2
6+
2 1 2 1
7+
2 2 2 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
2
3+
3
4+
4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')
24+
25+
def test2_solve(self):
26+
self.test_solve('2')

0 commit comments

Comments
 (0)