[BOJ] 구간합 구하기5

박신희·2022년 8월 3일
0

[풀이] BOJ

목록 보기
4/7
post-thumbnail

❗ 풀이 과정

  • 구간합을 2차원으로 생각하면 된다..ㅎ

🤜 풀이코드

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 {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int N = Integer.parseInt(st.nextToken());
		int T = Integer.parseInt(st.nextToken());
		int [][] map = new int [N+1][N+1]; 
		
		// init border 0
		for(int i=0;i<=N;i++) {
			map[i][0]=0;
			map[0][i]=0;
		}
		
		for(int h=1;h<=N;h++) {
			st = new StringTokenizer(br.readLine());
			for(int w=1;w<=N;w++) {
				map[h][w]=Integer.parseInt(st.nextToken());
				map[h][w]= map[h][w]+map[h-1][w]+map[h][w-1]-map[h-1][w-1];
			}
		}
		
		for(int t=0;t<T;t++) {
			int sum=0;
			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(map[x2][y2]-map[x1-1][y2]-map[x2][y1-1]+map[x1-1][y1-1]);
		}
		
	}
}
profile
log my moments 'u')/

0개의 댓글