#include <stdio.h> #include <set> #include <tuple> using namespace std; namespace { typedef int I; typedef tuple<I, I, I, I> P;//x1, x2, y1, y2 typedef set<P> Set; typedef Set::iterator Iter; I N; Set points; bool inflate(const P& p, I& x1, I& x2, I& y1, I& y2) { if (get<1>(p) <= x1 || x2 <= get<0>(p) || get<3>(p) <= y1 || y2 <= get<2>(p)) return false; x1 = min(get<0>(p), x1); x2 = max(get<1>(p), x2); y1 = min(get<2>(p), y1); y2 = max(get<3>(p), y2); return true; } void insert(I x1, I x2, I y1, I y2) { bool wasInflated; do { wasInflated = false; for (Iter it = points.begin(); it != points.end();) { if (inflate(*it, x1, x2, y1, y2)) { it = points.erase(it); wasInflated = true; } else { ++it; } } } while (wasInflated); points.insert(P(x1, x2, y1, y2)); } } //namespace int main() { scanf("%d", &N); for (I n=0; n<N; ++n) { I x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); insert(x1, x2, y1, y2); } printf("%d\n", (int)points.size()); for(const P& p : points) { printf("%d %d %d %d\n", get<0>(p), get<1>(p), get<2>(p), get<3>(p)); } 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 | #include <stdio.h> #include <set> #include <tuple> using namespace std; namespace { typedef int I; typedef tuple<I, I, I, I> P;//x1, x2, y1, y2 typedef set<P> Set; typedef Set::iterator Iter; I N; Set points; bool inflate(const P& p, I& x1, I& x2, I& y1, I& y2) { if (get<1>(p) <= x1 || x2 <= get<0>(p) || get<3>(p) <= y1 || y2 <= get<2>(p)) return false; x1 = min(get<0>(p), x1); x2 = max(get<1>(p), x2); y1 = min(get<2>(p), y1); y2 = max(get<3>(p), y2); return true; } void insert(I x1, I x2, I y1, I y2) { bool wasInflated; do { wasInflated = false; for (Iter it = points.begin(); it != points.end();) { if (inflate(*it, x1, x2, y1, y2)) { it = points.erase(it); wasInflated = true; } else { ++it; } } } while (wasInflated); points.insert(P(x1, x2, y1, y2)); } } //namespace int main() { scanf("%d", &N); for (I n=0; n<N; ++n) { I x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); insert(x1, x2, y1, y2); } printf("%d\n", (int)points.size()); for(const P& p : points) { printf("%d %d %d %d\n", get<0>(p), get<1>(p), get<2>(p), get<3>(p)); } return 0; } |