#include <cstdio> #include <cassert> #include <algorithm> #include <vector> #include <queue> using namespace std; unsigned int x, y, n; class true_rect { public: unsigned int x1, y1, x2, y2; unsigned long long area; true_rect(unsigned int a1, unsigned int b1, unsigned int a2, unsigned int b2); true_rect intersect(true_rect &tr); void print(); }; true_rect::true_rect(unsigned int a1, unsigned int b1, unsigned int a2, unsigned int b2) { x1 = min(a1, a2); x2 = max(a1, a2); y1 = min(b1, b2); y2 = max(b1, b2); area = (static_cast<long long>(x2-x1)) * (static_cast<long long>(y2-y1)); // printf("(%u, %u) - (%u, %u) - area=%llu\n", x1, y1, x2, y2, area); } true_rect true_rect::intersect(true_rect &tr) { if (this->x1 >= tr.x2 || tr.x1 >= this->x2) return true_rect(0, 0, 0, 0); if (this->y1 >= tr.y2 || tr.y1 >= this->y2) return true_rect(0, 0, 0, 0); return true_rect( max(this->x1, tr.x1), max(this->y1, tr.y1), min(this->x2, tr.x2), min(this->y2, tr.y2) ); } void true_rect::print() { printf(" (%u, %u) - (%u, %u) - (%llu)\n", x1, y1, x2, y2, area); } class region { public: vector<true_rect> v; long long area; int nr_processed; region() {}; region(true_rect tr); region* intersect(true_rect r); region* intersect(region *r); void add_true_rect(true_rect tr); void print_area(); void print(); }; class region_cmp { public: bool operator()(const region *l, region *r) { if (l->area == r->area) { return l->nr_processed < r->nr_processed; } return l->area < r->area; } }; region::region(true_rect tr) { v.push_back(tr); area = tr.area; } region* region::intersect(true_rect r) { region *reg = new region(); reg->area = 0; for(vector<true_rect>::iterator it = this->v.begin(); it != this->v.end(); ++it) { true_rect tr = it->intersect(r); if (tr.area != 0) { reg->v.push_back(tr); reg->area += tr.area; } } if (reg->area == 0) { delete reg; return nullptr; } return reg; } region* region::intersect(region *r) { region *reg = new region(); reg->area = 0; for(vector<true_rect>::iterator it = r->v.begin(); it != r->v.end(); ++it) { region *tmp = this->intersect(*it); if (tmp == nullptr) { continue; } for(vector<true_rect>::iterator it2 = tmp->v.begin(); it2 != tmp->v.end(); ++it2) { reg->v.push_back(*it2); reg->area += it2->area; } delete tmp; } if (reg->area == 0) { delete reg; return nullptr; } return reg; } void region::add_true_rect(true_rect tr) { v.push_back(tr); area += tr.area; } void region::print_area() { printf("area = %lld\n", this->area); } void region::print() { for(vector<true_rect>::iterator it = this->v.begin(); it != this->v.end(); ++it) { (*it).print(); } } class rect { public: vector<region*> v; rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2); void print(); }; rect::rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) { region *a, *b, *c, *d; a = new region ( true_rect(x1, y1, x2, y2) ); b = new region ( true_rect(x2, y1, x, y2) ); c = new region ( true_rect(x1, y2, x2, y) ); d = new region ( true_rect(x2, y2, x, y) ); if (x1 > 0) { b->add_true_rect( true_rect(0, y1, x1, y2) ); } if (y1 > 0) { c->add_true_rect( true_rect(x1, 0, x2, y1) ); } if (x1 > 0 && y1 > 0) { d->add_true_rect( true_rect(0, 0, x1, y1) ); } if (x1 > 0) { d->add_true_rect( true_rect(0, y2, x1, y) ); } if (y1 > 0) { d->add_true_rect( true_rect(x2, 0, x, y1) ); } v.push_back(a); v.push_back(b); v.push_back(c); v.push_back(d); } void rect::print() { printf("area a=%llu, b=%llu, c=%llu, d=%llu\n", v[0]->area, v[1]->area, v[2]->area, v[3]->area); } priority_queue<region*, vector<region*>, region_cmp> make_initial_q(rect *r1, rect *r2) { priority_queue<region*, vector<region*>, region_cmp> q; for(vector<region*>::iterator it = r1->v.begin(); it != r1->v.end(); ++it) { for(vector<region*>::iterator it2 = r2->v.begin(); it2 != r2->v.end(); ++it2) { region *tmp = (*it)->intersect(*it2); if (tmp != nullptr) { tmp->nr_processed = 2; q.push(tmp); } } } return q; } int main() { scanf("%u %u %u\n", &n, &x, &y); vector<rect*> v; priority_queue<region*, vector<region*>, region_cmp> q; for(int i = 0; i < n; ++i) { unsigned int x1, y1, x2, y2; scanf("%u %u %u %u\n", &x1, &y1, &x2, &y2); rect *r = new rect( min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2) ); //r->print(); v.push_back(r); } if (n == 1) { for(vector<region*>::iterator it = v[0]->v.begin(); it != v[0]->v.end(); ++it) { (*it)->nr_processed = 1; q.push( *it ); } } else { q = make_initial_q( v[0], v[1] ); } while (!q.empty()) { region *r = q.top(); region *rnew; if (r->nr_processed >= n) { printf("%llu\n", r->area); return 0; } q.pop(); //r->print_area(); //r->print(); //printf("***\n"); rnew = r->intersect( v[r->nr_processed]->v[0] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[1] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[2] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[3] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } delete r; } 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 | #include <cstdio> #include <cassert> #include <algorithm> #include <vector> #include <queue> using namespace std; unsigned int x, y, n; class true_rect { public: unsigned int x1, y1, x2, y2; unsigned long long area; true_rect(unsigned int a1, unsigned int b1, unsigned int a2, unsigned int b2); true_rect intersect(true_rect &tr); void print(); }; true_rect::true_rect(unsigned int a1, unsigned int b1, unsigned int a2, unsigned int b2) { x1 = min(a1, a2); x2 = max(a1, a2); y1 = min(b1, b2); y2 = max(b1, b2); area = (static_cast<long long>(x2-x1)) * (static_cast<long long>(y2-y1)); // printf("(%u, %u) - (%u, %u) - area=%llu\n", x1, y1, x2, y2, area); } true_rect true_rect::intersect(true_rect &tr) { if (this->x1 >= tr.x2 || tr.x1 >= this->x2) return true_rect(0, 0, 0, 0); if (this->y1 >= tr.y2 || tr.y1 >= this->y2) return true_rect(0, 0, 0, 0); return true_rect( max(this->x1, tr.x1), max(this->y1, tr.y1), min(this->x2, tr.x2), min(this->y2, tr.y2) ); } void true_rect::print() { printf(" (%u, %u) - (%u, %u) - (%llu)\n", x1, y1, x2, y2, area); } class region { public: vector<true_rect> v; long long area; int nr_processed; region() {}; region(true_rect tr); region* intersect(true_rect r); region* intersect(region *r); void add_true_rect(true_rect tr); void print_area(); void print(); }; class region_cmp { public: bool operator()(const region *l, region *r) { if (l->area == r->area) { return l->nr_processed < r->nr_processed; } return l->area < r->area; } }; region::region(true_rect tr) { v.push_back(tr); area = tr.area; } region* region::intersect(true_rect r) { region *reg = new region(); reg->area = 0; for(vector<true_rect>::iterator it = this->v.begin(); it != this->v.end(); ++it) { true_rect tr = it->intersect(r); if (tr.area != 0) { reg->v.push_back(tr); reg->area += tr.area; } } if (reg->area == 0) { delete reg; return nullptr; } return reg; } region* region::intersect(region *r) { region *reg = new region(); reg->area = 0; for(vector<true_rect>::iterator it = r->v.begin(); it != r->v.end(); ++it) { region *tmp = this->intersect(*it); if (tmp == nullptr) { continue; } for(vector<true_rect>::iterator it2 = tmp->v.begin(); it2 != tmp->v.end(); ++it2) { reg->v.push_back(*it2); reg->area += it2->area; } delete tmp; } if (reg->area == 0) { delete reg; return nullptr; } return reg; } void region::add_true_rect(true_rect tr) { v.push_back(tr); area += tr.area; } void region::print_area() { printf("area = %lld\n", this->area); } void region::print() { for(vector<true_rect>::iterator it = this->v.begin(); it != this->v.end(); ++it) { (*it).print(); } } class rect { public: vector<region*> v; rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2); void print(); }; rect::rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) { region *a, *b, *c, *d; a = new region ( true_rect(x1, y1, x2, y2) ); b = new region ( true_rect(x2, y1, x, y2) ); c = new region ( true_rect(x1, y2, x2, y) ); d = new region ( true_rect(x2, y2, x, y) ); if (x1 > 0) { b->add_true_rect( true_rect(0, y1, x1, y2) ); } if (y1 > 0) { c->add_true_rect( true_rect(x1, 0, x2, y1) ); } if (x1 > 0 && y1 > 0) { d->add_true_rect( true_rect(0, 0, x1, y1) ); } if (x1 > 0) { d->add_true_rect( true_rect(0, y2, x1, y) ); } if (y1 > 0) { d->add_true_rect( true_rect(x2, 0, x, y1) ); } v.push_back(a); v.push_back(b); v.push_back(c); v.push_back(d); } void rect::print() { printf("area a=%llu, b=%llu, c=%llu, d=%llu\n", v[0]->area, v[1]->area, v[2]->area, v[3]->area); } priority_queue<region*, vector<region*>, region_cmp> make_initial_q(rect *r1, rect *r2) { priority_queue<region*, vector<region*>, region_cmp> q; for(vector<region*>::iterator it = r1->v.begin(); it != r1->v.end(); ++it) { for(vector<region*>::iterator it2 = r2->v.begin(); it2 != r2->v.end(); ++it2) { region *tmp = (*it)->intersect(*it2); if (tmp != nullptr) { tmp->nr_processed = 2; q.push(tmp); } } } return q; } int main() { scanf("%u %u %u\n", &n, &x, &y); vector<rect*> v; priority_queue<region*, vector<region*>, region_cmp> q; for(int i = 0; i < n; ++i) { unsigned int x1, y1, x2, y2; scanf("%u %u %u %u\n", &x1, &y1, &x2, &y2); rect *r = new rect( min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2) ); //r->print(); v.push_back(r); } if (n == 1) { for(vector<region*>::iterator it = v[0]->v.begin(); it != v[0]->v.end(); ++it) { (*it)->nr_processed = 1; q.push( *it ); } } else { q = make_initial_q( v[0], v[1] ); } while (!q.empty()) { region *r = q.top(); region *rnew; if (r->nr_processed >= n) { printf("%llu\n", r->area); return 0; } q.pop(); //r->print_area(); //r->print(); //printf("***\n"); rnew = r->intersect( v[r->nr_processed]->v[0] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[1] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[2] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } rnew = r->intersect( v[r->nr_processed]->v[3] ); if (rnew != nullptr) { rnew->nr_processed = r->nr_processed+1; q.push(rnew); } delete r; } return 0; } |