๋ฐ์ํ
๋ฌธ์ (Silver 1)
https://www.acmicpc.net/problem/11660
ํ์ด
์์ด๋์ด
์ค ๋จ์๋ก ๋ํด์ค
[ ][ ][ ][ ][ ]
[ ][ ][o][o][ ] -> o ๋ถ๋ถ ํฉ: sum(2,5) - sum(2,2)
[ ][ ][o][o][ ] -> o ๋ถ๋ถ ํฉ: sum(3,5) - sum(3,2)
o์ ์ต์ข
๋ถ๋ถ ํฉ: ( sum(2,5) - sum(2,2) ) + ( sum(3,5) - sum(3,2) )
์ฝ๋
๋๋ณด๊ธฐ
package implement;
import java.util.*;
import java.io.*;
public class Main_11660_๊ตฌ๊ฐํฉ๊ตฌํ๊ธฐ5 {
public static void main(String[] args) throws Exception{
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());
int[][] map = new int[N][N];
int[][] sum = new int[N][N]; // 0,0๋ถํฐ i,j๊น์ง ํฉ ์ ์ฅ
for(int i =0 ; i < N ; i++){
st = new StringTokenizer(br.readLine());
for(int j =0 ; j < N ; j++){
map[i][j] = Integer.parseInt(st.nextToken());
if(i == 0 && j == 0){
sum[i][j] = map[i][j];
}
else if(j == 0){
sum[i][j] = sum[i-1][N-1]+map[i][j];
} else{
sum[i][j] = sum[i][j-1] + map[i][j];
}
}
}
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < M ; i++){
st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken())-1;
int y1 = Integer.parseInt(st.nextToken())-1;
int x2 = Integer.parseInt(st.nextToken())-1;
int y2 = Integer.parseInt(st.nextToken())-1;
/**
* ์ค ๋จ์๋ก ๋ํด์ค
* [ ][ ][ ][ ][ ]
* [ ][ ][o][o][ ] -> o ๋ถ๋ถ ํฉ: sum(2,5) - sum(2,2)
* [ ][ ][o][o][ ] -> o ๋ถ๋ถ ํฉ: sum(3,5) - sum(3,2)
* => o์ ์ต์ข
๋ถ๋ถ ํฉ: ( sum(2,5) - sum(2,2) ) + ( sum(3,5) - sum(3,2) )
*/
int partial = 0;
for(int j = x1 ; j <= x2 ; j++){
if(y1 == 0 && j == 0) partial += sum[j][y2];
else if(y1 == 0) partial += (sum[j][y2] - sum[j-1][N-1]);
else partial += (sum[j][y2]-sum[j][y1-1]);
}
sb.append(partial+"\n");
}
System.out.println(sb.toString());
}
}
๋ฐ์ํ
'๐ ์๊ณ ๋ฆฌ์ฆ > Implementation' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1253๋ฒ: ์ข๋ค (Java) (0) | 2022.04.13 |
---|---|
[๋ฐฑ์ค] 16639๋ฒ: ๊ดํธ ์ถ๊ฐํ๊ธฐ 3 (0) | 2022.03.29 |
[๋ฐฑ์ค] 9996๋ฒ: ํ๊ตญ์ด ๊ทธ๋ฆฌ์ธ ๋ ์๋ฒ์ ์ ์ํ์ง (Java) (0) | 2022.03.16 |
[๋ฐฑ์ค] 18222๋ฒ ํฌ์-๋ชจ์ค ๋ฌธ์์ด ( Java ) (0) | 2022.02.16 |
[๋ฐฑ์ค] 1032๋ฒ ๋ช ๋ นํ๋กฌํํธ (Java) (0) | 2022.02.16 |