#include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Rect { int x1; int x2; int y1; int y2; }; vector<Rect> rectangles; inline bool cmpLex(const Rect &a, const Rect &b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; return a.y2 < b.y2; } inline bool intersects(const Rect &a, const Rect &b) { if ((a.x2 <= b.x1) || (b.x2 <= a.x1)) return false; if ((a.y2 <= b.y1) || (b.y2 <= a.y1)) return false; return true; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { Rect rect; scanf("%d%d%d%d", &rect.x1, &rect.x2, &rect.y1, &rect.y2); rectangles.push_back(rect); } int m = n; bool intersections = true; while (intersections) { intersections = false; int i = 0; while (i < m) { int j = i + 1; while (j < m) { if (intersects(rectangles[i], rectangles[j])) { //printf("intersect %d %d\n", i, j); rectangles[j].x1 = min(rectangles[i].x1, rectangles[j].x1); rectangles[j].x2 = max(rectangles[i].x2, rectangles[j].x2); rectangles[j].y1 = min(rectangles[i].y1, rectangles[j].y1); rectangles[j].y2 = max(rectangles[i].y2, rectangles[j].y2); //printf(" new rectangle %d %d %d %d\n", rectangles[j].x1, rectangles[j].x2, rectangles[j].y1, rectangles[j].y2); rectangles[i] = rectangles[m - 1]; m--; intersections = true; } j++; } i++; } } printf("%d\n", m); sort(rectangles.begin(), rectangles.begin() + m, cmpLex); for (int i = 0; i < m; i++) { printf("%d %d %d %d\n", rectangles[i].x1, rectangles[i].x2, rectangles[i].y1, rectangles[i].y2); } return 0; }
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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Rect { int x1; int x2; int y1; int y2; }; vector<Rect> rectangles; inline bool cmpLex(const Rect &a, const Rect &b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; return a.y2 < b.y2; } inline bool intersects(const Rect &a, const Rect &b) { if ((a.x2 <= b.x1) || (b.x2 <= a.x1)) return false; if ((a.y2 <= b.y1) || (b.y2 <= a.y1)) return false; return true; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { Rect rect; scanf("%d%d%d%d", &rect.x1, &rect.x2, &rect.y1, &rect.y2); rectangles.push_back(rect); } int m = n; bool intersections = true; while (intersections) { intersections = false; int i = 0; while (i < m) { int j = i + 1; while (j < m) { if (intersects(rectangles[i], rectangles[j])) { //printf("intersect %d %d\n", i, j); rectangles[j].x1 = min(rectangles[i].x1, rectangles[j].x1); rectangles[j].x2 = max(rectangles[i].x2, rectangles[j].x2); rectangles[j].y1 = min(rectangles[i].y1, rectangles[j].y1); rectangles[j].y2 = max(rectangles[i].y2, rectangles[j].y2); //printf(" new rectangle %d %d %d %d\n", rectangles[j].x1, rectangles[j].x2, rectangles[j].y1, rectangles[j].y2); rectangles[i] = rectangles[m - 1]; m--; intersections = true; } j++; } i++; } } printf("%d\n", m); sort(rectangles.begin(), rectangles.begin() + m, cmpLex); for (int i = 0; i < m; i++) { printf("%d %d %d %d\n", rectangles[i].x1, rectangles[i].x2, rectangles[i].y1, rectangles[i].y2); } return 0; } |