-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11660(DP).java
More file actions
50 lines (40 loc) · 1.61 KB
/
11660(DP).java
File metadata and controls
50 lines (40 loc) · 1.61 KB
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
48
49
import java.util.*;
import java.io.*;
public class Hororop {
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());
int arr[][] = new int[N + 1][N + 1];
for(int i = 1; i < N + 1; i++){
st = new StringTokenizer(br.readLine());
for(int j = 1; j < N + 1; j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
int dp[][] = new int[N + 1][N + 1];
for(int i = 1; i < N + 1; i++){
dp[1][i] = dp[1][i - 1] + arr[1][i];
dp[i][1] = dp[i - 1][1] + arr[i][1];
}
for(int i = 2; i < N + 1; i++) {
for(int j = 2; j < N + 1; j++){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + arr[i][j];
}
}
for(int i = 0; i < M; i++){
st = new StringTokenizer(br.readLine());
int startx = Integer.parseInt(st.nextToken());
int starty = Integer.parseInt(st.nextToken());
int endx = Integer.parseInt(st.nextToken());
int endy = Integer.parseInt(st.nextToken());
int answer = dp[endx][endy] - dp[endx][starty - 1] - dp[startx - 1][endy];
answer += dp[startx - 1][starty - 1];
sb.append(answer).append('\n');
}
System.out.println(sb);
}
}