import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; public class ple { public static void main(String[] args) throws IOException { ple solver = new ple(); solver.init(); solver.solve(); } private void init() { } private static class Rect implements Comparable<Rect>{ int x1,x2,y1,y2; int spreadedBy = -1; public Rect(int x1, int x2, int y1, int y2) { super(); this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } public boolean cross(Rect r) { if ((x1 >= r.x1 && x1 < r.x2) || (r.x1 >= x1 && r.x1 < x2)) { if ((y1 >= r.y1 && y1 < r.y2) || (r.y1 >= y1 && r.y1 < y2)) { return true; } } return false; } public void spread(Rect r) { this.x1 = Math.min(this.x1, r.x1); this.x2 = Math.max(this.x2, r.x2); this.y1 = Math.min(this.y1, r.y1); this.y2 = Math.max(this.y2, r.y2); } @Override public int compareTo(Rect rect) { if (this.x1 == rect.x1) { if (this.x2 == rect.x2) { if (this.y1 == rect.y1) { return this.y2 - rect.y2; } else { return this.y1 - rect.y1; } } else { return this.x2 - rect.x2; } } else { return this.x1 - rect.x1; } } } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); Rect[] rects2 = new Rect[n]; for (int i = 0 ; i < rects2.length; i++) { rects2[i] = new Rect(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()); } List<Integer> order = new ArrayList<Integer>(n); for (int i = 0; i < n; i++) order.add(i); Collections.shuffle(order); Rect[] rects = new Rect[n]; int rectsI = 0; for (Integer idx : order) { rects[rectsI++] = rects2[idx]; } Set<Integer> toUse = new HashSet<Integer>(); for (int i = 0; i < n; i++) toUse.add(i); Set<Integer> newRects = new HashSet<Integer>(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if(rects[i].cross(rects[j])) { if (rects[j].spreadedBy == -1) { rects[i].spread(rects[j]); newRects.add(i); toUse.remove(j); rects[j].spreadedBy = i; } else { rects[rects[j].spreadedBy].spread(rects[i]); toUse.remove(i); rects[i].spreadedBy = rects[j].spreadedBy; break; } } } } while (!newRects.isEmpty()) { Set<Integer> usedRects = new HashSet<Integer>(); Set<Integer> toRemove = new HashSet<Integer>(); for (Integer i : newRects) if (rects[i].spreadedBy == -1) { for (int j : toUse) if (!i.equals(j)) { if (rects[i].cross(rects[j])) { if (rects[j].spreadedBy == -1) { rects[i].spread(rects[j]); usedRects.add(i); toRemove.add(j); rects[j].spreadedBy = i; } else { rects[rects[j].spreadedBy].spread(rects[i]); toRemove.add(i); rects[i].spreadedBy = rects[j].spreadedBy; break; } } } } for (Integer i : toRemove) toUse.remove(i); newRects = usedRects; } //TODO sort lexigography and print toUse Rect[] ans = new Rect[toUse.size()]; int i = 0; for (Integer idx : toUse) { ans[i++] = rects[idx]; } Arrays.sort(ans); out.println(ans.length); for (Rect r : ans) { out.println(r.x1 + " " + r.x2 + " " + r.y1 + " " + r.y2); } out.flush(); out.close(); } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; public class ple { public static void main(String[] args) throws IOException { ple solver = new ple(); solver.init(); solver.solve(); } private void init() { } private static class Rect implements Comparable<Rect>{ int x1,x2,y1,y2; int spreadedBy = -1; public Rect(int x1, int x2, int y1, int y2) { super(); this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } public boolean cross(Rect r) { if ((x1 >= r.x1 && x1 < r.x2) || (r.x1 >= x1 && r.x1 < x2)) { if ((y1 >= r.y1 && y1 < r.y2) || (r.y1 >= y1 && r.y1 < y2)) { return true; } } return false; } public void spread(Rect r) { this.x1 = Math.min(this.x1, r.x1); this.x2 = Math.max(this.x2, r.x2); this.y1 = Math.min(this.y1, r.y1); this.y2 = Math.max(this.y2, r.y2); } @Override public int compareTo(Rect rect) { if (this.x1 == rect.x1) { if (this.x2 == rect.x2) { if (this.y1 == rect.y1) { return this.y2 - rect.y2; } else { return this.y1 - rect.y1; } } else { return this.x2 - rect.x2; } } else { return this.x1 - rect.x1; } } } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); Rect[] rects2 = new Rect[n]; for (int i = 0 ; i < rects2.length; i++) { rects2[i] = new Rect(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()); } List<Integer> order = new ArrayList<Integer>(n); for (int i = 0; i < n; i++) order.add(i); Collections.shuffle(order); Rect[] rects = new Rect[n]; int rectsI = 0; for (Integer idx : order) { rects[rectsI++] = rects2[idx]; } Set<Integer> toUse = new HashSet<Integer>(); for (int i = 0; i < n; i++) toUse.add(i); Set<Integer> newRects = new HashSet<Integer>(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if(rects[i].cross(rects[j])) { if (rects[j].spreadedBy == -1) { rects[i].spread(rects[j]); newRects.add(i); toUse.remove(j); rects[j].spreadedBy = i; } else { rects[rects[j].spreadedBy].spread(rects[i]); toUse.remove(i); rects[i].spreadedBy = rects[j].spreadedBy; break; } } } } while (!newRects.isEmpty()) { Set<Integer> usedRects = new HashSet<Integer>(); Set<Integer> toRemove = new HashSet<Integer>(); for (Integer i : newRects) if (rects[i].spreadedBy == -1) { for (int j : toUse) if (!i.equals(j)) { if (rects[i].cross(rects[j])) { if (rects[j].spreadedBy == -1) { rects[i].spread(rects[j]); usedRects.add(i); toRemove.add(j); rects[j].spreadedBy = i; } else { rects[rects[j].spreadedBy].spread(rects[i]); toRemove.add(i); rects[i].spreadedBy = rects[j].spreadedBy; break; } } } } for (Integer i : toRemove) toUse.remove(i); newRects = usedRects; } //TODO sort lexigography and print toUse Rect[] ans = new Rect[toUse.size()]; int i = 0; for (Integer idx : toUse) { ans[i++] = rects[idx]; } Arrays.sort(ans); out.println(ans.length); for (Rect r : ans) { out.println(r.x1 + " " + r.x2 + " " + r.y1 + " " + r.y2); } out.flush(); out.close(); } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } } |