BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
를 써서 시간을 최대한 줄여줘야했던 문제였다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// 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());
int num[][] = 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++) {
num[i][j]=num[i][j-1]+Integer.parseInt(st.nextToken());
}
}
StringBuilder sb = new StringBuilder();
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());
int sum=0;
for(int k=x1; k<=x2; k++) {
sum=sum+num[k][y2]-num[k][y1-1];
}
sb.append(sum+"\n");
}
System.out.println(sb);
}
}
'알고리즘 > 문제풀이' 카테고리의 다른 글
백준 2493 (0) | 2022.08.09 |
---|---|
sw expert 1228 (0) | 2022.08.08 |
백준 10826 피보나치 수 4 (0) | 2022.08.02 |
백준 1929 소수 구하기 (0) | 2022.08.02 |
백준 1244 (0) | 2022.08.02 |
댓글