Notice
Recent Posts
Recent Comments
Link
Dev.baelanche
[백준 16507] 어두운 건 무서워 본문
반응형
2차원 배열 구간합을 구현하면 된다.
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
int psum[][] = new int[r+1][c+1];
for(int i=1; i<=r; i++) {
st = new StringTokenizer(br.readLine());
for(int j=1; j<=c; j++)
psum[i][j] = psum[i-1][j] + psum[i][j-1] - psum[i-1][j-1] + Integer.parseInt(st.nextToken());
}
for(int i=0; i<q; i++) {
st = new StringTokenizer(br.readLine());
int r1 = Integer.parseInt(st.nextToken());
int c1 = Integer.parseInt(st.nextToken());
int r2 = Integer.parseInt(st.nextToken());
int c2 = Integer.parseInt(st.nextToken());
int cnt = ((r2 - r1) + 1) * ((c2 - c1) + 1);
System.out.println((psum[r2][c2] - psum[r1-1][c2] - psum[r2][c1-1] + psum[r1-1][c1-1]) / cnt);
}
}
}
반응형
'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글
[백준 1717] 집합의 표현 (0) | 2019.06.17 |
---|---|
[백준 16713] Generic Queries (0) | 2019.06.17 |
[백준 10211] Maximum Subarray (0) | 2019.06.16 |
[백준 11969] Breed Counting (0) | 2019.06.16 |
[백준 11441] 합 구하기 (0) | 2019.06.16 |
Comments