#include <cstdio> #include <algorithm> #include <vector> using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAX = 100000; pair<pair<int, int>, pair<int, int>> P[MAX]; struct Node { int x1; int x2; int y1; int y2; Node* prev; Node* next; }; Node* createNode(int x1, int x2, int y1, int y2) { Node* node = new Node(); node->x1 = x1; node->x2 = x2; node->y1 = y1; node->y2 = y2; node->prev = node->next = NULL; return node; } int main(int argc, char **args) { int n; scanf("%d", &n); Node* head = createNode(-1, -1, -1, -1); head->next = head->prev = head; REP(i, n) { int x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); Node* node = createNode(x1, x2, y1, y2); bool found = true; while (found) { found = false; Node* cur = head->next; while (cur->x1 != -1) { if (node->x1 < cur->x2 && node->x2 > cur->x1 && node->y1 < cur->y2 && node->y2 > cur->y1) { found = true; node->x1 = min(node->x1, cur->x1); node->x2 = max(node->x2, cur->x2); node->y1 = min(node->y1, cur->y1); node->y2 = max(node->y2, cur->y2); cur->next->prev = cur->prev; cur->prev->next = cur->next; } cur = cur->next; } if (!found) { node->prev = cur->prev; node->next = cur; node->prev->next = node->next->prev = node; } } } int cnt = 0; Node* cur = head->next; while (cur->x1 != -1) { P[cnt++] = make_pair(make_pair(cur->x1, cur->x2), make_pair(cur->y1, cur->y2)); cur = cur->next; } sort(P, P + cnt); printf("%d\n", cnt); REP(i, cnt) printf("%d %d %d %d\n", P[i].first.first, P[i].first.second, P[i].second.first, P[i].second.second); 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 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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAX = 100000; pair<pair<int, int>, pair<int, int>> P[MAX]; struct Node { int x1; int x2; int y1; int y2; Node* prev; Node* next; }; Node* createNode(int x1, int x2, int y1, int y2) { Node* node = new Node(); node->x1 = x1; node->x2 = x2; node->y1 = y1; node->y2 = y2; node->prev = node->next = NULL; return node; } int main(int argc, char **args) { int n; scanf("%d", &n); Node* head = createNode(-1, -1, -1, -1); head->next = head->prev = head; REP(i, n) { int x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); Node* node = createNode(x1, x2, y1, y2); bool found = true; while (found) { found = false; Node* cur = head->next; while (cur->x1 != -1) { if (node->x1 < cur->x2 && node->x2 > cur->x1 && node->y1 < cur->y2 && node->y2 > cur->y1) { found = true; node->x1 = min(node->x1, cur->x1); node->x2 = max(node->x2, cur->x2); node->y1 = min(node->y1, cur->y1); node->y2 = max(node->y2, cur->y2); cur->next->prev = cur->prev; cur->prev->next = cur->next; } cur = cur->next; } if (!found) { node->prev = cur->prev; node->next = cur; node->prev->next = node->next->prev = node; } } } int cnt = 0; Node* cur = head->next; while (cur->x1 != -1) { P[cnt++] = make_pair(make_pair(cur->x1, cur->x2), make_pair(cur->y1, cur->y2)); cur = cur->next; } sort(P, P + cnt); printf("%d\n", cnt); REP(i, cnt) printf("%d %d %d %d\n", P[i].first.first, P[i].first.second, P[i].second.first, P[i].second.second); return 0; } |