// Sebastian Jaszczur
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Plemie
{
public:
int x1;
int x2; // x1<x2
int y1;
int y2; // y1<y2
bool deleted;
bool intersect(Plemie &othr)
{
if(not (othr.x2 <= x1 or x2 <= othr.x1) and not (othr.y2 <= y1 or y2 <= othr.y1))
return true;
else
return false;
}
void update(Plemie &othr)
{
x1 = min(x1, othr.x1);
x2 = max(x2, othr.x2);
y1 = min(y1, othr.y1);
y2 = max(y2, othr.y2);
}
Plemie(int x1, int x2, int y1, int y2)
{
this->x1 = x1;
this->x2 = x2;
this->y1 = y1;
this->y2 = y2;
this->deleted = false;
}
};
bool cmp(const Plemie &one, const Plemie &othr)
{
if(one.x1 != othr.x1)
return one.x1<othr.x1;
if(one.x2 != othr.x2)
return one.x2<othr.x2;
if(one.y1 != othr.y1)
return one.y1<othr.y1;
if(one.y2 != othr.y2)
return one.y2<othr.y2;
return false;
}
vector<Plemie> mapa;
int main()
{
ios_base::sync_with_stdio(0);
int n;
cin>>n;
for(int in=0; in<n; in++)
{
int x1, x2, y1, y2;
cin>>x1>>x2>>y1>>y2;
mapa.push_back(Plemie(x1, x2, y1, y2));
}
queue<int> zmiana;
for(int i=0; i<mapa.size(); i++)
{
if(mapa[i].deleted)
continue;
for(int j=i+1; j<mapa.size(); j++)
{
if(mapa[j].deleted)
continue;
if(mapa[i].intersect(mapa[j]))
{
mapa[i].update(mapa[j]);
mapa[j].deleted = true;
zmiana.push(i);
}
}
}
while(not zmiana.empty())
{
int num = zmiana.front();
zmiana.pop();
if(mapa[num].deleted)
continue;
for(int i=0; i<mapa.size(); i++)
{
if(num==i or mapa[i].deleted)
continue;
if(mapa[num].intersect(mapa[i]))
{
mapa[num].update(mapa[i]);
mapa[i].deleted = true;
zmiana.push(num);
}
}
}
sort(mapa.begin(), mapa.end(), cmp);
int ilosc = 0;
for(auto &panstwo: mapa)
{
if(not panstwo.deleted)
ilosc += 1;
}
cout<<ilosc<<"\n";
for(auto &panstwo: mapa)
{
if(not panstwo.deleted)
cout<<panstwo.x1<<" "<<panstwo.x2<<" "<<panstwo.y1<<" "<<panstwo.y2<<"\n";
}
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 | // Sebastian Jaszczur #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; class Plemie { public: int x1; int x2; // x1<x2 int y1; int y2; // y1<y2 bool deleted; bool intersect(Plemie &othr) { if(not (othr.x2 <= x1 or x2 <= othr.x1) and not (othr.y2 <= y1 or y2 <= othr.y1)) return true; else return false; } void update(Plemie &othr) { x1 = min(x1, othr.x1); x2 = max(x2, othr.x2); y1 = min(y1, othr.y1); y2 = max(y2, othr.y2); } Plemie(int x1, int x2, int y1, int y2) { this->x1 = x1; this->x2 = x2; this->y1 = y1; this->y2 = y2; this->deleted = false; } }; bool cmp(const Plemie &one, const Plemie &othr) { if(one.x1 != othr.x1) return one.x1<othr.x1; if(one.x2 != othr.x2) return one.x2<othr.x2; if(one.y1 != othr.y1) return one.y1<othr.y1; if(one.y2 != othr.y2) return one.y2<othr.y2; return false; } vector<Plemie> mapa; int main() { ios_base::sync_with_stdio(0); int n; cin>>n; for(int in=0; in<n; in++) { int x1, x2, y1, y2; cin>>x1>>x2>>y1>>y2; mapa.push_back(Plemie(x1, x2, y1, y2)); } queue<int> zmiana; for(int i=0; i<mapa.size(); i++) { if(mapa[i].deleted) continue; for(int j=i+1; j<mapa.size(); j++) { if(mapa[j].deleted) continue; if(mapa[i].intersect(mapa[j])) { mapa[i].update(mapa[j]); mapa[j].deleted = true; zmiana.push(i); } } } while(not zmiana.empty()) { int num = zmiana.front(); zmiana.pop(); if(mapa[num].deleted) continue; for(int i=0; i<mapa.size(); i++) { if(num==i or mapa[i].deleted) continue; if(mapa[num].intersect(mapa[i])) { mapa[num].update(mapa[i]); mapa[i].deleted = true; zmiana.push(num); } } } sort(mapa.begin(), mapa.end(), cmp); int ilosc = 0; for(auto &panstwo: mapa) { if(not panstwo.deleted) ilosc += 1; } cout<<ilosc<<"\n"; for(auto &panstwo: mapa) { if(not panstwo.deleted) cout<<panstwo.x1<<" "<<panstwo.x2<<" "<<panstwo.y1<<" "<<panstwo.y2<<"\n"; } return 0; } |
English