Java/공부

누적합

feel2 2023. 4. 12. 16:06
반응형
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static void input(){
    }
    static void pro() {

//        int[] imos = new int[]{0,1,1,1,0,0,0,0,-2,0,0,-1};
//        int now = 0;
//        for(int i=0; i< 12; i++) {
//            now +=imos[i];
//            imos[i] = now;
//        }

        // 데이터를 입력한다
        int[][] query1 = new int[][]{{1,1},{4,4}};
        int[][] query2 = new int[][]{{4,3},{6,5}};
        int[][] query3 = new int[][]{{2,4},{5,6}};

        int N = 8;

        Queue<int[][]> queue = new LinkedList();

        queue.add(query1);
        queue.add(query2);
        queue.add(query3);

        int[][] imos = new int[N][N];

        for(int i = 0; i < 3; i++){
            int[][] val = queue.poll();

            int sx = val[0][0];
            int sy = val[0][1];

            int ex = val[1][0];
            int ey = val[1][1];

            imos[sx][sy] += 1;
            imos[ex+1][ey+1] += 1;
            imos[sx][ey+1] -= 1;
            imos[ex+1][sy] -= 1;


        }
        // 누적합을 이용해 중첩 구간 개수를 구한다
        // → 휩쓸기
        for(int i = 1; i < N; i++){
            for(int j = 1; j < N; j++){
                imos[i][j] += imos[i][j-1];
            }

        }
        // ↓ 휩쓸기
        for(int i = 1; i < N; i++){
            for(int j = 1; j < N; j++){
                imos[j][i] += imos[j-1][i];
            }

        }

        System.out.println("ddd");

    }

    public static void main(String[] args) {
        input();
        pro();
    }


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

 

아래 누적합에 대한 설명을 읽고 2차원 배열에 대한 누적합이 어떻게 구현되는지 코드로 간단하게 짜보았다.

https://driip.me/65d9b58c-bf02-44bf-8fba-54d394ed21e0

반응형