結果
問題 | No.1074 増殖 |
ユーザー |
![]() |
提出日時 | 2023-06-08 08:27:22 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,652 ms / 2,000 ms |
コード長 | 5,231 bytes |
コンパイル時間 | 3,543 ms |
コンパイル使用メモリ | 85,820 KB |
実行使用メモリ | 54,828 KB |
最終ジャッジ日時 | 2024-12-30 15:05:24 |
合計ジャッジ時間 | 12,854 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
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();}}