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));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
int[][] arr = 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++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
int[][] sum = new int[n+1][n+1];
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + arr[i][j];
}
}
for(int i=0; i<q; i++) {
st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
int result = sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];
System.out.println(result);
}
br.close();
}
}
'Coding Test > 백준' 카테고리의 다른 글
[백준/자바] 2884 - 알람 시계 (0) | 2023.05.06 |
---|---|
[백준/자바] 11382 - 꼬마 정민 (0) | 2023.05.05 |
[백준/자바] 11659 - 구간 합 구하기 4 (0) | 2023.05.04 |
[백준/자바] 1546 - 평균 (0) | 2023.05.04 |
[백준/자바] 11720 - 숫자의 합 (0) | 2023.05.04 |