Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <list>
#include <string>
using namespace std;
typedef long long LL;
typedef pair<int,int> PI;
#define MP make_pair
#define PB push_back
#define SD second
#define FT first
int k,n,m,q;
const int N = 100003;
const int INF = 20000000;
struct Prost : pair<PI,PI>
{
PI punkt1;
PI punkt2;
PI punkt3;
PI punkt4;
Prost() {}
Prost(int x1, int x2, int y1, int y2) {
FT.FT=min(x1,x2);
FT.SD=max(x1,x2);
SD.FT=min(y1,y2);
SD.SD=max(y1,y2);
punkt1 = MP(FT.FT,SD.FT);
punkt2 = MP(FT.SD,SD.SD);
punkt3 = MP(FT.FT,SD.SD);
punkt4 = MP(FT.SD,SD.FT);
}
int x1() {return FT.FT;}
int x2() {return FT.SD;}
int y1() {return SD.FT;}
int y2() {return SD.SD;}
bool przecina(Prost &b) {
return ( (x1() < b.x2() && x2() > b.x1() &&
y1() < b.y2() && y2() > b.y1() ));
//return b.zawiera(punkt1) || b.zawiera(punkt2) || b.zawiera(punkt3) || b.zawiera(punkt4) || zawiera(b.punkt1) || zawiera(b.punkt2) || zawiera(b.punkt3) || zawiera(b.punkt4);
}
bool zawiera(PI punkt) {
return FT.FT < punkt.FT && punkt.FT < FT.SD && SD.FT < punkt.SD && punkt.SD < SD.SD;
}
Prost polacz(Prost &b) {
return Prost(min(x1(),b.x1()),max(x2(),b.x2()),min(y1(),b.y1()),max(y2(),b.y2()));
}
string toString() {
char res[100];
sprintf(res,"%d %d %d %d",FT.FT,FT.SD,SD.FT,SD.SD);
return res;
}
};
list<Prost> plem;
typedef list<Prost>::iterator it;
void polacz(list<Prost> &plem, Prost nowe) {
int cykl = 1;
// O(n) amortyzuje si�?
while(cykl) {
cykl = 0;
for(it p=plem.begin();p!=plem.end();p++) {
if(nowe.przecina(*p)) {
nowe = nowe.polacz(*p);
it usun = p;
p++;
plem.erase(usun);
p--;
cykl = 1;
}
}
}
plem.PB(nowe);
}
int main() {
scanf("%d",&n);
// O(n^2);
for(int i=0;i<n;i++) {
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
Prost nowe = Prost(x1,x2,y1,y2);
polacz(plem,nowe);
}
vector<Prost>panstwa = vector<Prost>(plem.begin(),plem.end());
sort(panstwa.begin(),panstwa.end());
printf("%d\n", panstwa.size());
for(int i=0;i<panstwa.size();i++) {
printf("%s\n", panstwa[i].toString().c_str());
}
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 | #include <cstdio> #include <map> #include <algorithm> #include <vector> #include <queue> #include <list> #include <string> using namespace std; typedef long long LL; typedef pair<int,int> PI; #define MP make_pair #define PB push_back #define SD second #define FT first int k,n,m,q; const int N = 100003; const int INF = 20000000; struct Prost : pair<PI,PI> { PI punkt1; PI punkt2; PI punkt3; PI punkt4; Prost() {} Prost(int x1, int x2, int y1, int y2) { FT.FT=min(x1,x2); FT.SD=max(x1,x2); SD.FT=min(y1,y2); SD.SD=max(y1,y2); punkt1 = MP(FT.FT,SD.FT); punkt2 = MP(FT.SD,SD.SD); punkt3 = MP(FT.FT,SD.SD); punkt4 = MP(FT.SD,SD.FT); } int x1() {return FT.FT;} int x2() {return FT.SD;} int y1() {return SD.FT;} int y2() {return SD.SD;} bool przecina(Prost &b) { return ( (x1() < b.x2() && x2() > b.x1() && y1() < b.y2() && y2() > b.y1() )); //return b.zawiera(punkt1) || b.zawiera(punkt2) || b.zawiera(punkt3) || b.zawiera(punkt4) || zawiera(b.punkt1) || zawiera(b.punkt2) || zawiera(b.punkt3) || zawiera(b.punkt4); } bool zawiera(PI punkt) { return FT.FT < punkt.FT && punkt.FT < FT.SD && SD.FT < punkt.SD && punkt.SD < SD.SD; } Prost polacz(Prost &b) { return Prost(min(x1(),b.x1()),max(x2(),b.x2()),min(y1(),b.y1()),max(y2(),b.y2())); } string toString() { char res[100]; sprintf(res,"%d %d %d %d",FT.FT,FT.SD,SD.FT,SD.SD); return res; } }; list<Prost> plem; typedef list<Prost>::iterator it; void polacz(list<Prost> &plem, Prost nowe) { int cykl = 1; // O(n) amortyzuje si�? while(cykl) { cykl = 0; for(it p=plem.begin();p!=plem.end();p++) { if(nowe.przecina(*p)) { nowe = nowe.polacz(*p); it usun = p; p++; plem.erase(usun); p--; cykl = 1; } } } plem.PB(nowe); } int main() { scanf("%d",&n); // O(n^2); for(int i=0;i<n;i++) { int x1,x2,y1,y2; scanf("%d%d%d%d",&x1,&x2,&y1,&y2); Prost nowe = Prost(x1,x2,y1,y2); polacz(plem,nowe); } vector<Prost>panstwa = vector<Prost>(plem.begin(),plem.end()); sort(panstwa.begin(),panstwa.end()); printf("%d\n", panstwa.size()); for(int i=0;i<panstwa.size();i++) { printf("%s\n", panstwa[i].toString().c_str()); } return 0; } |
English