Submission #70981670


Source Code Expand

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

public class Main {

    static class Rectangle {
        long x1, x2, y1, y2;

        Rectangle(long x1, long x2, long y1, long y2) {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
        }

        long size() {
            if (x1 > x2 || y1 > y2) {
                return 0;
            }
            return (x2 - x1 + 1) * (y2 - y1 + 1);
        }

        boolean isValid() {
            return x1 <= x2 && y1 <= y2;
        }
    }

    static boolean checkOverlap(long a1, long a2, long b1, long b2) {
        return Math.max(a1, b1) <= Math.min(a2, b2);
    }

    static boolean areAdjacent(Rectangle r1, Rectangle r2) {
        if (checkOverlap(r1.x1, r1.x2, r2.x1, r2.x2)) {
            if (r1.y2 + 1 == r2.y1 || r2.y2 + 1 == r1.y1) {
                return true;
            }
        }
        if (checkOverlap(r1.y1, r1.y2, r2.y1, r2.y2)) {
            if (r1.x2 + 1 == r2.x1 || r2.x2 + 1 == r1.x1) {
                return true;
            }
        }
        return false;
    }

    static class UnionFind {
        int[] parent;
        int count;

        UnionFind(int n) {
            parent = new int[n];
            count = n;
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }

        int find(int i) {
            if (parent[i] == i) {
                return i;
            }
            return parent[i] = find(parent[i]);
        }

