Notice
Recent Posts
Recent Comments
05-21 07:17
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Byeol Lo

N11660 - 구간 합 구하기 5 본문

Algorithm/Accumulated Sum

N11660 - 구간 합 구하기 5

알 수 없는 사용자 2024. 2. 13. 13:14
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.StringTokenizer;
import java.util.Arrays;


public class Main {
    static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static final BufferedWriter BW = new BufferedWriter(new OutputStreamWriter(System.out));
    static final StringBuffer SB = new StringBuffer();
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {
        st = new StringTokenizer(BR.readLine());
        String tmp;
        int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
        long[][] table = new long[n+1][n+1];
        long result;
        int x1, y1, x2, y2;

        for(int i = 1; i <= n; i++) {
            st = new StringTokenizer(BR.readLine());
            for(int j = 1; j <= n; j++)
                table[i][j] = Long.parseLong(st.nextToken()) + table[i-1][j]
                    + table[i][j-1] - table[i-1][j-1];
        }

        while((tmp = BR.readLine()) != null) {
            st = new StringTokenizer(tmp);
            x1 = Integer.parseInt(st.nextToken());
            y1 = Integer.parseInt(st.nextToken());
            x2 = Integer.parseInt(st.nextToken());
            y2 = Integer.parseInt(st.nextToken());

            result = table[x2][y2] - table[x2][y1-1] - table[x1-1][y2] + table[x1-1][y1-1];
            SB.append(result).append("\n");
        }

        BW.write(SB.toString());
        BW.close();
        BR.close();
    }
}

'Algorithm > Accumulated Sum' 카테고리의 다른 글

N25682 - 체스판 다시 칠하기 2  (1) 2024.02.13
N10986 - 나머지 합  (0) 2024.02.13
Comments