๋ฌธ์
https://www.acmicpc.net/problem/11660
ํ์ด
# ๋ฐฑ์ค 11660๋ฒ ๋ฌธ์ - ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 5
n, m = map(int, input().split())
graph = []
graph = [list(map(int, input().split())) for _ in range(n)]
dp = [[0] * (n+1) for _ in range(n+1)]
for i in range(1, n+1):
for j in range(1, n+1):
dp[i][j] = dp[i-1][j]+ dp[i][j-1] - dp[i-1][j-1] + graph[i-1][j-1]
for i in range(m):
x1, y1, x2, y2 = map(int, input().split())
result = dp[x2][y2] - dp[x2][y1-1] - dp[x1-1][y2] + dp[x1-1][y1-1]
print(result)
์ฒ์์ ์ดํด๋ฅผ ํ๋๋ฐ ์ ๋ฅผ ๋จน์ด์ ๋ค๋ฅธ ํ์ด๋ฅผ ์ฐพ์๋ณด์๋ค.
๊ฐ์ฅ ์ฝ๊ฒ ์ดํดํ ์ ์์๋ ํ์ด๋ ์์ญ์ ๋๋์ด์ x1, x2, y1, y2 ์ขํ๋ฅผ ๋ณด๊ณ ๊ณตํต๋๋ ๋ถ๋ถ์ ๋ค์ ๋ํด์ฃผ๋ ํ์ด์๋ค.
DP ๋ฌธ์ ๋ ๋ง์ด ํ์ด๋ด์ผ๊ฒ ๋ค.
** sys ๋ชจ๋์ ์ฌ์ฉํ์ง ์์ผ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋๊ฒ ๋๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold2] 1202๋ฒ_๋ณด์ ๋๋ (0) | 2023.03.30 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1715๋ฒ_์นด๋ ์ ๋ ฌํ๊ธฐ (0) | 2023.03.28 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 18111๋ฒ_๋ง์ธํฌ๋ํํธ (0) | 2023.03.20 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 11000๋ฒ_๊ฐ์์ค ๋ฐฐ์ (0) | 2023.03.19 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver4] 10825๋ฒ_๊ตญ์์ (0) | 2023.03.18 |