結果

問題 No.1074 増殖
ユーザー tentententen
提出日時 2023-06-07 20:56:53
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,737 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 93,148 KB
実行使用メモリ 65,400 KB
最終ジャッジ日時 2024-06-09 13:33:49
合計ジャッジ時間 18,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,128 KB
testcase_01 AC 52 ms
37,088 KB
testcase_02 AC 53 ms
37,000 KB
testcase_03 AC 52 ms
37,064 KB
testcase_04 AC 54 ms
36,808 KB
testcase_05 AC 52 ms
37,072 KB
testcase_06 AC 922 ms
51,488 KB
testcase_07 AC 355 ms
50,944 KB
testcase_08 AC 1,867 ms
65,200 KB
testcase_09 TLE -
testcase_10 AC 285 ms
47,504 KB
testcase_11 TLE -
testcase_12 AC 1,746 ms
65,244 KB
testcase_13 AC 1,942 ms
65,400 KB
testcase_14 TLE -
権限があれば一括ダウンロードができます

ソースコード

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;
            for (int i = 0; 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