結果

問題 No.1074 増殖
ユーザー tentententen
提出日時 2023-06-08 08:27:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,535 ms / 2,000 ms
コード長 5,231 bytes
コンパイル時間 4,112 ms
コンパイル使用メモリ 90,764 KB
実行使用メモリ 65,304 KB
最終ジャッジ日時 2023-08-28 23:40:56
合計ジャッジ時間 10,211 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,548 KB
testcase_01 AC 40 ms
49,420 KB
testcase_02 AC 41 ms
49,412 KB
testcase_03 AC 43 ms
49,428 KB
testcase_04 AC 43 ms
49,380 KB
testcase_05 AC 44 ms
49,552 KB
testcase_06 AC 372 ms
61,164 KB
testcase_07 AC 356 ms
61,168 KB
testcase_08 AC 1,535 ms
65,280 KB
testcase_09 AC 554 ms
65,180 KB
testcase_10 AC 294 ms
58,300 KB
testcase_11 AC 583 ms
65,020 KB
testcase_12 AC 598 ms
64,884 KB
testcase_13 AC 674 ms
64,864 KB
testcase_14 AC 633 ms
65,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] xlow = new int[n];
        int[] ylow = new int[n];
        int[] xhigh = new int[n];
        int[] yhigh = new int[n];
        Compress xn = new Compress();
        Compress yn = new Compress();
        Compress xp = new Compress();
        Compress yp = new Compress();
        for (int i = 0; i < n; i++) {
            xlow[i] = -sc.nextInt();
            xn.add(xlow[i]);
            ylow[i] = -sc.nextInt();
            yn.add(ylow[i]);
            xhigh[i] = sc.nextInt();
            xp.add(xhigh[i]);
            yhigh[i] = sc.nextInt();
            yp.add(yhigh[i]);
        }
        xn.init();
        yn.init();
        xp.init();
        yp.init();
        Dimension leftDown = new Dimension(xn, yn);
        Dimension leftUp = new Dimension(xn, yp);
        Dimension rightDown = new Dimension(xp, yn);
        Dimension rightUp = new Dimension(xp, yp);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int ans = 
              leftDown.get(xlow[i], ylow[i])
             + leftUp.get(xlow[i], yhigh[i])
             + rightDown.get(xhigh[i], ylow[i])
             + rightUp.get(xhigh[i], yhigh[i]); 
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static class Dimension {
        Compress xCom;
        Compress yCom;
        int[] used;
        
        public Dimension(Compress xCom, Compress yCom) {
            this.xCom = xCom;
            this.yCom = yCom;
            used = new int[yCom.size + 1];
        }
        
        public int get(int xVal, int yVal) {
            int xIdx = xCom.get(xVal);
            int yIdx = yCom.get(yVal);
            int ans = 0;
            int start;
            if (used[0] < xIdx) {
                start = 0;
            } else {
                int left = 0;
                int right = yCom.size;
                while (right - left > 1) {
                    int m = (left + right) / 2;
                    if (used[m] < xIdx) {
                        right = m;
                    } else {
                        left = m;
                    }
                }
                start = right;
            }
            for (int i = start; i < yIdx; i++) {
                ans += xCom.getSum(used[i], xIdx) * yCom.value[i];
                used[i] = Math.max(used[i], xIdx);
            }
            return ans;
        }
    }
    
    static class Compress {
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        int[] value;
        int[] sum;
        int size;
        
        public Compress() {
            compress.put(0, null);
        }
        
        public void add(int x) {
            compress.put(x, null);
        }
        
        public void init() {
            size = compress.size();
            value = new int[size + 1];
            int prev = 0;
            int idx = 0;
            for (int x : compress.keySet()) {
                compress.put(x, idx);
                if (idx > 0) {
                    value[idx - 1] = x - prev;
                }
                prev = x;
                idx++;
            }
            sum = new int[size + 1];
            for (int i = 0; i <= size; i++) {
                if (i  == 0) {
                    sum[i] = value[i];
                } else {
                    sum[i] = sum[i - 1] + value[i];
                }
            }
        }
        
        public int get(int x) {
            return compress.get(x);
        }
        
        public int getSum(int from, int to) {
            if (to <= from) {
                return 0;
            }
            if (from == 0) {
                return sum[to - 1];
            } else {
                return sum[to - 1] - sum[from - 1];
            }
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0