import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.StringTokenizer; public class ple { public static class Tribe { private int x0; private int x1; private int y0; private int y1; private int xy0; private int xy1; public Tribe(int x0, int x1, int y0, int y1) { this.x0 = x0; this.x1 = x1; this.y0 = y0; this.y1 = y1; this.xy0 = this.x0 + this.y0; this.xy1 = this.x1 + this.y1; } public int getX0() { return x0; } public int getX1() { return x1; } public int getY0() { return y0; } public int getY1() { return y1; } public int getXy0() { return xy0; } public int getXy1() { return xy1; } @Override public String toString() { return x0 + " " + x1 + " " + y0 + " " + y1; } } public static class TribeX0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getX0(), arg1.getX0()); } } public static class TribeX1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getX1(), arg1.getX1()); } } public static class TribeY0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getY0(), arg1.getY0()); } } public static class TribeY1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getY1(), arg1.getY1()); } } public static class TribeXy0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getXy0(), arg1.getXy0()); } } public static class TribeXy1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getXy1(), arg1.getXy1()); } } public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(buffReader.readLine()); PriorityQueue<Tribe> pingQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); PriorityQueue<Tribe> pongQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); LinkedList<Tribe> testList = new LinkedList<Tribe>(); for(int i=0; i<n; i++){ StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); pingQueue.add(new Tribe(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()))); } int pingQueueSize = pingQueue.size(); int pongQueueSize = pongQueue.size(); while(pingQueueSize != pongQueueSize){ testList.add(pingQueue.poll()); while(!pingQueue.isEmpty()){ Tribe first = pingQueue.poll(); Iterator<Tribe> testTribeIter = testList.iterator(); boolean addTest = true; while(testTribeIter.hasNext() && addTest){ Tribe testTribe = testTribeIter.next(); if(testTribe.getXy1() < first.getXy0()){ pongQueue.add(testTribe); testTribeIter.remove(); }else{ if(first.getX0() < testTribe.getX1() && first.getY0() < testTribe.getY1() && first.getX1() > testTribe.getX0() && first.getY1() > testTribe.getY0()){ pingQueue.add(new Tribe(Math.min(first.getX0(), testTribe.getX0()), Math.max(first.getX1(), testTribe.getX1()), Math.min(first.getY0(), testTribe.getY0()), Math.max(first.getY1(), testTribe.getY1()) )); testTribeIter.remove(); addTest = false; } } } if(addTest) testList.add(first); } Iterator<Tribe> testTribeIter = testList.iterator(); while(testTribeIter.hasNext()){ pongQueue.add(testTribeIter.next()); testTribeIter.remove(); } pingQueue = pongQueue; pongQueueSize = pingQueueSize; pingQueueSize = pingQueue.size(); pongQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); } Tribe[] tribeMap = pingQueue.toArray(new Tribe[0]); Arrays.sort(tribeMap, new TribeY1Comparator()); Arrays.sort(tribeMap, new TribeY0Comparator()); Arrays.sort(tribeMap, new TribeX1Comparator()); Arrays.sort(tribeMap, new TribeX0Comparator()); System.out.println(tribeMap.length); for(Tribe tribe : tribeMap) System.out.println(tribe); }catch(Exception e){ e.printStackTrace(); } } }
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.StringTokenizer; public class ple { public static class Tribe { private int x0; private int x1; private int y0; private int y1; private int xy0; private int xy1; public Tribe(int x0, int x1, int y0, int y1) { this.x0 = x0; this.x1 = x1; this.y0 = y0; this.y1 = y1; this.xy0 = this.x0 + this.y0; this.xy1 = this.x1 + this.y1; } public int getX0() { return x0; } public int getX1() { return x1; } public int getY0() { return y0; } public int getY1() { return y1; } public int getXy0() { return xy0; } public int getXy1() { return xy1; } @Override public String toString() { return x0 + " " + x1 + " " + y0 + " " + y1; } } public static class TribeX0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getX0(), arg1.getX0()); } } public static class TribeX1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getX1(), arg1.getX1()); } } public static class TribeY0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getY0(), arg1.getY0()); } } public static class TribeY1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getY1(), arg1.getY1()); } } public static class TribeXy0Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getXy0(), arg1.getXy0()); } } public static class TribeXy1Comparator implements Comparator<Tribe> { @Override public int compare(Tribe arg0, Tribe arg1) { return Integer.compare(arg0.getXy1(), arg1.getXy1()); } } public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(buffReader.readLine()); PriorityQueue<Tribe> pingQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); PriorityQueue<Tribe> pongQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); LinkedList<Tribe> testList = new LinkedList<Tribe>(); for(int i=0; i<n; i++){ StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); pingQueue.add(new Tribe(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()))); } int pingQueueSize = pingQueue.size(); int pongQueueSize = pongQueue.size(); while(pingQueueSize != pongQueueSize){ testList.add(pingQueue.poll()); while(!pingQueue.isEmpty()){ Tribe first = pingQueue.poll(); Iterator<Tribe> testTribeIter = testList.iterator(); boolean addTest = true; while(testTribeIter.hasNext() && addTest){ Tribe testTribe = testTribeIter.next(); if(testTribe.getXy1() < first.getXy0()){ pongQueue.add(testTribe); testTribeIter.remove(); }else{ if(first.getX0() < testTribe.getX1() && first.getY0() < testTribe.getY1() && first.getX1() > testTribe.getX0() && first.getY1() > testTribe.getY0()){ pingQueue.add(new Tribe(Math.min(first.getX0(), testTribe.getX0()), Math.max(first.getX1(), testTribe.getX1()), Math.min(first.getY0(), testTribe.getY0()), Math.max(first.getY1(), testTribe.getY1()) )); testTribeIter.remove(); addTest = false; } } } if(addTest) testList.add(first); } Iterator<Tribe> testTribeIter = testList.iterator(); while(testTribeIter.hasNext()){ pongQueue.add(testTribeIter.next()); testTribeIter.remove(); } pingQueue = pongQueue; pongQueueSize = pingQueueSize; pingQueueSize = pingQueue.size(); pongQueue = new PriorityQueue<Tribe>(n, new TribeXy0Comparator()); } Tribe[] tribeMap = pingQueue.toArray(new Tribe[0]); Arrays.sort(tribeMap, new TribeY1Comparator()); Arrays.sort(tribeMap, new TribeY0Comparator()); Arrays.sort(tribeMap, new TribeX1Comparator()); Arrays.sort(tribeMap, new TribeX0Comparator()); System.out.println(tribeMap.length); for(Tribe tribe : tribeMap) System.out.println(tribe); }catch(Exception e){ e.printStackTrace(); } } } |