import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class ple { private static int skipWS(final InputStream in) throws IOException { int val = -1; while ((val = in.read()) != -1) { if (!Character.isWhitespace((char) val)) break; } return val; } private static int readInt(final InputStream in) { try { final StringBuilder b = new StringBuilder(); int val = skipWS(in); b.append((char) val); while ((val = in.read()) != -1) { if (Character.isWhitespace((char) val)) break; b.append((char) val); } return Integer.parseInt(b.toString()); } catch (final IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { final int n = readInt(System.in); MyList<Rect> rects = new MyList<>(); for (int i = 0; i < n; i++) { rects.add(new Rect()); } Node<Rect> nav1 = rects.tail; Node<Rect> nav2; first: while (nav1 != null) { nav2 = nav1.next; while (nav2 != null) { if (nav2.element.intersects(nav1.element)) { nav2.element.merge(nav1.element); rects.mergeNavs(nav1, nav2); nav1 = nav2; continue first; } nav2 = nav2.next; } nav1 = nav1.next; } Rect[] toSort = new Rect[rects.size]; nav1 = rects.tail; for (int i = 0; i < rects.size; i++) { toSort[i] = nav1.element; nav1 = nav1.next; } Arrays.sort(toSort, new RectComparator()); System.out.println(toSort.length); for (int i = 0; i < toSort.length; i++) { System.out.println(toSort[i]); } } private static class RectComparator implements Comparator<Rect> { @Override public int compare(Rect r1, Rect r2) { int result = r1.x1 - r2.x1; if (result == 0) result = r1.x2 - r2.x2; if (result == 0) result = r1.y1 - r2.y1; if (result == 0) result = r1.y2 - r2.y2; return result; } } private static class Rect { int x1, y1, x2, y2; Rect() { x1 = readInt(System.in); x2 = readInt(System.in); y1 = readInt(System.in); y2 = readInt(System.in); } boolean intersects(Rect that) { if (x1 >= that.x1 && x2 <= that.x2 && y1 >= that.y1 && y2 <= that.y2) return true; //embedded if (that.x1 >= x1 && that.x2 <= x2 && that.y1 >= y1 && that.y2 <= y2) return true; //embedded boolean startsX = x1 >= that.x1 && x1 <= that.x2; boolean endsX = x2 >= that.x1 && x2 <= that.x2; boolean startsY = y1 >= that.y1 && y1 <= that.y2; boolean endsY = y2 >= that.y1 && y2 <= that.y2; return (startsX || endsX) && (startsY || endsY); } void merge(Rect that) { if (that.x1 < this.x1) this.x1 = that.x1; if (that.x2 > this.x2) this.x2 = that.x2; if (that.y1 < this.y1) this.y1 = that.y1; if (that.y2 > this.y2) this.y2 = that.y2; } @Override public String toString() { return x1 + " " + x2 + " " + y1 + " " + y2; } } private static class MyList<T> { Node<T> head; Node<T> tail; int size = 0; void add(T element) { if (head == null) { head = new Node<>(element); tail = head; } else { Node<T> newNode = new Node<>(element); head.next = newNode; newNode.prev = head; head = newNode; } size++; } void mergeNavs(Node<T> nav1, Node<T> nav2) { if (size == 2) { nav2.next = null; nav2.prev = null; head = nav2; tail = nav2; } else { //more than 2 elements Node<T> newTail = nav1.next; Node<T> newHead = nav1.prev != null ? nav1.prev : head; newTail.prev = null; newHead.next = null; if (head != newHead) head.next = tail; if (tail != newTail) tail.prev = head; head = newHead; tail = newTail; } size--; } @Override public String toString() { if (head == null) return "[]"; StringBuffer result = new StringBuffer("["); Node<T> ptr = tail; while (ptr != null) { result.append(ptr.element).append(", "); ptr = ptr.next; } result.append(']'); return result.toString(); } } private static class Node<T> { T element; Node<T> prev; Node<T> next; Node(T element) { this.element = element; } @Override public String toString() { return element.toString(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class ple { private static int skipWS(final InputStream in) throws IOException { int val = -1; while ((val = in.read()) != -1) { if (!Character.isWhitespace((char) val)) break; } return val; } private static int readInt(final InputStream in) { try { final StringBuilder b = new StringBuilder(); int val = skipWS(in); b.append((char) val); while ((val = in.read()) != -1) { if (Character.isWhitespace((char) val)) break; b.append((char) val); } return Integer.parseInt(b.toString()); } catch (final IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { final int n = readInt(System.in); MyList<Rect> rects = new MyList<>(); for (int i = 0; i < n; i++) { rects.add(new Rect()); } Node<Rect> nav1 = rects.tail; Node<Rect> nav2; first: while (nav1 != null) { nav2 = nav1.next; while (nav2 != null) { if (nav2.element.intersects(nav1.element)) { nav2.element.merge(nav1.element); rects.mergeNavs(nav1, nav2); nav1 = nav2; continue first; } nav2 = nav2.next; } nav1 = nav1.next; } Rect[] toSort = new Rect[rects.size]; nav1 = rects.tail; for (int i = 0; i < rects.size; i++) { toSort[i] = nav1.element; nav1 = nav1.next; } Arrays.sort(toSort, new RectComparator()); System.out.println(toSort.length); for (int i = 0; i < toSort.length; i++) { System.out.println(toSort[i]); } } private static class RectComparator implements Comparator<Rect> { @Override public int compare(Rect r1, Rect r2) { int result = r1.x1 - r2.x1; if (result == 0) result = r1.x2 - r2.x2; if (result == 0) result = r1.y1 - r2.y1; if (result == 0) result = r1.y2 - r2.y2; return result; } } private static class Rect { int x1, y1, x2, y2; Rect() { x1 = readInt(System.in); x2 = readInt(System.in); y1 = readInt(System.in); y2 = readInt(System.in); } boolean intersects(Rect that) { if (x1 >= that.x1 && x2 <= that.x2 && y1 >= that.y1 && y2 <= that.y2) return true; //embedded if (that.x1 >= x1 && that.x2 <= x2 && that.y1 >= y1 && that.y2 <= y2) return true; //embedded boolean startsX = x1 >= that.x1 && x1 <= that.x2; boolean endsX = x2 >= that.x1 && x2 <= that.x2; boolean startsY = y1 >= that.y1 && y1 <= that.y2; boolean endsY = y2 >= that.y1 && y2 <= that.y2; return (startsX || endsX) && (startsY || endsY); } void merge(Rect that) { if (that.x1 < this.x1) this.x1 = that.x1; if (that.x2 > this.x2) this.x2 = that.x2; if (that.y1 < this.y1) this.y1 = that.y1; if (that.y2 > this.y2) this.y2 = that.y2; } @Override public String toString() { return x1 + " " + x2 + " " + y1 + " " + y2; } } private static class MyList<T> { Node<T> head; Node<T> tail; int size = 0; void add(T element) { if (head == null) { head = new Node<>(element); tail = head; } else { Node<T> newNode = new Node<>(element); head.next = newNode; newNode.prev = head; head = newNode; } size++; } void mergeNavs(Node<T> nav1, Node<T> nav2) { if (size == 2) { nav2.next = null; nav2.prev = null; head = nav2; tail = nav2; } else { //more than 2 elements Node<T> newTail = nav1.next; Node<T> newHead = nav1.prev != null ? nav1.prev : head; newTail.prev = null; newHead.next = null; if (head != newHead) head.next = tail; if (tail != newTail) tail.prev = head; head = newHead; tail = newTail; } size--; } @Override public String toString() { if (head == null) return "[]"; StringBuffer result = new StringBuffer("["); Node<T> ptr = tail; while (ptr != null) { result.append(ptr.element).append(", "); ptr = ptr.next; } result.append(']'); return result.toString(); } } private static class Node<T> { T element; Node<T> prev; Node<T> next; Node(T element) { this.element = element; } @Override public String toString() { return element.toString(); } } } |