#include <iostream>
#include <algorithm>
using namespace std;
#define MAXT 100000
struct tribe
{
int x1,x2,y1,y2;
bool merged;
};
tribe t[MAXT];
bool cmp(tribe a, tribe b)
{
return (a.x1 < b.x1);
}
bool leksy(tribe a, tribe b)
{
if(!(a.merged || b.merged))
{
if(a.x1 < b.x1)
return true;
else if(a.x1 == b.x1 && a.x2 < b.x2)
return true;
else if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 < b.y1)
return true;
else if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1 && a.y2 < b.y2)
return true;
else return false;
}
else if(a.merged && !(b.merged))
return true;
else return false;
}
void ple()
{
int n;
cin >> n;
for ( int i = 0 ; i < n ; ++i )
{
cin >> t[i].x1 >> t[i].x2 >> t[i].y1 >> t[i].y2;
}
sort (t, t + n, cmp);
bool merg = 1;
while (merg)
{
merg = 0;
for (int i = 0 ; i < n-1 ; ++i )
{
if (t[i].merged==false)
{
for (int j = i+1 ; j < n ; ++j )
{
if (t[j].merged == false)
{
if ( t[i].x2 > t[j].x1 )
{
if ( !(( t[j].y1 < t[i].y1 && t[j].y2 < t[i].y1) || ( t[j].y1 > t[i].y2 && t[j].y2 > t[i].y2)) )
{
t[i].x2 = max(t[i].x2, t[j].x2);
t[i].y1 = min(t[i].y1, t[j].y1);
t[i].y2 = max(t[i].y2, t[j].y2);
t[j].merged = true;
merg = 1;
}
}
}
}
}
}
}
int count = 0;
for ( int i = 0; i < n; ++i )
{
if(t[i].merged == false)
count++;
}
sort(t, t + n, leksy);
cout << count << endl;
for ( int i = 0; i < n ; ++i )
{
if (t[i].merged == false )
cout << t[i].x1 << " " << t[i].x2 << " " << t[i].y1 << " " << t[i].y2 << endl;
}
}
int main()
{
ple();
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 | #include <iostream> #include <algorithm> using namespace std; #define MAXT 100000 struct tribe { int x1,x2,y1,y2; bool merged; }; tribe t[MAXT]; bool cmp(tribe a, tribe b) { return (a.x1 < b.x1); } bool leksy(tribe a, tribe b) { if(!(a.merged || b.merged)) { if(a.x1 < b.x1) return true; else if(a.x1 == b.x1 && a.x2 < b.x2) return true; else if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 < b.y1) return true; else if (a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1 && a.y2 < b.y2) return true; else return false; } else if(a.merged && !(b.merged)) return true; else return false; } void ple() { int n; cin >> n; for ( int i = 0 ; i < n ; ++i ) { cin >> t[i].x1 >> t[i].x2 >> t[i].y1 >> t[i].y2; } sort (t, t + n, cmp); bool merg = 1; while (merg) { merg = 0; for (int i = 0 ; i < n-1 ; ++i ) { if (t[i].merged==false) { for (int j = i+1 ; j < n ; ++j ) { if (t[j].merged == false) { if ( t[i].x2 > t[j].x1 ) { if ( !(( t[j].y1 < t[i].y1 && t[j].y2 < t[i].y1) || ( t[j].y1 > t[i].y2 && t[j].y2 > t[i].y2)) ) { t[i].x2 = max(t[i].x2, t[j].x2); t[i].y1 = min(t[i].y1, t[j].y1); t[i].y2 = max(t[i].y2, t[j].y2); t[j].merged = true; merg = 1; } } } } } } } int count = 0; for ( int i = 0; i < n; ++i ) { if(t[i].merged == false) count++; } sort(t, t + n, leksy); cout << count << endl; for ( int i = 0; i < n ; ++i ) { if (t[i].merged == false ) cout << t[i].x1 << " " << t[i].x2 << " " << t[i].y1 << " " << t[i].y2 << endl; } } int main() { ple(); return 0; } |
English