#include<cstdio> #include<vector> #include<algorithm> #include<set> #include<iostream> using namespace std; #define rep(i,n) for(int i=0; i<(int)n; i++) #define st first #define nd second #define mp make_pair #define pb push_back #define mtup make_tuple typedef vector<int> vi; typedef pair<int,int> pi; typedef vector<pi> vpii; typedef set<int> SI; typedef long long LL; typedef tuple<int, int, int, int > cztINt; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif int n,m,k,l; const int inf = 1000 * 1000 * 1000 ; const int MAKSN = 1000 * 100 + 13; // UZUPElnic int x,y,x2,y2; vector<tuple<int, int, int, int > > v, ans; void readIn() { l = scanf("%d",&n); rep(i,n) { l = scanf("%d %d %d %d",&x,&x2,&y,&y2); if(x > x2) swap(x, x2); if(y > y2) swap(y, y2); v.pb(mtup(x2,x,y2,y)); } sort(v.begin(), v.end()); } bool empty(int i) { return get<0>(v[i]) == 0 && get<1>(v[i]) == 0 && get<2>(v[i]) == 0 && get<3>(v[i]) == 0; } void setEmpty(int i) { v[i] = mtup(0,0,0,0); } bool intersects(int i, int j) { int x12 = get<0>(v[i]); int x21 = get<1>(v[j]); int x11 = get<1>(v[i]); int x22 = get<0>(v[j]); int y12 = get<2>(v[i]); int y21 = get<3>(v[j]); int y11 = get<3>(v[i]); int y22 = get<2>(v[j]); return !(x12 <= x21 || x22 <= x11 || y12 <= y21 || y22 <= y11); } void setInter(int i, int j) { x = min(get<1>(v[i]), get<1>(v[j])); x2 = max(get<0>(v[i]), get<0>(v[j])); y = min(get<3>(v[i]), get<3>(v[j])); y2 = max(get<2>(v[i]), get<2>(v[j])); v[i] = (mtup(x2,x,y2,y)); } void solve() { bool found = true; // int cnt = 0; while(found == true) { // printf("TA\n"); found = false; rep(i,v.size()) { for(int j=i+1; j<(int)v.size(); j++) { // printf("%d %d %d\n",i,j, intersects(i,j)); if(i != j && !empty(i) && !empty(j) && intersects(i,j)) { setInter(i,j); setEmpty(j); // printf("%d \n",get<0>(v.back())); swap(v[j],v.back()); // printf("%d\n",get<0>(v.back())); v.pop_back(); j--; found = true; } } } } rep(i,v.size()) { if(!empty(i)) { ans.pb(mtup(get<1>(v[i]), get<0>(v[i]), get<3>(v[i]), get<2>(v[i]))); } } sort(ans.begin(), ans.end()); printf("%d\n",ans.size()); rep(i,ans.size()) { printf("%d %d %d %d\n",get<0>(ans[i]), get<1>(ans[i]), get<2>(ans[i]), get<3>(ans[i])); } } int main() { readIn(); solve(); 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 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 | #include<cstdio> #include<vector> #include<algorithm> #include<set> #include<iostream> using namespace std; #define rep(i,n) for(int i=0; i<(int)n; i++) #define st first #define nd second #define mp make_pair #define pb push_back #define mtup make_tuple typedef vector<int> vi; typedef pair<int,int> pi; typedef vector<pi> vpii; typedef set<int> SI; typedef long long LL; typedef tuple<int, int, int, int > cztINt; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif int n,m,k,l; const int inf = 1000 * 1000 * 1000 ; const int MAKSN = 1000 * 100 + 13; // UZUPElnic int x,y,x2,y2; vector<tuple<int, int, int, int > > v, ans; void readIn() { l = scanf("%d",&n); rep(i,n) { l = scanf("%d %d %d %d",&x,&x2,&y,&y2); if(x > x2) swap(x, x2); if(y > y2) swap(y, y2); v.pb(mtup(x2,x,y2,y)); } sort(v.begin(), v.end()); } bool empty(int i) { return get<0>(v[i]) == 0 && get<1>(v[i]) == 0 && get<2>(v[i]) == 0 && get<3>(v[i]) == 0; } void setEmpty(int i) { v[i] = mtup(0,0,0,0); } bool intersects(int i, int j) { int x12 = get<0>(v[i]); int x21 = get<1>(v[j]); int x11 = get<1>(v[i]); int x22 = get<0>(v[j]); int y12 = get<2>(v[i]); int y21 = get<3>(v[j]); int y11 = get<3>(v[i]); int y22 = get<2>(v[j]); return !(x12 <= x21 || x22 <= x11 || y12 <= y21 || y22 <= y11); } void setInter(int i, int j) { x = min(get<1>(v[i]), get<1>(v[j])); x2 = max(get<0>(v[i]), get<0>(v[j])); y = min(get<3>(v[i]), get<3>(v[j])); y2 = max(get<2>(v[i]), get<2>(v[j])); v[i] = (mtup(x2,x,y2,y)); } void solve() { bool found = true; // int cnt = 0; while(found == true) { // printf("TA\n"); found = false; rep(i,v.size()) { for(int j=i+1; j<(int)v.size(); j++) { // printf("%d %d %d\n",i,j, intersects(i,j)); if(i != j && !empty(i) && !empty(j) && intersects(i,j)) { setInter(i,j); setEmpty(j); // printf("%d \n",get<0>(v.back())); swap(v[j],v.back()); // printf("%d\n",get<0>(v.back())); v.pop_back(); j--; found = true; } } } } rep(i,v.size()) { if(!empty(i)) { ans.pb(mtup(get<1>(v[i]), get<0>(v[i]), get<3>(v[i]), get<2>(v[i]))); } } sort(ans.begin(), ans.end()); printf("%d\n",ans.size()); rep(i,ans.size()) { printf("%d %d %d %d\n",get<0>(ans[i]), get<1>(ans[i]), get<2>(ans[i]), get<3>(ans[i])); } } int main() { readIn(); solve(); return 0; } |