728x90
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.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class Main { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException{ String[] str = br.readLine().split(" "); int N = Integer.parseInt(str[0]); int M = Integer.parseInt(str[1]); int[][] arr = new int[N+1][M+1]; int[][] accum = new int[N+1][M+1]; for(int i=1; i<=N; i++) { StringTokenizer stk = new StringTokenizer(br.readLine()," "); for(int j=1; j<=M; j++) { arr[i][j] = Integer.parseInt(stk.nextToken()); accum[i][j] = accum[i][j-1]+accum[i-1][j]-accum[i-1][j-1] + arr[i][j]; } } int K = Integer.parseInt(br.readLine()); for(int m=1; m<=K; m++) { str = br.readLine().split(" "); int i = Integer.parseInt(str[0]); int j = Integer.parseInt(str[1]); int x = Integer.parseInt(str[2]); int y = Integer.parseInt(str[3]); int res = accum[x][y] - accum[i-1][y] - accum[x][j-1] + accum[i-1][j-1]; System.out.println(res); } // bw.write(""); // bw.flush(); // bw.close(); } } | cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 11659번 구간 합 구하기 4 (누적합) (0) | 2021.09.18 |
---|---|
[백준][Java] 11441번 합 구하기 (누적합) (0) | 2021.09.18 |
[백준][Java] 1753번 최단경로 (다익스트라) (0) | 2021.09.17 |
[백준][Java] 2667번 단지번호붙이기 (DFS, BFS) #2 (0) | 2021.09.15 |
[백준][Java] 1260번 DFS와 BFS (DFS, BFS) (0) | 2021.09.15 |