結果

問題 No.1074 増殖
ユーザー tentententen
提出日時 2023-06-08 08:27:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,625 ms / 2,000 ms
コード長 5,231 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 92,116 KB
実行使用メモリ 55,140 KB
最終ジャッジ日時 2024-06-09 18:10:13
合計ジャッジ時間 10,176 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
36,996 KB
testcase_01 AC 53 ms
37,100 KB
testcase_02 AC 51 ms
37,200 KB
testcase_03 AC 50 ms
37,308 KB
testcase_04 AC 50 ms
36,948 KB
testcase_05 AC 50 ms
37,272 KB
testcase_06 AC 368 ms
51,448 KB
testcase_07 AC 358 ms
51,228 KB
testcase_08 AC 1,625 ms
54,816 KB
testcase_09 AC 548 ms
55,140 KB
testcase_10 AC 292 ms
48,644 KB
testcase_11 AC 585 ms
53,732 KB
testcase_12 AC 576 ms
53,472 KB
testcase_13 AC 606 ms
54,224 KB
testcase_14 AC 613 ms
54,276 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