#include <cstdio> #include <algorithm> #include <set> #include <cmath> using namespace std; #define D(x) #define E(x) #define S(x) #define INF 1000000001 //int TREE_SIZE; struct Point{ int id; int x,y; Point(){} }; struct Rectangle{ Point p1, p2; Rectangle(){} Rectangle(const Point& p1, const Point& p2): p1(p1), p2(p2){} }; struct Query{ int minX, minY; int maxX, maxY; Query(){} Query(int minX, int minY, int maxX, int maxY) : minX(minX), minY(minY), maxX(maxX), maxY(maxY){} Query(const Rectangle& rc){ minX = rc.p1.x; minY = rc.p1.y; maxX = rc.p2.x; maxY = rc.p2.y; } }; struct PointByX{ bool operator()(const Point& lhs, const Point& rhs){ if(lhs.x != rhs.x) return lhs.x < rhs.x; if(lhs.id != rhs.id) return lhs.id < rhs.id; // if(lhs.y != rhs.y) return lhs.y < rhs.y; } }; bool comparePointByX(const Point& lhs, const Point& rhs){ if(lhs.x != rhs.x) return lhs.x < rhs.x; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.y != rhs.y) return lhs.y < rhs.y; } struct PointByY{ bool operator()(const Point& lhs, const Point& rhs){ if(lhs.y != rhs.y) return lhs.y < rhs.y; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.x != rhs.x) return lhs.x < rhs.x; } }; bool comparePointByY(const Point& lhs, const Point& rhs){ if(lhs.y != rhs.y) return lhs.y < rhs.y; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.x != rhs.x) return lhs.x < rhs.x; } inline int leftSon(const int index){ return 2*index+1; } inline int rightSon(const int index){ return 2*index+2; } inline int parent(const int index){ return (index-1)/2; } struct SetPointer{ int index; int x; SetPointer(){} SetPointer(int index, int x) : index(index), x(x){} bool operator<(const SetPointer& rhs) const{ if(x != rhs.x) return x < rhs.x; return index < rhs.index; } }; struct SetTree{ set<Point, PointByY> *tree;//[TREE_SIZE*2]; set<SetPointer> pointers; int TREE_SIZE; SetTree(Point * points, int numberOfPoints, int treeSize) : TREE_SIZE(treeSize){ tree = new set<Point, PointByY>[TREE_SIZE*2]; sort(points, points+numberOfPoints, comparePointByX); int pointsPerSet = ceil((float)numberOfPoints / TREE_SIZE); for(int i = TREE_SIZE-1; i<2*TREE_SIZE-1; i++){ int currentIndex = (i-TREE_SIZE+1)*pointsPerSet; if(currentIndex >= numberOfPoints) break; pointers.insert(SetPointer(i, points[currentIndex].x)); E(printf("%d %d\n", currentIndex, points[currentIndex].x)); int start = currentIndex; int end = min(currentIndex+pointsPerSet, numberOfPoints); if(start!=end){ //printf("%d %d\n", start, end); sort(points+start, points+end, comparePointByY); tree[i] = set<Point, PointByY>(points + start, points + end); for(int j=0; j<pointsPerSet && currentIndex+j<numberOfPoints; j++){ //tree[i].insert(points[currentIndex+j]); insert(points[currentIndex+j], i); } } } } ~SetTree(){ delete []tree; } void printTree() const{ for(int i=0; i<TREE_SIZE*2; i++){ for(const Point& p : tree[i]){ printf("|%d %d %d|", p.x, p.y, p.id); //if(p.id == 0) printf("ZERO!\n\n\n\n"); } printf("\n"); } } set<int> search(const Query& query) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, query.minX)); while((itLeft == pointers.end()) || (itLeft->x >= query.minX && itLeft != pointers.begin())) itLeft--; auto itRight = pointers.lower_bound(SetPointer(INF, query.maxX)); if(itRight == pointers.end() || (itRight->x > query.maxX && itRight != pointers.begin())) itRight--; int leftId = itLeft->index; int rightId = itRight->index; D(printf("left: %d, right: %d\n", leftId, rightId)); set<int> points; if(leftId != rightId) searchInBucket(query, tree[leftId], points); searchInBucket(query, tree[rightId], points); while(leftId + 1 < rightId){ if(leftId%2 == 1) searchInBucket(query, tree[leftId+1], points); leftId = parent(leftId); if(rightId%2 == 0) searchInBucket(query, tree[rightId-1], points); rightId = parent(rightId); } return points; } void deletePoint(const Point& point) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, point.x)); while((itLeft == pointers.end()) || (itLeft->x >= point.x && itLeft != pointers.begin())) itLeft--; //E(printf("itleftX: %d\n", itLeft->x)); while(itLeft != pointers.end() && itLeft->x <= point.x){ auto& ySet = tree[itLeft->index]; auto it = ySet.find(point); if(it != ySet.end()){ ySet.erase(it); int index = itLeft->index; while(index > 0){ index = parent(index); tree[index].erase(point); } return; } itLeft++; } E(printf("%d %d\n", point.x, point.y)); E(printTree()); //assert(false); } void insertPoint(const Point& point) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, point.x)); if((itLeft == pointers.end()) || (itLeft->x > point.x && itLeft != pointers.begin())) itLeft--; auto& ySet = tree[itLeft->index]; ySet.insert(point); int index = itLeft->index; while(index > 0){ index = parent(index); tree[index].insert(point); } } private: void searchInBucket(const Query& query, set<Point, PointByY>& src, set<int>& points) const{ Point startPoint; startPoint.y = query.minY; startPoint.id = -1; auto it = src.lower_bound(startPoint); int minX = query.minX; int maxX = query.maxX; int maxY = query.maxY; while(it != src.end() && it->y <= maxY){ int x = it->x; //(printf("@%dx%dy%d@", it->id, it->x, it->y)); if(x >= minX && x <= maxX) points.insert(it->id); it++; } } inline void insert(const Point& point, int index) const{ //tree[index].insert(point); do{ index = parent(index); tree[index].insert(point); }while(index > 0); } }; inline bool intersect(const Rectangle& r1, const Rectangle& r2){ return (r1.p1.x < r2.p2.x && r1.p2.x > r2.p1.x && r1.p1.y < r2.p2.y && r1.p2.y > r2.p1.y); } void maximize(Rectangle& mRec, const Rectangle& addRec){ if(addRec.p1.x < mRec.p1.x) mRec.p1.x = addRec.p1.x; if(addRec.p1.y < mRec.p1.y) mRec.p1.y = addRec.p1.y; if(addRec.p2.x > mRec.p2.x) mRec.p2.x = addRec.p2.x; if(addRec.p2.y > mRec.p2.y) mRec.p2.y = addRec.p2.y; } void deleteAllPointsFromRectangle(const SetTree* tree, const Rectangle& rec){ tree->deletePoint(rec.p1); tree->deletePoint(rec.p2); Point p3 = rec.p1; p3.x = rec.p2.x; tree->deletePoint(p3); Point p4 = rec.p2; p4.x = rec.p1.x; tree->deletePoint(p4); } void putAllPointsFromRectangle(const SetTree* tree, const Rectangle& rec){ tree->insertPoint(rec.p1); tree->insertPoint(rec.p2); Point p3 = rec.p1; p3.x = rec.p2.x; tree->insertPoint(p3); Point p4 = rec.p2; p4.x = rec.p1.x; tree->insertPoint(p4); } bool lexiOrder(const Rectangle& lhs, const Rectangle& rhs){ if(lhs.p1.x != rhs.p1.x) return lhs.p1.x < rhs.p1.x; if(lhs.p2.x != rhs.p2.x) return lhs.p2.x < rhs.p2.x; if(lhs.p1.y != rhs.p1.y) return lhs.p1.y < rhs.p1.y; return lhs.p2.y < rhs.p2.y; } //------------------------------------------------------------------------- struct XSeg{ Point p; int height; bool opening; XSeg(){} XSeg(Point p, int height, bool opening): p(p), height(height), opening(opening){} XSeg(const Rectangle &rec, bool opening): opening(opening){ p = rec.p1; if(!opening) p.x = rec.p2.x; height = rec.p2.y - rec.p1.y; } bool operator<(const XSeg& rhs) const{ if(this->p.x != rhs.p.x) return p.x < rhs.p.x; if(opening != rhs.opening) return opening < rhs.opening; return p.id < rhs.p.id; } }; struct YSeg{ int id; int y; int height; YSeg(){} YSeg(int id, int y, int height): id(id), y(y), height(height){} YSeg(const XSeg& seg){ id = seg.p.id; y = seg.p.y; height = seg.height; } bool operator<(const YSeg& rhs) const{ if(y != rhs.y) return y < rhs.y; return id < rhs.id; } }; void prepare(Rectangle* rects, int n){ set<YSeg> sweep; set<XSeg> sweepSrc; for(int i=0; i<n; i++){ sweepSrc.insert(XSeg(rects[i], true)); sweepSrc.insert(XSeg(rects[i], false)); } //sort(sweepSrc.begin(), sweepSrc.end()); bool changed = false; for(auto srcIt = sweepSrc.begin(); srcIt != sweepSrc.end(); ){ changed = false; S(printf("X%d|", srcIt->p.x)); if(srcIt->opening){ S(printf("O")); XSeg src = XSeg(rects[srcIt->p.id], true); auto it = sweep.lower_bound(YSeg(src)); Rectangle beforeMax = rects[src.p.id]; auto itLow = it; S(if(it== sweep.begin()) printf("BEGIN")); if(it != sweep.begin()){ it--; while(it->y + it->height > src.p.y){ S(printf("PRZECIECIE DOL!")); maximize(rects[src.p.id], rects[it->id]); changed = true; src = XSeg(rects[src.p.id], true); auto lastIt = it; if(it != sweep.begin()) it--; else{ sweep.erase(lastIt); break; } sweep.erase(lastIt); } } it = itLow; while(it != sweep.end() && it->y < src.p.y + src.height){ S(printf("PRZECIECIE!%d|", rects[it->id].p1.x)); maximize(rects[src.p.id], rects[it->id]); changed = true; src = XSeg(rects[src.p.id], true); auto lastIt = it; it++; sweep.erase(lastIt); } //}else{ sweepSrc.erase(XSeg(beforeMax,false)); sweepSrc.insert(XSeg(rects[src.p.id], false)); if(!changed){ sweep.insert(YSeg(XSeg(rects[src.p.id], true))); } //} } else{ sweep.erase(YSeg(*srcIt)); } if(!changed) srcIt++; S(printf("\n")); } } //-------------------------------------------------------------------------- int main(){ int n; scanf("%d", &n); Point* points = new Point[n*4]; Rectangle* rects = new Rectangle[n]; for(int i=0 ;i<n; i++){ scanf("%d%d%d%d", &rects[i].p1.x, &rects[i].p2.x, &rects[i].p1.y, &rects[i].p2.y); rects[i].p1.id = rects[i].p2.id = i; } prepare(rects, n); for(int i=0; i<n; i++){ int i4 = i*4; points[i4] = rects[i].p1; points[i4+1] = rects[i].p1; points[i4+1].x = rects[i].p2.x; points[i4+2] = rects[i].p2; points[i4+3] = rects[i].p2; points[i4+3].x = rects[i].p1.x; } SetTree* tree = new SetTree(points, n*4, 2048); for(int i=0; i<n; i++){ bool change = false; Rectangle mainRec = rects[i]; if(mainRec.p1.id == -1) continue; set<int> inArea = tree->search(Query(rects[i])); for(int recId: inArea){ if(recId == i) continue; D(printf("Y%d,%d ", i, recId)); Rectangle addRec = rects[recId]; D(if(addRec.p1.id == -1){ printf("YMCA"); }); if(intersect(mainRec, addRec)) { change = true; D(printf("X")); maximize(mainRec, addRec); //E(printf("DELETE %d\n", addRec.p1.id)); deleteAllPointsFromRectangle(tree, addRec); rects[recId].p1.id = -1; } } if(change){ //E(printf("DELETEX %d\n", rects[i].p1.id)); deleteAllPointsFromRectangle(tree, rects[i]); putAllPointsFromRectangle(tree, mainRec); rects[i] = mainRec; i--; } } //delete tree; vector<Rectangle> result; for(int i=0; i<n; i++){ Rectangle rec = rects[i]; if(rec.p1.id == -1) continue; result.push_back(rec); D(printf("%d %d %d %d\n", rec.p1.x, rec.p2.x, rec.p1.y, rec.p2.y)); } sort(result.begin(), result.end(), lexiOrder); printf("%d\n", (int)result.size()); for(const Rectangle & rec: result){ printf("%d %d %d %d\n", rec.p1.x, rec.p2.x, rec.p1.y, rec.p2.y); } //delete[] points; //delete[] rects; 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | #include <cstdio> #include <algorithm> #include <set> #include <cmath> using namespace std; #define D(x) #define E(x) #define S(x) #define INF 1000000001 //int TREE_SIZE; struct Point{ int id; int x,y; Point(){} }; struct Rectangle{ Point p1, p2; Rectangle(){} Rectangle(const Point& p1, const Point& p2): p1(p1), p2(p2){} }; struct Query{ int minX, minY; int maxX, maxY; Query(){} Query(int minX, int minY, int maxX, int maxY) : minX(minX), minY(minY), maxX(maxX), maxY(maxY){} Query(const Rectangle& rc){ minX = rc.p1.x; minY = rc.p1.y; maxX = rc.p2.x; maxY = rc.p2.y; } }; struct PointByX{ bool operator()(const Point& lhs, const Point& rhs){ if(lhs.x != rhs.x) return lhs.x < rhs.x; if(lhs.id != rhs.id) return lhs.id < rhs.id; // if(lhs.y != rhs.y) return lhs.y < rhs.y; } }; bool comparePointByX(const Point& lhs, const Point& rhs){ if(lhs.x != rhs.x) return lhs.x < rhs.x; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.y != rhs.y) return lhs.y < rhs.y; } struct PointByY{ bool operator()(const Point& lhs, const Point& rhs){ if(lhs.y != rhs.y) return lhs.y < rhs.y; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.x != rhs.x) return lhs.x < rhs.x; } }; bool comparePointByY(const Point& lhs, const Point& rhs){ if(lhs.y != rhs.y) return lhs.y < rhs.y; if(lhs.id != rhs.id) return lhs.id < rhs.id; //if(lhs.x != rhs.x) return lhs.x < rhs.x; } inline int leftSon(const int index){ return 2*index+1; } inline int rightSon(const int index){ return 2*index+2; } inline int parent(const int index){ return (index-1)/2; } struct SetPointer{ int index; int x; SetPointer(){} SetPointer(int index, int x) : index(index), x(x){} bool operator<(const SetPointer& rhs) const{ if(x != rhs.x) return x < rhs.x; return index < rhs.index; } }; struct SetTree{ set<Point, PointByY> *tree;//[TREE_SIZE*2]; set<SetPointer> pointers; int TREE_SIZE; SetTree(Point * points, int numberOfPoints, int treeSize) : TREE_SIZE(treeSize){ tree = new set<Point, PointByY>[TREE_SIZE*2]; sort(points, points+numberOfPoints, comparePointByX); int pointsPerSet = ceil((float)numberOfPoints / TREE_SIZE); for(int i = TREE_SIZE-1; i<2*TREE_SIZE-1; i++){ int currentIndex = (i-TREE_SIZE+1)*pointsPerSet; if(currentIndex >= numberOfPoints) break; pointers.insert(SetPointer(i, points[currentIndex].x)); E(printf("%d %d\n", currentIndex, points[currentIndex].x)); int start = currentIndex; int end = min(currentIndex+pointsPerSet, numberOfPoints); if(start!=end){ //printf("%d %d\n", start, end); sort(points+start, points+end, comparePointByY); tree[i] = set<Point, PointByY>(points + start, points + end); for(int j=0; j<pointsPerSet && currentIndex+j<numberOfPoints; j++){ //tree[i].insert(points[currentIndex+j]); insert(points[currentIndex+j], i); } } } } ~SetTree(){ delete []tree; } void printTree() const{ for(int i=0; i<TREE_SIZE*2; i++){ for(const Point& p : tree[i]){ printf("|%d %d %d|", p.x, p.y, p.id); //if(p.id == 0) printf("ZERO!\n\n\n\n"); } printf("\n"); } } set<int> search(const Query& query) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, query.minX)); while((itLeft == pointers.end()) || (itLeft->x >= query.minX && itLeft != pointers.begin())) itLeft--; auto itRight = pointers.lower_bound(SetPointer(INF, query.maxX)); if(itRight == pointers.end() || (itRight->x > query.maxX && itRight != pointers.begin())) itRight--; int leftId = itLeft->index; int rightId = itRight->index; D(printf("left: %d, right: %d\n", leftId, rightId)); set<int> points; if(leftId != rightId) searchInBucket(query, tree[leftId], points); searchInBucket(query, tree[rightId], points); while(leftId + 1 < rightId){ if(leftId%2 == 1) searchInBucket(query, tree[leftId+1], points); leftId = parent(leftId); if(rightId%2 == 0) searchInBucket(query, tree[rightId-1], points); rightId = parent(rightId); } return points; } void deletePoint(const Point& point) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, point.x)); while((itLeft == pointers.end()) || (itLeft->x >= point.x && itLeft != pointers.begin())) itLeft--; //E(printf("itleftX: %d\n", itLeft->x)); while(itLeft != pointers.end() && itLeft->x <= point.x){ auto& ySet = tree[itLeft->index]; auto it = ySet.find(point); if(it != ySet.end()){ ySet.erase(it); int index = itLeft->index; while(index > 0){ index = parent(index); tree[index].erase(point); } return; } itLeft++; } E(printf("%d %d\n", point.x, point.y)); E(printTree()); //assert(false); } void insertPoint(const Point& point) const{ auto itLeft = pointers.lower_bound(SetPointer(-1, point.x)); if((itLeft == pointers.end()) || (itLeft->x > point.x && itLeft != pointers.begin())) itLeft--; auto& ySet = tree[itLeft->index]; ySet.insert(point); int index = itLeft->index; while(index > 0){ index = parent(index); tree[index].insert(point); } } private: void searchInBucket(const Query& query, set<Point, PointByY>& src, set<int>& points) const{ Point startPoint; startPoint.y = query.minY; startPoint.id = -1; auto it = src.lower_bound(startPoint); int minX = query.minX; int maxX = query.maxX; int maxY = query.maxY; while(it != src.end() && it->y <= maxY){ int x = it->x; //(printf("@%dx%dy%d@", it->id, it->x, it->y)); if(x >= minX && x <= maxX) points.insert(it->id); it++; } } inline void insert(const Point& point, int index) const{ //tree[index].insert(point); do{ index = parent(index); tree[index].insert(point); }while(index > 0); } }; inline bool intersect(const Rectangle& r1, const Rectangle& r2){ return (r1.p1.x < r2.p2.x && r1.p2.x > r2.p1.x && r1.p1.y < r2.p2.y && r1.p2.y > r2.p1.y); } void maximize(Rectangle& mRec, const Rectangle& addRec){ if(addRec.p1.x < mRec.p1.x) mRec.p1.x = addRec.p1.x; if(addRec.p1.y < mRec.p1.y) mRec.p1.y = addRec.p1.y; if(addRec.p2.x > mRec.p2.x) mRec.p2.x = addRec.p2.x; if(addRec.p2.y > mRec.p2.y) mRec.p2.y = addRec.p2.y; } void deleteAllPointsFromRectangle(const SetTree* tree, const Rectangle& rec){ tree->deletePoint(rec.p1); tree->deletePoint(rec.p2); Point p3 = rec.p1; p3.x = rec.p2.x; tree->deletePoint(p3); Point p4 = rec.p2; p4.x = rec.p1.x; tree->deletePoint(p4); } void putAllPointsFromRectangle(const SetTree* tree, const Rectangle& rec){ tree->insertPoint(rec.p1); tree->insertPoint(rec.p2); Point p3 = rec.p1; p3.x = rec.p2.x; tree->insertPoint(p3); Point p4 = rec.p2; p4.x = rec.p1.x; tree->insertPoint(p4); } bool lexiOrder(const Rectangle& lhs, const Rectangle& rhs){ if(lhs.p1.x != rhs.p1.x) return lhs.p1.x < rhs.p1.x; if(lhs.p2.x != rhs.p2.x) return lhs.p2.x < rhs.p2.x; if(lhs.p1.y != rhs.p1.y) return lhs.p1.y < rhs.p1.y; return lhs.p2.y < rhs.p2.y; } //------------------------------------------------------------------------- struct XSeg{ Point p; int height; bool opening; XSeg(){} XSeg(Point p, int height, bool opening): p(p), height(height), opening(opening){} XSeg(const Rectangle &rec, bool opening): opening(opening){ p = rec.p1; if(!opening) p.x = rec.p2.x; height = rec.p2.y - rec.p1.y; } bool operator<(const XSeg& rhs) const{ if(this->p.x != rhs.p.x) return p.x < rhs.p.x; if(opening != rhs.opening) return opening < rhs.opening; return p.id < rhs.p.id; } }; struct YSeg{ int id; int y; int height; YSeg(){} YSeg(int id, int y, int height): id(id), y(y), height(height){} YSeg(const XSeg& seg){ id = seg.p.id; y = seg.p.y; height = seg.height; } bool operator<(const YSeg& rhs) const{ if(y != rhs.y) return y < rhs.y; return id < rhs.id; } }; void prepare(Rectangle* rects, int n){ set<YSeg> sweep; set<XSeg> sweepSrc; for(int i=0; i<n; i++){ sweepSrc.insert(XSeg(rects[i], true)); sweepSrc.insert(XSeg(rects[i], false)); } //sort(sweepSrc.begin(), sweepSrc.end()); bool changed = false; for(auto srcIt = sweepSrc.begin(); srcIt != sweepSrc.end(); ){ changed = false; S(printf("X%d|", srcIt->p.x)); if(srcIt->opening){ S(printf("O")); XSeg src = XSeg(rects[srcIt->p.id], true); auto it = sweep.lower_bound(YSeg(src)); Rectangle beforeMax = rects[src.p.id]; auto itLow = it; S(if(it== sweep.begin()) printf("BEGIN")); if(it != sweep.begin()){ it--; while(it->y + it->height > src.p.y){ S(printf("PRZECIECIE DOL!")); maximize(rects[src.p.id], rects[it->id]); changed = true; src = XSeg(rects[src.p.id], true); auto lastIt = it; if(it != sweep.begin()) it--; else{ sweep.erase(lastIt); break; } sweep.erase(lastIt); } } it = itLow; while(it != sweep.end() && it->y < src.p.y + src.height){ S(printf("PRZECIECIE!%d|", rects[it->id].p1.x)); maximize(rects[src.p.id], rects[it->id]); changed = true; src = XSeg(rects[src.p.id], true); auto lastIt = it; it++; sweep.erase(lastIt); } //}else{ sweepSrc.erase(XSeg(beforeMax,false)); sweepSrc.insert(XSeg(rects[src.p.id], false)); if(!changed){ sweep.insert(YSeg(XSeg(rects[src.p.id], true))); } //} } else{ sweep.erase(YSeg(*srcIt)); } if(!changed) srcIt++; S(printf("\n")); } } //-------------------------------------------------------------------------- int main(){ int n; scanf("%d", &n); Point* points = new Point[n*4]; Rectangle* rects = new Rectangle[n]; for(int i=0 ;i<n; i++){ scanf("%d%d%d%d", &rects[i].p1.x, &rects[i].p2.x, &rects[i].p1.y, &rects[i].p2.y); rects[i].p1.id = rects[i].p2.id = i; } prepare(rects, n); for(int i=0; i<n; i++){ int i4 = i*4; points[i4] = rects[i].p1; points[i4+1] = rects[i].p1; points[i4+1].x = rects[i].p2.x; points[i4+2] = rects[i].p2; points[i4+3] = rects[i].p2; points[i4+3].x = rects[i].p1.x; } SetTree* tree = new SetTree(points, n*4, 2048); for(int i=0; i<n; i++){ bool change = false; Rectangle mainRec = rects[i]; if(mainRec.p1.id == -1) continue; set<int> inArea = tree->search(Query(rects[i])); for(int recId: inArea){ if(recId == i) continue; D(printf("Y%d,%d ", i, recId)); Rectangle addRec = rects[recId]; D(if(addRec.p1.id == -1){ printf("YMCA"); }); if(intersect(mainRec, addRec)) { change = true; D(printf("X")); maximize(mainRec, addRec); //E(printf("DELETE %d\n", addRec.p1.id)); deleteAllPointsFromRectangle(tree, addRec); rects[recId].p1.id = -1; } } if(change){ //E(printf("DELETEX %d\n", rects[i].p1.id)); deleteAllPointsFromRectangle(tree, rects[i]); putAllPointsFromRectangle(tree, mainRec); rects[i] = mainRec; i--; } } //delete tree; vector<Rectangle> result; for(int i=0; i<n; i++){ Rectangle rec = rects[i]; if(rec.p1.id == -1) continue; result.push_back(rec); D(printf("%d %d %d %d\n", rec.p1.x, rec.p2.x, rec.p1.y, rec.p2.y)); } sort(result.begin(), result.end(), lexiOrder); printf("%d\n", (int)result.size()); for(const Rectangle & rec: result){ printf("%d %d %d %d\n", rec.p1.x, rec.p2.x, rec.p1.y, rec.p2.y); } //delete[] points; //delete[] rects; return 0; } |