알고리즘/문제풀이

[DP] 구간 합 구하기5

lipnus 2019. 1. 15. 22:42
반응형

백준 11660 구간 합 구하기

https://www.acmicpc.net/submit/11660/11362110


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

static int[][] map;
static int[][] D;

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

map = new int[N+1][N+1];
D = 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());
D[i][j] = D[i][j-1] + D[i-1][j] - D[i-1][j-1] + map[i][j];
}
}

for(int i=0; i<M; 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());
System.out.println( sum(x1,y1,x2,y2) );

}
}

static int sum(int x1, int y1, int x2, int y2) {
return D[x2][y2] - D[x2][y1-1] - D[x1-1][y2] + D[x1-1][y1-1];
}

}


반응형

'알고리즘 > 문제풀이' 카테고리의 다른 글

[다익스트라] 최단경로  (0) 2019.01.20
[DP] 제단  (0) 2019.01.16
[DP] 정수 삼각형  (0) 2019.01.15
[그래프] LCA2  (0) 2019.01.15
파스칼의 삼각형  (0) 2019.01.14