#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#include <list>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define MP make_pair
#define FI first
#define SE second
#define PII pair<int, int>
#define PB push_back
#define LL long long
using namespace std;
int n;
vector<pair<PII, PII > > v;
list<pair<PII, PII > > t;
void readInput()
{
scanf("%d", &n);
REP(i, n)
{
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
v.PB(MP(MP(x1, y1), MP(x2, y2)));
}
sort(v.begin(), v.end());
REP(i, n)
t.PB(v[i]);
}
bool inside(pair<PII, PII > r, PII p)
{
return p.FI > r.FI.FI && p.FI < r.SE.FI && p.SE > r.FI.SE && p.SE < r.SE.SE;
}
bool insideE(pair<PII, PII > r, PII p)
{
return p.FI >= r.FI.FI && p.FI <= r.SE.FI && p.SE >= r.FI.SE && p.SE <= r.SE.SE;
}
bool intersect(pair<PII, PII > a, pair<PII, PII > b)
{
if (inside(a, b.FI)
|| inside(a, b.SE)
|| inside(a, MP(b.FI.FI, b.SE.SE))
|| inside(a, MP(b.SE.FI, b.FI.SE))
|| inside(b, a.FI)
|| inside(b, a.SE)
|| inside(b, MP(a.FI.FI, a.SE.SE))
|| inside(b, MP(a.SE.FI, a.FI.SE)))
return true;
if ((insideE(a, b.FI)
&& insideE(a, b.SE)
&& insideE(a, MP(b.FI.FI, b.SE.SE))
&& insideE(a, MP(b.SE.FI, b.FI.SE)))
|| (insideE(b, a.FI)
&& insideE(b, a.SE)
&& insideE(b, MP(a.FI.FI, a.SE.SE))
&& insideE(b, MP(a.SE.FI, a.FI.SE))))
return true;
if ((a.FI.FI >= b.FI.FI && a.SE.FI <= b.SE.FI && b.FI.SE >= a.FI.SE && b.SE.SE <= a.SE.SE)
|| (a.FI.FI >= b.FI.FI && a.SE.FI <= b.SE.FI && b.FI.SE >= a.FI.SE && b.SE.SE <= a.SE.SE))
return true;
return false;
}
int main()
{
readInput();
while (true)
{
bool found = false;
for (list<pair<PII, PII > >::iterator it = t.begin(); it != t.end(); ++it)
{
for (list<pair<PII, PII > >::iterator jt = t.begin(); jt != t.end(); ++jt)
if (it != jt && intersect(*it, *jt))
{
t.push_front(MP(MP(min(it->FI.FI, jt->FI.FI), min(it->FI.SE, jt->FI.SE)), MP(max(it->SE.FI, jt->SE.FI), max(it->SE.SE, jt->SE.SE))));
t.erase(it);
t.erase(jt);
found = true;
break;
}
if (found)
break;
}
if (!found)
break;
}
v.clear();
for (list<pair<PII, PII > >::iterator it = t.begin(); it != t.end(); ++it)
v.PB(*it);
sort(v.begin(), v.end());
int numV = v.size();
printf("%d\n", numV);
REP(i, numV)
printf("%d %d %d %d\n", v[i].FI.FI, v[i].SE.FI, v[i].FI.SE, v[i].SE.SE);
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 | #include <cstdio> #include <vector> #include <set> #include <algorithm> #include <list> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define MP make_pair #define FI first #define SE second #define PII pair<int, int> #define PB push_back #define LL long long using namespace std; int n; vector<pair<PII, PII > > v; list<pair<PII, PII > > t; void readInput() { scanf("%d", &n); REP(i, n) { int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &x2, &y1, &y2); v.PB(MP(MP(x1, y1), MP(x2, y2))); } sort(v.begin(), v.end()); REP(i, n) t.PB(v[i]); } bool inside(pair<PII, PII > r, PII p) { return p.FI > r.FI.FI && p.FI < r.SE.FI && p.SE > r.FI.SE && p.SE < r.SE.SE; } bool insideE(pair<PII, PII > r, PII p) { return p.FI >= r.FI.FI && p.FI <= r.SE.FI && p.SE >= r.FI.SE && p.SE <= r.SE.SE; } bool intersect(pair<PII, PII > a, pair<PII, PII > b) { if (inside(a, b.FI) || inside(a, b.SE) || inside(a, MP(b.FI.FI, b.SE.SE)) || inside(a, MP(b.SE.FI, b.FI.SE)) || inside(b, a.FI) || inside(b, a.SE) || inside(b, MP(a.FI.FI, a.SE.SE)) || inside(b, MP(a.SE.FI, a.FI.SE))) return true; if ((insideE(a, b.FI) && insideE(a, b.SE) && insideE(a, MP(b.FI.FI, b.SE.SE)) && insideE(a, MP(b.SE.FI, b.FI.SE))) || (insideE(b, a.FI) && insideE(b, a.SE) && insideE(b, MP(a.FI.FI, a.SE.SE)) && insideE(b, MP(a.SE.FI, a.FI.SE)))) return true; if ((a.FI.FI >= b.FI.FI && a.SE.FI <= b.SE.FI && b.FI.SE >= a.FI.SE && b.SE.SE <= a.SE.SE) || (a.FI.FI >= b.FI.FI && a.SE.FI <= b.SE.FI && b.FI.SE >= a.FI.SE && b.SE.SE <= a.SE.SE)) return true; return false; } int main() { readInput(); while (true) { bool found = false; for (list<pair<PII, PII > >::iterator it = t.begin(); it != t.end(); ++it) { for (list<pair<PII, PII > >::iterator jt = t.begin(); jt != t.end(); ++jt) if (it != jt && intersect(*it, *jt)) { t.push_front(MP(MP(min(it->FI.FI, jt->FI.FI), min(it->FI.SE, jt->FI.SE)), MP(max(it->SE.FI, jt->SE.FI), max(it->SE.SE, jt->SE.SE)))); t.erase(it); t.erase(jt); found = true; break; } if (found) break; } if (!found) break; } v.clear(); for (list<pair<PII, PII > >::iterator it = t.begin(); it != t.end(); ++it) v.PB(*it); sort(v.begin(), v.end()); int numV = v.size(); printf("%d\n", numV); REP(i, numV) printf("%d %d %d %d\n", v[i].FI.FI, v[i].SE.FI, v[i].FI.SE, v[i].SE.SE); return 0; } |
English