#include <set> #include <vector> #include <cstdio> using namespace std; int n, last; const int MAXN = 100005; struct rectangle { int xl,xr; int yb,yt; }rec[MAXN*2]; struct byBegin { bool operator()(const int &a, const int &b) { if(rec[a].xl != rec[b].xl) return rec[a].xl < rec[b].xl; if(rec[a].xr != rec[b].xr) return rec[a].xr < rec[b].xr; if(rec[a].yb != rec[b].yb) return rec[a].yb < rec[b].yb; if(rec[a].yt != rec[b].yt) return rec[a].yt < rec[b].yt; return a < b; } }; struct byEnd { bool operator()(const int &a, const int &b) { if(rec[a].xr != rec[b].xr) return rec[a].xr != rec[b].xr; return a < b; } }; int get() { scanf("%d", &n); for(last = 0; last < n; ++last){ scanf("%d %d %d %d", &rec[last].xl, &rec[last].xr, &rec[last].yb, &rec[last].yt); } } void conquer() { set<int, byBegin> sortByBegin; set<int, byEnd> proccesed; set<int, byBegin> proccesedBegin; int current; //Dodaj przetwarzane for(int i = 0; i < n; ++i) sortByBegin.insert(i); int toRemove; vector<int> overlapping; int t = 0; for(set<int, byBegin>::iterator it = sortByBegin.begin(); it != sortByBegin.end();){ current = *it; while(rec[(*proccesed.begin())].xr < rec[(*it)].xl && proccesed.begin() != proccesed.end()){ toRemove = *proccesed.begin(); proccesed.erase(proccesed.begin()); } overlapping.clear(); for(set<int>::iterator iter = proccesed.begin(); iter != proccesed.end(); ++iter){ if( (rec[*it].yb >= rec[*iter].yt || rec[*it].yt <= rec[*iter].yb) == false) { overlapping.push_back(*iter); } } if(overlapping.empty() == false){ sortByBegin.erase(sortByBegin.find(current)); for(size_t i = 0; i < overlapping.size(); ++i){ sortByBegin.erase(sortByBegin.find(overlapping[i])); rec[*it].yb = min(rec[*it].yb, rec[overlapping[i]].yb); rec[*it].xl = min(rec[*it].xl, rec[overlapping[i]].xl); rec[*it].yt = max(rec[*it].yt, rec[overlapping[i]].yt); rec[*it].xr = max(rec[*it].xr, rec[overlapping[i]].xr); } sortByBegin.insert(current); proccesed.clear(); it = sortByBegin.begin(); }else{ proccesed.insert(*it); ++it; } } printf("%d\n", sortByBegin.size()); for(set<int, byBegin>::iterator it = sortByBegin.begin(); it != sortByBegin.end(); it++){ printf("%d %d %d %d\n", rec[*it].xl, rec[*it].xr, rec[*it].yb, rec[*it].yt); } } int main() { get(); conquer(); }
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 | #include <set> #include <vector> #include <cstdio> using namespace std; int n, last; const int MAXN = 100005; struct rectangle { int xl,xr; int yb,yt; }rec[MAXN*2]; struct byBegin { bool operator()(const int &a, const int &b) { if(rec[a].xl != rec[b].xl) return rec[a].xl < rec[b].xl; if(rec[a].xr != rec[b].xr) return rec[a].xr < rec[b].xr; if(rec[a].yb != rec[b].yb) return rec[a].yb < rec[b].yb; if(rec[a].yt != rec[b].yt) return rec[a].yt < rec[b].yt; return a < b; } }; struct byEnd { bool operator()(const int &a, const int &b) { if(rec[a].xr != rec[b].xr) return rec[a].xr != rec[b].xr; return a < b; } }; int get() { scanf("%d", &n); for(last = 0; last < n; ++last){ scanf("%d %d %d %d", &rec[last].xl, &rec[last].xr, &rec[last].yb, &rec[last].yt); } } void conquer() { set<int, byBegin> sortByBegin; set<int, byEnd> proccesed; set<int, byBegin> proccesedBegin; int current; //Dodaj przetwarzane for(int i = 0; i < n; ++i) sortByBegin.insert(i); int toRemove; vector<int> overlapping; int t = 0; for(set<int, byBegin>::iterator it = sortByBegin.begin(); it != sortByBegin.end();){ current = *it; while(rec[(*proccesed.begin())].xr < rec[(*it)].xl && proccesed.begin() != proccesed.end()){ toRemove = *proccesed.begin(); proccesed.erase(proccesed.begin()); } overlapping.clear(); for(set<int>::iterator iter = proccesed.begin(); iter != proccesed.end(); ++iter){ if( (rec[*it].yb >= rec[*iter].yt || rec[*it].yt <= rec[*iter].yb) == false) { overlapping.push_back(*iter); } } if(overlapping.empty() == false){ sortByBegin.erase(sortByBegin.find(current)); for(size_t i = 0; i < overlapping.size(); ++i){ sortByBegin.erase(sortByBegin.find(overlapping[i])); rec[*it].yb = min(rec[*it].yb, rec[overlapping[i]].yb); rec[*it].xl = min(rec[*it].xl, rec[overlapping[i]].xl); rec[*it].yt = max(rec[*it].yt, rec[overlapping[i]].yt); rec[*it].xr = max(rec[*it].xr, rec[overlapping[i]].xr); } sortByBegin.insert(current); proccesed.clear(); it = sortByBegin.begin(); }else{ proccesed.insert(*it); ++it; } } printf("%d\n", sortByBegin.size()); for(set<int, byBegin>::iterator it = sortByBegin.begin(); it != sortByBegin.end(); it++){ printf("%d %d %d %d\n", rec[*it].xl, rec[*it].xr, rec[*it].yb, rec[*it].yt); } } int main() { get(); conquer(); } |