BOJ_S1_11660
π [S1_11660] κ΅¬κ° ν© κ΅¬νκΈ° 5
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
st = new StringTokenizer(br.readLine(), " ");
// νμ ν¬κΈ°
int N = Integer.parseInt(st.nextToken());
// ν©μ ꡬν΄μΌ νλ νμ
int M = Integer.parseInt(st.nextToken());
// map
int[][] map = new int[N+1][N+1];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 1; j <= N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
// DP μ μ₯ λ°°μ΄ -> ( 1,1 ) μμ ( i,j ) κΉμ§μ ν©
int[][] dp = new int[N+1][N+1];
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] + map[i][j];
}
}
// λ²μ
int x1,y1,x2,y2;
for (int i = 1; i <= M; i++) {
st = new StringTokenizer(br.readLine(), " ");
x1 = Integer.parseInt(st.nextToken());
y1 = Integer.parseInt(st.nextToken());
x2 = Integer.parseInt(st.nextToken());
y2 = Integer.parseInt(st.nextToken());
sb.append((dp[x2][y2] -dp[x2][y1-1]-dp[x1-1][y2] +dp[x1-1][y1-1]) + "\n");
}
System.out.println(sb);
}
}
π€ λμ μκ°
μ΄ λ¬Έμ μλ μ¬λ¬κ°μ§ μ£Όμμ μ΄ μμλ€β¦
- x,y μ’ν μμκ° μΌλ°μ μ΄μ§ μλ€λ μ ..
- (x1,y1) μμ (x2,y2) κΉμ§ ꡬνλλ° μ’νλ₯Ό λ€ λλ κ²μ΄ μλλΌ μ¬κ°νμΌλ‘ ꡬνλ κ²μ΄λ€.
- λ°μ΄ν° λ²μκ° λμ΄μ dpλ₯Ό μ¬μ©ν΄μΌμ§ μκ°μ΄κ³Όκ° μλ¬λ€.
λλ ν΄κ²°νμ§ λͺ»ν΄ μ°Έκ³ νλλ° μ€λͺ
μ΄ μμ£Ό μλμ΄μλ€.
μ°Έκ³ νλ©΄ μ’μ κ² κ°λ€.
π μ°Έμ‘°
https://subbak2.tistory.com/65