        void union(int i, int j) {
            int rootI = find(i);
            int rootJ = find(j);
            if (rootI != rootJ) {
                parent[rootI] = rootJ;
                count--;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        long X = Long.parseLong(st.nextToken());
        long Y = Long.parseLong(st.nextToken());

        char[] C = new char[N];
        long[] A = new long[N];
        long[] B = new long[N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            C[i] = st.nextToken().charAt(0);
            A[i] = Long.parseLong(st.nextToken());
            B[i] = Long.parseLong(st.nextToken());
        }

        List<Rectangle> rects = new ArrayList<>();
        rects.add(new Rectangle(0, X - 1, 0, Y - 1));

        for (int i = 0; i < N; i++) {
            List<Rectangle> nextRects = new ArrayList<>();
            long Ai = A[i];
            long Bi = B[i];

            for (Rectangle r : rects) {
                if (C[i] == 'X') {
                    Rectangle r1 = new Rectangle(r.x1, Math.min(r.x2, Ai - 1), r.y1 - Bi, r.y2 - Bi);
                    Rectangle r2 = new Rectangle(Math.max(r.x1, Ai), r.x2, r.y1 + Bi, r.y2 + Bi);
                    if (r1.isValid()) nextRects.add(r1);
                    if (r2.isValid()) nextRects.add(r2);
                } else {
                    Rectangle r1 = new Rectangle(r.x1 - Bi, r.x2 - Bi, r.y1, Math.min(r.y2, Ai - 1));
                    Rectangle r2 = new Rectangle(r.x1 + Bi, r.x2 + Bi, Math.max(r.y1, Ai), r.y2);
                    if (r1.isValid()) nextRects.add(r1);
                    if (r2.isValid()) nextRects.add(r2);
                }
            }
            rects = nextRects;
        }

        int K = rects.size();
        if (K == 0) {
            System.out.println(0);
            System.out.println();
            return;
        }
        
        UnionFind uf = new UnionFind(K);
        for (int i = 0; i < K; i++) {
            for (int j = i + 1; j < K; j++) {
                if (areAdjacent(rects.get(i), rects.get(j))) {
                    uf.union(i, j);
                }
            }
        }

        Map<Integer, Long> componentSizes = new HashMap<>();
        for (int i = 0; i < K; i++) {
            int root = uf.find(i);
            long size = rects.get(i).size();
            componentSizes.put(root, componentSizes.getOrDefault(root, 0L) + size);
        }

        List<Long> sizes = new ArrayList<>(componentSizes.values());
        Collections.sort(sizes);

        PrintWriter pw = new PrintWriter(System.out);
        pw.println(sizes.size());
        for (int i = 0; i < sizes.size(); i++) {
            pw.print(sizes.get(i));
            if (i < sizes.size() - 1) {
                pw.print(" ");
            }
        }
        pw.println();
        pw.flush();
    }
}

Submission Info

Submission Time
Task D - Suddenly, A Tempest
User addy
Language Java24 (OpenJDK 24.0.2)
Score 425
Code Size 4778 Byte
Status AC
Exec Time 44 ms
Memory 38812 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 2
AC × 96
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt
All 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt, 01-57.txt, 01-58.txt, 01-59.txt, 01-60.txt, 01-61.txt, 01-62.txt, 01-63.txt, 01-64.txt, 01-65.txt, 01-66.txt, 01-67.txt, 01-68.txt, 01-69.txt, 01-70.txt, 01-71.txt, 01-72.txt, 01-73.txt, 01-74.txt, 01-75.txt, 01-76.txt, 01-77.txt, 01-78.txt, 01-79.txt, 01-80.txt, 01-81.txt, 01-82.txt, 01-83.txt, 01-84.txt, 01-85.txt, 01-86.txt, 01-87.txt, 01-88.txt, 01-89.txt, 01-90.txt, 01-91.txt, 01-92.txt, 01-93.txt, 01-94.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 42 ms 38556 KiB
00-sample-02.txt AC 37 ms 38580 KiB
01-01.txt AC 36 ms 38408 KiB
01-02.txt AC 35 ms 38628 KiB
01-03.txt AC 35 ms 38388 KiB
01-04.txt AC 35 ms 38500 KiB
01-05.txt AC 35 ms 38496 KiB
01-06.txt AC 36 ms 38004 KiB
01-07.txt AC 36 ms 38284 KiB
01-08.txt AC 35 ms 38308 KiB
01-09.txt AC 35 ms 38344 KiB
01-10.txt AC 34 ms 38372 KiB
01-11.txt AC 34 ms 38452 KiB
01-12.txt AC 34 ms 38256 KiB
01-13.txt AC 35 ms 38596 KiB
01-14.txt AC 35 ms 38108 KiB
01-15.txt AC 35 ms 38364 KiB
01-16.txt AC 35 ms 38496 KiB
01-17.txt AC 36 ms 38512 KiB
01-18.txt AC 36 ms 38204 KiB
01-19.txt AC 35 ms 38216 KiB
01-20.txt AC 35 ms 38340 KiB
01-21.txt AC 36 ms 38508 KiB
01-22.txt AC 35 ms 38256 KiB
01-23.txt AC 35 ms 38160 KiB
01-24.txt AC 35 ms 38316 KiB
01-25.txt AC 34 ms 38100 KiB
01-26.txt AC 34 ms 38592 KiB
01-27.txt AC 34 ms 38480 KiB
01-28.txt AC 35 ms 38408 KiB
01-29.txt AC 35 ms 38160 KiB
01-30.txt AC 35 ms 38636 KiB
01-31.txt AC 34 ms 38116 KiB
01-32.txt AC 34 ms 38252 KiB
01-33.txt AC 34 ms 38092 KiB
01-34.txt AC 35 ms 38492 KiB
01-35.txt AC 34 ms 38784 KiB
01-36.txt AC 35 ms 38656 KiB
01-37.txt AC 34 ms 38364 KiB
01-38.txt AC 34 ms 38304 KiB
01-39.txt AC 34 ms 38432 KiB
01-40.txt AC 35 ms 38272 KiB
01-41.txt AC 35 ms 38464 KiB
01-42.txt AC 35 ms 38372 KiB
01-43.txt AC 35 ms 38648 KiB
01-44.txt AC 35 ms 38156 KiB
01-45.txt AC 35 ms 38384 KiB
01-46.txt AC 35 ms 38384 KiB
01-47.txt AC 35 ms 38232 KiB
01-48.txt AC 34 ms 38388 KiB
01-49.txt AC 36 ms 38084 KiB
01-50.txt AC 36 ms 38328 KiB
01-51.txt AC 36 ms 38292 KiB
01-52.txt AC 36 ms 38480 KiB
01-53.txt AC 35 ms 38572 KiB
01-54.txt AC 36 ms 38612 KiB
01-55.txt AC 35 ms 38360 KiB
01-56.txt AC 36 ms 38296 KiB
01-57.txt AC 36 ms 38076 KiB
01-58.txt AC 36 ms 38192 KiB
01-59.txt AC 35 ms 38456 KiB
01-60.txt AC 36 ms 38636 KiB
01-61.txt AC 36 ms 38328 KiB
01-62.txt AC 35 ms 38368 KiB
01-63.txt AC 36 ms 37996 KiB
01-64.txt AC 35 ms 38284 KiB
01-65.txt AC 35 ms 38464 KiB
01-66.txt AC 36 ms 38324 KiB
01-67.txt AC 36 ms 38560 KiB
01-68.txt AC 35 ms 38544 KiB
01-69.txt AC 36 ms 38392 KiB
01-70.txt AC 36 ms 38516 KiB
01-71.txt AC 36 ms 38360 KiB
01-72.txt AC 36 ms 38392 KiB
01-73.txt AC 36 ms 38364 KiB
01-74.txt AC 36 ms 38096 KiB
01-75.txt AC 36 ms 38356 KiB
01-76.txt AC 36 ms 38516 KiB
01-77.txt AC 36 ms 38564 KiB
01-78.txt AC 36 ms 38416 KiB
01-79.txt AC 36 ms 38432 KiB
01-80.txt AC 35 ms 38436 KiB
01-81.txt AC 36 ms 38624 KiB
01-82.txt AC 36 ms 38512 KiB
01-83.txt AC 39 ms 38552 KiB
01-84.txt AC 40 ms 38812 KiB
01-85.txt AC 37 ms 38520 KiB
01-86.txt AC 39 ms 38680 KiB
01-87.txt AC 44 ms 38460 KiB
01-88.txt AC 36 ms 38160 KiB
01-89.txt AC 36 ms 38332 KiB
01-90.txt AC 39 ms 38648 KiB
01-91.txt AC 36 ms 38224 KiB
01-92.txt AC 40 ms 38588 KiB
01-93.txt AC 42 ms 38732 KiB
01-94.txt AC 37 ms 38620 KiB