#define READ_NUM_TEST_CASES false #define USE_FAST_IO false //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/template.hpp" BEGINS HERE // #include <bits/stdc++.h> using namespace std; //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/abbrevs.hpp" BEGINS HERE // #define UI unsigned int #define LL long long #define ULL unsigned long long #define PII pair<int,int> #define RI ri() #define RUI rui() #define RLL rll() #define RULL rull() #define RSTR rstr() #define FOR(i,n) for(int i=0; i<int(n); ++i) #define FO(i,a,b) for(int i=(a); i<int(b); ++i) #define OF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define MIN(a,b) ((a)<(b) ? (a) : (b)) #define MAX(a,b) ((b)<(a) ? (a) : (b)) #define REMIN(a,b) ((a) = min(a,b)) #define REMAX(a,b) ((a) = max(a,b)) #define ALL(c) (c).begin(),(c).end() #define SQR(x) ((x)*(x)) bool is_pow(int x) { return (x&(x-1)) == 0; } // // AMALGAMATE: "../include/abbrevs.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #if USE_FAST_IO //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/fast-io.hpp" BEGINS HERE // //#define FREAD fread //#define FWRITE fwrite #define FREAD fread_unlocked #define FWRITE fwrite_unlocked struct Buffer { FILE* const stream; const int buffer_size; uint8_t* const buff; int pos = 0; Buffer(FILE* a_stream, int a_buffer_size) : stream( a_stream ), buffer_size( a_buffer_size ), buff( new uint8_t[ buffer_size ] ) { setvbuf(stream, NULL, _IONBF, 0); } ~Buffer() { delete[] buff; } }; class In_Buffer : private Buffer { public: In_Buffer(FILE* a_stream, int a_buffer_size) : Buffer(a_stream, a_buffer_size) { FREAD(buff, 1, buffer_size, stream); } public: void prepare(int num_chars) { auto remaining = buffer_size - pos; if(remaining >= num_chars) return; assert(remaining >= 0); // prevent compiler warning memcpy(buff, buff+pos, remaining); int read = FREAD(buff+remaining, 1, pos, stream); int block_end = remaining + read; memset(buff + block_end, 0, buffer_size - block_end); pos = 0; } public: char get_unchecked() { return buff[ pos++ ]; } }; template<class INT> INT read_unsigned(In_Buffer& buff) { buff.prepare(25); char c = buff.get_unchecked(); while(c < '-') c = buff.get_unchecked(); INT r = 0; for(;;) { if(c < '0') return r; c -= '0'; r = r*10 + c; c = buff.get_unchecked(); } } template<class INT> INT read_signed(In_Buffer& buff) { buff.prepare(25); char c = buff.get_unchecked(); while(c < '-') c = buff.get_unchecked(); bool minus = false; if(c=='-') { c = buff.get_unchecked(); minus = true; } INT r = 0; for(;;) { if(c < '0') return minus ? -r : r; r = r*10 + (c-'0'); c = buff.get_unchecked(); } } In_Buffer& operator>>( In_Buffer& buff, char& c) { buff.prepare(1); c = buff.get_unchecked(); return buff; } In_Buffer& operator>>( In_Buffer& buff, UI& r) { r = read_unsigned<UI>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, ULL& r) { r = read_unsigned<ULL>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, int& r) { r = read_signed<int>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, LL& r) { r = read_signed<LL>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, string& r) { r.clear(); char c = buff.get_unchecked(); while(c <= ' ') c = buff.get_unchecked(); do { r.push_back( c ); c = buff.get_unchecked(); } while(c > ' '); return buff; } class Out_Buffer : private Buffer { public: Out_Buffer(FILE* a_stream, int a_buffer_size) : Buffer(a_stream, a_buffer_size) {} ~Out_Buffer() { flush(); } public: void prepare(int num_chars) { if(buffer_size - pos >= num_chars) return; flush(); } void flush() { FWRITE(buff, 1, pos, stream); pos = 0; } public: void put_unchecked(const char& c) { buff[ pos++ ] = c; } void put_unchecked(const char* str, int len) { memcpy(buff+pos, str, len); pos += len; } }; template<class INT> void write_signed(Out_Buffer& buff, const INT& i) { buff.prepare(25); if(i == 0) { buff.put_unchecked('0'); return; } auto ci = i; if(ci < 0) { buff.put_unchecked('-'); ci = -ci; } const int sz = 25; char temp[sz]; int pos = sz; while(ci) { temp[ --pos ] = '0' + ci%10; ci/=10; } buff.put_unchecked(temp+pos, sz-pos); } template<class INT> void write_unsigned(Out_Buffer& buff, const INT& i) { buff.prepare(25); if(i==0) { buff.put_unchecked('0'); return; } const int sz = 25; char temp[sz]; int pos = sz; auto ci = i; while(ci) { temp[ --pos ] = '0' + ci%10; ci/=10; } buff.put_unchecked(temp+pos, sz-pos); } Out_Buffer& operator<<( Out_Buffer& buff, const char& c) { buff.prepare(1); buff.put_unchecked( c ); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const UI& r) { write_unsigned<UI>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const ULL& r) { write_unsigned<ULL>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const int& r) { write_signed<int>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const LL& r) { write_signed<LL>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const char* cstr) { auto len = strlen(cstr); buff.prepare(len); buff.put_unchecked(cstr, len); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const string& str) { buff.prepare( str.size() ); buff.put_unchecked(str.data(), str.size()); return buff; } #define BUFF_SIZE 32768 In_Buffer fast_cin( stdin, BUFF_SIZE ); Out_Buffer fast_cout( stdout, BUFF_SIZE ); //Out_Buffer fast_cerr( stderr, BUFF_SIZE ); char fast_endl = '\n'; // // AMALGAMATE: "../include/fast-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #define cin fast_cin #define cout fast_cout #define endl fast_endl #else //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/slow-io.hpp" BEGINS HERE // struct Slow_IO_Singleton { Slow_IO_Singleton() { ios_base::sync_with_stdio( false ); std::cin.tie( nullptr ); std::cout.tie( nullptr ); setlocale(LC_ALL,"C"); const int buffer_size = 32768; static char stdin_buffer[ buffer_size ]; static char stdout_buffer[ buffer_size ]; cin.rdbuf()->pubsetbuf( stdin_buffer, buffer_size ); cout.rdbuf()->pubsetbuf( stdout_buffer, buffer_size ); } ~Slow_IO_Singleton() { } }; Slow_IO_Singleton slow_io_singleton; // // AMALGAMATE: "../include/slow-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #define endl '\n' #endif //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/abbrevs-io.hpp" BEGINS HERE // int ri() { int r; cin >> r; return r; } UI rui() { UI r; cin >> r; return r; } LL rll() { LL r; cin >> r; return r; } ULL rull() { ULL r; cin >> r; return r; } string rstr() {string s; cin >> s; return s; } // // AMALGAMATE: "../include/abbrevs-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/debug.hpp" BEGINS HERE // #define ASS(x) assert(x) // // AMALGAMATE: "../include/debug.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// // // for salgo #define CONTEST_MODE namespace salgo {}; using namespace salgo; // // // AMALGAMATE: "../include/template.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// //#include <salgo/graph.hpp> // c++17 //#include <salgo/binomial.hpp> //#include <salgo/modulo.hpp> //#include <salgo/common.hpp> struct R { LL numer = 0; LL denom = 1; R() = default; R(LL x) : numer(x) {} R(LL n, LL d) : numer(n), denom(d) {} R& gcd() { auto g = __gcd(denom, numer); numer /= g; denom /= g; if(denom < 0) { numer *= -1; denom *= -1; } // rounding... while(fabs(numer) > 2 * 1000 * 1000 * 1000 || fabs(denom) > 2 * 1000 * 1000 * 1000) { numer /= 2; denom /= 2; if(denom == 0) cerr << "asd " << numer << endl; } assert(denom > 0); return *this; } explicit operator long double() const { return 1.0L * numer / denom; } R operator-() const { return R{ -numer, denom }; } // debug LL max_abs() const { return max(abs(numer), abs(denom)); } }; ostream& operator<<(ostream& os, const R& r) { return os << r.numer << "/" << r.denom; } R operator*(const R& a, const R& b) { R r = {a.numer * b.numer, a.denom * b.denom}; return r.gcd(); } R operator/(const R& a, const R& b) { R r = {a.numer * b.denom, a.denom * b.numer}; return r.gcd(); } R operator+(const R& a, const R& b) { R r = { a.numer * b.denom + b.numer * a.denom, a.denom * b.denom }; return r.gcd(); } R operator-(const R& a, const R& b) { R r = { a.numer * b.denom - b.numer * a.denom, a.denom * b.denom }; return r.gcd(); } bool operator<(const R& a, const R& b) { return (a-b).numer < 0; } bool operator>(const R& a, const R& b) { return (a-b).numer > 0; } bool operator<=(const R& a, const R& b) { return (a-b).numer <= 0; } bool operator>=(const R& a, const R& b) { return (a-b).numer >= 0; } bool operator==(const R& a, const R& b) { return a.numer == b.numer && a.denom == b.denom; } bool operator!=(const R& a, const R& b) { return a.numer != b.numer || a.denom != b.denom; } bool isnan(const R& r) { return r.numer == 0 && r.denom == 0; } using F = long double; //#define BAD_F {0,0} #define BAD_F std::numeric_limits<F>::quiet_NaN() struct Vec { array<F,2> pos = {{0, 0}}; Vec() = default; Vec(const F& x, const F& y) : pos{{x,y}} {} F& operator[](int i) { return pos[i]; } const F& operator[](int i) const { return pos[i]; } Vec operator-() const { return {-pos[0], -pos[1]}; } }; F operator*(const Vec& a, const Vec& b) { return a[0] * b[0] + a[1] * b[1]; } Vec operator+(const Vec& a, const Vec& b) { return Vec(a[0] + b[0], a[1] + b[1]); } Vec operator-(const Vec& a, const Vec& b) { return Vec(a[0] - b[0], a[1] - b[1]); } bool operator==(const Vec& a, const Vec& b) { return a.pos == b.pos; } ostream& operator<<(ostream& os, const Vec& vec) { os.precision(2); return os << "[" << vec.pos[0] << ", " << vec.pos[1] << "]"; } F det(const Vec& a, const Vec& b) { return a[0] * b[1] - a[1] * b[0]; } F orient(const Vec& a, const Vec& b, const Vec& c) { return det(b-a, c-a); } Vec operator*(const Vec& v, F alpha) { return {v[0] * alpha, v[1] * alpha}; } Vec operator*(F alpha, const Vec& v) { return {v[0] * alpha, v[1] * alpha}; } struct Range { int fr, to; }; ostream& operator<<(ostream& os, const Range& range) { return os << range.fr << "->" << range.to; } template<class VEC> void cycle(VEC& vec, int how_many) { if(how_many == 0) return; how_many = (how_many + vec.size()*10) % vec.size(); vec.insert( vec.end(), vec.begin(), vec.begin() + how_many ); vec.erase( vec.begin(), vec.begin() + how_many ); } struct Poly; ostream& operator<<(ostream& os, const Poly& poly); struct Poly { vector<Vec> pts; Vec& operator[](int i) { return pts[fix_range(i)]; } const Vec& operator[](int i) const { return pts[fix_range(i)]; } int size() const { return pts.size(); } bool empty() const { return pts.empty(); } void push_back(const Vec& v) { pts.push_back(v); } int fix_range(int i) const { return (i + 100*pts.size()) % pts.size(); } long double area() const { long double r = 0; FO(i, 0, pts.size()-2) { r += (long double) orient(pts[i], pts[i+1], pts.back()); } return r / 2; } // inclusive int remove_range(Range range) { range.fr = fix_range(range.fr); range.to = fix_range(range.to); if(range.fr <= range.to) { pts.erase(pts.begin() + range.fr, pts.begin() + range.to+1); return range.fr; } else { pts.erase(pts.begin() + range.fr, pts.end()); pts.erase(pts.begin(), pts.begin() + range.to+1); return 0; } } }; ostream& operator<<(ostream& os, const Poly& poly) { os << "["; for(auto& e : poly.pts) { os << e << " "; } os << "]"; return os; } struct Segment { Vec fr, to; }; /* Vec cut(const Segment& a, const Segment& b) { auto total = det(a.to - a.fr, b.to - b.fr); if(total == 0) return {BAD_F, BAD_F}; auto part_a = det(b.to - b.fr, a.fr - b.fr); auto alpha_a = part_a / total; return a.fr + (a.to - a.fr) * alpha_a; } */ struct Test_Case { int n = RI; struct Mag { array<Vec,2> towers; }; vector<Mag> mags; //vector<Segment> debug_segments; Poly poly; /* void debug_svg() { ofstream os("input.svg"); int x0 = 0; int y0 = 0; int x1 = 1000; int y1 = 1000; double scale = 10; double shift_x = 25; double shift_y = 25; os << "<svg viewBox='" << x0 << " " << y0 << " " << x1 << " " << y1 << "' xmlns='http://www.w3.org/2000/svg'>" << endl; os << "<rect width='100%' height='100%' fill='black'/>" << endl; FOR(m, n) { os << "<line x1='" << ((long double)mags[m].towers[0].pos[0] + shift_x) * scale << "' y1='" << ((long double)mags[m].towers[0].pos[1] + shift_y) * scale << "' x2='" << ((long double)mags[m].towers[1].pos[0] + shift_x) * scale << "' y2='" << ((long double)mags[m].towers[1].pos[1] + shift_y) * scale << "' style='stroke:rgb(255,255,255);stroke-width:2' />" << endl; } for(auto& s : debug_segments) { os << "<line x1='" << ((long double)s.fr.pos[0] + shift_x) * scale << "' y1='" << ((long double)s.fr.pos[1] + shift_y) * scale << "' x2='" << ((long double)s.to.pos[0] + shift_x) * scale << "' y2='" << ((long double)s.to.pos[1] + shift_y) * scale << "' style='stroke:rgb(0,255,0);stroke-width:1' />" << endl; } os << "<polygon points='"; for(auto& p : poly.pts) os << ((long double)p[0]+shift_x) * scale << "," << ((long double)p[1]+shift_y)*scale << " "; os << "' style='fill:none;stroke:red;stroke-width:2' />" << endl; os << "</svg>" << endl; } */ void solve() { // cout << "Case #" << icase+1 << ": "; mags.resize(n); FOR(i, n) { mags[i].towers[0].pos[0] = RI; mags[i].towers[0].pos[1] = RI; mags[i].towers[1].pos[0] = RI; mags[i].towers[1].pos[1] = RI; } vector<Vec> dirs; FOR(i, n) { FO(j, i+1, n) { FOR(aa,2) FOR(bb,2) { auto seg = Segment{ mags[i].towers[aa], mags[j].towers[bb] }; auto dir = seg.to - seg.fr; swap(dir[0], dir[1]); dir[0] = -dir[0]; dirs.push_back( dir ); dirs.push_back( -dir ); } } } // sort dirs { vector<Vec> dirs_by_half[2]; auto is_in_lower = [](const Vec& v) { if(v[1] != 0) return v[1] < 0; return v[0] < 0; }; for(auto& d : dirs) { dirs_by_half[ is_in_lower(d) ].push_back( d ); } FOR(i,2) sort(ALL(dirs_by_half[i]), [](const Vec& a, const Vec& b) { return det(a,b) > 0; }); dirs.clear(); FOR(i,2) dirs.insert(dirs.end(), ALL(dirs_by_half[i])); } //cerr << "dirs: " << endl; //for(auto& d : dirs) cerr << " " << d << endl; //cerr << " (" << dirs.size() << " dirs total)" << endl; /* { LL max_abs = 0; for(auto& d : dirs) { REMAX(max_abs, d[0].max_abs()); REMAX(max_abs, d[1].max_abs()); } cerr << "dirs max abs: " << max_abs << endl; // 3 digits } */ poly.push_back({-512, -512}); poly.push_back({ 512, -512}); poly.push_back({ 512, 512}); poly.push_back({-512, 512}); //FOR(i,n) FO(j,i+1,n) FOR(k,n) FO(l,k+1,n) { // cerr << "test " << cut(Segment{mags[i].towers[0], mags[j].towers[0]}, Segment{mags[k].towers[0], mags[l].towers[0]}) << endl; //} int cvert = 0; for(auto& d : dirs) { F best_dot = INT_MAX; FOR(m,n) { F cand = max(d * mags[m].towers[0], d * mags[m].towers[1]); REMIN(best_dot, cand); } //cerr << "best_dot is " << best_dot << endl; // 6 digits, integer // poly_vert_coords * dot -> 6+6=12 digits // update cvert while((poly[cvert+1] - poly[cvert]) * d < 0) ++cvert; // 6+18 digits! while((poly[cvert-1] - poly[cvert]) * d < 0) --cvert; if(d * poly[cvert] >= best_dot) continue; // find range Range range = {cvert, cvert}; while(d * poly[range.fr-1] < best_dot) { --range.fr; if(poly.fix_range(range.fr) == poly.fix_range(range.to)) { cout << 0 << endl; return; } } while(d * poly[range.to+1] < best_dot) ++range.to; //cerr << " range is " << range << endl; auto weight_by_dot = [](const Vec& close, const Vec& far, const Vec& dd, F target_dot) { auto close_dot = close * dd; auto far_dot = far * dd; if(close_dot == far_dot) { assert(target_dot == close_dot); return close; // close is priority } F alpha = (target_dot - close_dot) / (far_dot - close_dot); return close + alpha * (far - close); }; array<Vec,2> v = { weight_by_dot(poly[range.fr], poly[range.fr-1], d, best_dot), weight_by_dot(poly[range.to], poly[range.to+1], d, best_dot) }; //cerr << endl; //cerr << "new point v0 " << v[0] << endl; //cerr << "new point v1 " << v[1] << endl; cvert = poly.remove_range(range); poly.pts.insert(poly.pts.begin() + cvert, ALL(v)); } cout.precision(500); cout << poly.area() << endl; //debug_svg(); } int icase = -1; }; // struct Test_Case int main() { int num_cases = READ_NUM_TEST_CASES ? RI : 1; FOR(i, num_cases) { Test_Case tc; tc.icase = i; tc.solve(); } 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | #define READ_NUM_TEST_CASES false #define USE_FAST_IO false //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/template.hpp" BEGINS HERE // #include <bits/stdc++.h> using namespace std; //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/abbrevs.hpp" BEGINS HERE // #define UI unsigned int #define LL long long #define ULL unsigned long long #define PII pair<int,int> #define RI ri() #define RUI rui() #define RLL rll() #define RULL rull() #define RSTR rstr() #define FOR(i,n) for(int i=0; i<int(n); ++i) #define FO(i,a,b) for(int i=(a); i<int(b); ++i) #define OF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define MIN(a,b) ((a)<(b) ? (a) : (b)) #define MAX(a,b) ((b)<(a) ? (a) : (b)) #define REMIN(a,b) ((a) = min(a,b)) #define REMAX(a,b) ((a) = max(a,b)) #define ALL(c) (c).begin(),(c).end() #define SQR(x) ((x)*(x)) bool is_pow(int x) { return (x&(x-1)) == 0; } // // AMALGAMATE: "../include/abbrevs.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #if USE_FAST_IO //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/fast-io.hpp" BEGINS HERE // //#define FREAD fread //#define FWRITE fwrite #define FREAD fread_unlocked #define FWRITE fwrite_unlocked struct Buffer { FILE* const stream; const int buffer_size; uint8_t* const buff; int pos = 0; Buffer(FILE* a_stream, int a_buffer_size) : stream( a_stream ), buffer_size( a_buffer_size ), buff( new uint8_t[ buffer_size ] ) { setvbuf(stream, NULL, _IONBF, 0); } ~Buffer() { delete[] buff; } }; class In_Buffer : private Buffer { public: In_Buffer(FILE* a_stream, int a_buffer_size) : Buffer(a_stream, a_buffer_size) { FREAD(buff, 1, buffer_size, stream); } public: void prepare(int num_chars) { auto remaining = buffer_size - pos; if(remaining >= num_chars) return; assert(remaining >= 0); // prevent compiler warning memcpy(buff, buff+pos, remaining); int read = FREAD(buff+remaining, 1, pos, stream); int block_end = remaining + read; memset(buff + block_end, 0, buffer_size - block_end); pos = 0; } public: char get_unchecked() { return buff[ pos++ ]; } }; template<class INT> INT read_unsigned(In_Buffer& buff) { buff.prepare(25); char c = buff.get_unchecked(); while(c < '-') c = buff.get_unchecked(); INT r = 0; for(;;) { if(c < '0') return r; c -= '0'; r = r*10 + c; c = buff.get_unchecked(); } } template<class INT> INT read_signed(In_Buffer& buff) { buff.prepare(25); char c = buff.get_unchecked(); while(c < '-') c = buff.get_unchecked(); bool minus = false; if(c=='-') { c = buff.get_unchecked(); minus = true; } INT r = 0; for(;;) { if(c < '0') return minus ? -r : r; r = r*10 + (c-'0'); c = buff.get_unchecked(); } } In_Buffer& operator>>( In_Buffer& buff, char& c) { buff.prepare(1); c = buff.get_unchecked(); return buff; } In_Buffer& operator>>( In_Buffer& buff, UI& r) { r = read_unsigned<UI>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, ULL& r) { r = read_unsigned<ULL>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, int& r) { r = read_signed<int>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, LL& r) { r = read_signed<LL>(buff); return buff; } In_Buffer& operator>>( In_Buffer& buff, string& r) { r.clear(); char c = buff.get_unchecked(); while(c <= ' ') c = buff.get_unchecked(); do { r.push_back( c ); c = buff.get_unchecked(); } while(c > ' '); return buff; } class Out_Buffer : private Buffer { public: Out_Buffer(FILE* a_stream, int a_buffer_size) : Buffer(a_stream, a_buffer_size) {} ~Out_Buffer() { flush(); } public: void prepare(int num_chars) { if(buffer_size - pos >= num_chars) return; flush(); } void flush() { FWRITE(buff, 1, pos, stream); pos = 0; } public: void put_unchecked(const char& c) { buff[ pos++ ] = c; } void put_unchecked(const char* str, int len) { memcpy(buff+pos, str, len); pos += len; } }; template<class INT> void write_signed(Out_Buffer& buff, const INT& i) { buff.prepare(25); if(i == 0) { buff.put_unchecked('0'); return; } auto ci = i; if(ci < 0) { buff.put_unchecked('-'); ci = -ci; } const int sz = 25; char temp[sz]; int pos = sz; while(ci) { temp[ --pos ] = '0' + ci%10; ci/=10; } buff.put_unchecked(temp+pos, sz-pos); } template<class INT> void write_unsigned(Out_Buffer& buff, const INT& i) { buff.prepare(25); if(i==0) { buff.put_unchecked('0'); return; } const int sz = 25; char temp[sz]; int pos = sz; auto ci = i; while(ci) { temp[ --pos ] = '0' + ci%10; ci/=10; } buff.put_unchecked(temp+pos, sz-pos); } Out_Buffer& operator<<( Out_Buffer& buff, const char& c) { buff.prepare(1); buff.put_unchecked( c ); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const UI& r) { write_unsigned<UI>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const ULL& r) { write_unsigned<ULL>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const int& r) { write_signed<int>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const LL& r) { write_signed<LL>(buff, r); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const char* cstr) { auto len = strlen(cstr); buff.prepare(len); buff.put_unchecked(cstr, len); return buff; } Out_Buffer& operator<<( Out_Buffer& buff, const string& str) { buff.prepare( str.size() ); buff.put_unchecked(str.data(), str.size()); return buff; } #define BUFF_SIZE 32768 In_Buffer fast_cin( stdin, BUFF_SIZE ); Out_Buffer fast_cout( stdout, BUFF_SIZE ); //Out_Buffer fast_cerr( stderr, BUFF_SIZE ); char fast_endl = '\n'; // // AMALGAMATE: "../include/fast-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #define cin fast_cin #define cout fast_cout #define endl fast_endl #else //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/slow-io.hpp" BEGINS HERE // struct Slow_IO_Singleton { Slow_IO_Singleton() { ios_base::sync_with_stdio( false ); std::cin.tie( nullptr ); std::cout.tie( nullptr ); setlocale(LC_ALL,"C"); const int buffer_size = 32768; static char stdin_buffer[ buffer_size ]; static char stdout_buffer[ buffer_size ]; cin.rdbuf()->pubsetbuf( stdin_buffer, buffer_size ); cout.rdbuf()->pubsetbuf( stdout_buffer, buffer_size ); } ~Slow_IO_Singleton() { } }; Slow_IO_Singleton slow_io_singleton; // // AMALGAMATE: "../include/slow-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// #define endl '\n' #endif //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/abbrevs-io.hpp" BEGINS HERE // int ri() { int r; cin >> r; return r; } UI rui() { UI r; cin >> r; return r; } LL rll() { LL r; cin >> r; return r; } ULL rull() { ULL r; cin >> r; return r; } string rstr() {string s; cin >> s; return s; } // // AMALGAMATE: "../include/abbrevs-io.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // AMALGAMATE: "../include/debug.hpp" BEGINS HERE // #define ASS(x) assert(x) // // AMALGAMATE: "../include/debug.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// // // for salgo #define CONTEST_MODE namespace salgo {}; using namespace salgo; // // // AMALGAMATE: "../include/template.hpp" ENDS HERE //////////////////////////////////////////////////////////////////////////////// //#include <salgo/graph.hpp> // c++17 //#include <salgo/binomial.hpp> //#include <salgo/modulo.hpp> //#include <salgo/common.hpp> struct R { LL numer = 0; LL denom = 1; R() = default; R(LL x) : numer(x) {} R(LL n, LL d) : numer(n), denom(d) {} R& gcd() { auto g = __gcd(denom, numer); numer /= g; denom /= g; if(denom < 0) { numer *= -1; denom *= -1; } // rounding... while(fabs(numer) > 2 * 1000 * 1000 * 1000 || fabs(denom) > 2 * 1000 * 1000 * 1000) { numer /= 2; denom /= 2; if(denom == 0) cerr << "asd " << numer << endl; } assert(denom > 0); return *this; } explicit operator long double() const { return 1.0L * numer / denom; } R operator-() const { return R{ -numer, denom }; } // debug LL max_abs() const { return max(abs(numer), abs(denom)); } }; ostream& operator<<(ostream& os, const R& r) { return os << r.numer << "/" << r.denom; } R operator*(const R& a, const R& b) { R r = {a.numer * b.numer, a.denom * b.denom}; return r.gcd(); } R operator/(const R& a, const R& b) { R r = {a.numer * b.denom, a.denom * b.numer}; return r.gcd(); } R operator+(const R& a, const R& b) { R r = { a.numer * b.denom + b.numer * a.denom, a.denom * b.denom }; return r.gcd(); } R operator-(const R& a, const R& b) { R r = { a.numer * b.denom - b.numer * a.denom, a.denom * b.denom }; return r.gcd(); } bool operator<(const R& a, const R& b) { return (a-b).numer < 0; } bool operator>(const R& a, const R& b) { return (a-b).numer > 0; } bool operator<=(const R& a, const R& b) { return (a-b).numer <= 0; } bool operator>=(const R& a, const R& b) { return (a-b).numer >= 0; } bool operator==(const R& a, const R& b) { return a.numer == b.numer && a.denom == b.denom; } bool operator!=(const R& a, const R& b) { return a.numer != b.numer || a.denom != b.denom; } bool isnan(const R& r) { return r.numer == 0 && r.denom == 0; } using F = long double; //#define BAD_F {0,0} #define BAD_F std::numeric_limits<F>::quiet_NaN() struct Vec { array<F,2> pos = {{0, 0}}; Vec() = default; Vec(const F& x, const F& y) : pos{{x,y}} {} F& operator[](int i) { return pos[i]; } const F& operator[](int i) const { return pos[i]; } Vec operator-() const { return {-pos[0], -pos[1]}; } }; F operator*(const Vec& a, const Vec& b) { return a[0] * b[0] + a[1] * b[1]; } Vec operator+(const Vec& a, const Vec& b) { return Vec(a[0] + b[0], a[1] + b[1]); } Vec operator-(const Vec& a, const Vec& b) { return Vec(a[0] - b[0], a[1] - b[1]); } bool operator==(const Vec& a, const Vec& b) { return a.pos == b.pos; } ostream& operator<<(ostream& os, const Vec& vec) { os.precision(2); return os << "[" << vec.pos[0] << ", " << vec.pos[1] << "]"; } F det(const Vec& a, const Vec& b) { return a[0] * b[1] - a[1] * b[0]; } F orient(const Vec& a, const Vec& b, const Vec& c) { return det(b-a, c-a); } Vec operator*(const Vec& v, F alpha) { return {v[0] * alpha, v[1] * alpha}; } Vec operator*(F alpha, const Vec& v) { return {v[0] * alpha, v[1] * alpha}; } struct Range { int fr, to; }; ostream& operator<<(ostream& os, const Range& range) { return os << range.fr << "->" << range.to; } template<class VEC> void cycle(VEC& vec, int how_many) { if(how_many == 0) return; how_many = (how_many + vec.size()*10) % vec.size(); vec.insert( vec.end(), vec.begin(), vec.begin() + how_many ); vec.erase( vec.begin(), vec.begin() + how_many ); } struct Poly; ostream& operator<<(ostream& os, const Poly& poly); struct Poly { vector<Vec> pts; Vec& operator[](int i) { return pts[fix_range(i)]; } const Vec& operator[](int i) const { return pts[fix_range(i)]; } int size() const { return pts.size(); } bool empty() const { return pts.empty(); } void push_back(const Vec& v) { pts.push_back(v); } int fix_range(int i) const { return (i + 100*pts.size()) % pts.size(); } long double area() const { long double r = 0; FO(i, 0, pts.size()-2) { r += (long double) orient(pts[i], pts[i+1], pts.back()); } return r / 2; } // inclusive int remove_range(Range range) { range.fr = fix_range(range.fr); range.to = fix_range(range.to); if(range.fr <= range.to) { pts.erase(pts.begin() + range.fr, pts.begin() + range.to+1); return range.fr; } else { pts.erase(pts.begin() + range.fr, pts.end()); pts.erase(pts.begin(), pts.begin() + range.to+1); return 0; } } }; ostream& operator<<(ostream& os, const Poly& poly) { os << "["; for(auto& e : poly.pts) { os << e << " "; } os << "]"; return os; } struct Segment { Vec fr, to; }; /* Vec cut(const Segment& a, const Segment& b) { auto total = det(a.to - a.fr, b.to - b.fr); if(total == 0) return {BAD_F, BAD_F}; auto part_a = det(b.to - b.fr, a.fr - b.fr); auto alpha_a = part_a / total; return a.fr + (a.to - a.fr) * alpha_a; } */ struct Test_Case { int n = RI; struct Mag { array<Vec,2> towers; }; vector<Mag> mags; //vector<Segment> debug_segments; Poly poly; /* void debug_svg() { ofstream os("input.svg"); int x0 = 0; int y0 = 0; int x1 = 1000; int y1 = 1000; double scale = 10; double shift_x = 25; double shift_y = 25; os << "<svg viewBox='" << x0 << " " << y0 << " " << x1 << " " << y1 << "' xmlns='http://www.w3.org/2000/svg'>" << endl; os << "<rect width='100%' height='100%' fill='black'/>" << endl; FOR(m, n) { os << "<line x1='" << ((long double)mags[m].towers[0].pos[0] + shift_x) * scale << "' y1='" << ((long double)mags[m].towers[0].pos[1] + shift_y) * scale << "' x2='" << ((long double)mags[m].towers[1].pos[0] + shift_x) * scale << "' y2='" << ((long double)mags[m].towers[1].pos[1] + shift_y) * scale << "' style='stroke:rgb(255,255,255);stroke-width:2' />" << endl; } for(auto& s : debug_segments) { os << "<line x1='" << ((long double)s.fr.pos[0] + shift_x) * scale << "' y1='" << ((long double)s.fr.pos[1] + shift_y) * scale << "' x2='" << ((long double)s.to.pos[0] + shift_x) * scale << "' y2='" << ((long double)s.to.pos[1] + shift_y) * scale << "' style='stroke:rgb(0,255,0);stroke-width:1' />" << endl; } os << "<polygon points='"; for(auto& p : poly.pts) os << ((long double)p[0]+shift_x) * scale << "," << ((long double)p[1]+shift_y)*scale << " "; os << "' style='fill:none;stroke:red;stroke-width:2' />" << endl; os << "</svg>" << endl; } */ void solve() { // cout << "Case #" << icase+1 << ": "; mags.resize(n); FOR(i, n) { mags[i].towers[0].pos[0] = RI; mags[i].towers[0].pos[1] = RI; mags[i].towers[1].pos[0] = RI; mags[i].towers[1].pos[1] = RI; } vector<Vec> dirs; FOR(i, n) { FO(j, i+1, n) { FOR(aa,2) FOR(bb,2) { auto seg = Segment{ mags[i].towers[aa], mags[j].towers[bb] }; auto dir = seg.to - seg.fr; swap(dir[0], dir[1]); dir[0] = -dir[0]; dirs.push_back( dir ); dirs.push_back( -dir ); } } } // sort dirs { vector<Vec> dirs_by_half[2]; auto is_in_lower = [](const Vec& v) { if(v[1] != 0) return v[1] < 0; return v[0] < 0; }; for(auto& d : dirs) { dirs_by_half[ is_in_lower(d) ].push_back( d ); } FOR(i,2) sort(ALL(dirs_by_half[i]), [](const Vec& a, const Vec& b) { return det(a,b) > 0; }); dirs.clear(); FOR(i,2) dirs.insert(dirs.end(), ALL(dirs_by_half[i])); } //cerr << "dirs: " << endl; //for(auto& d : dirs) cerr << " " << d << endl; //cerr << " (" << dirs.size() << " dirs total)" << endl; /* { LL max_abs = 0; for(auto& d : dirs) { REMAX(max_abs, d[0].max_abs()); REMAX(max_abs, d[1].max_abs()); } cerr << "dirs max abs: " << max_abs << endl; // 3 digits } */ poly.push_back({-512, -512}); poly.push_back({ 512, -512}); poly.push_back({ 512, 512}); poly.push_back({-512, 512}); //FOR(i,n) FO(j,i+1,n) FOR(k,n) FO(l,k+1,n) { // cerr << "test " << cut(Segment{mags[i].towers[0], mags[j].towers[0]}, Segment{mags[k].towers[0], mags[l].towers[0]}) << endl; //} int cvert = 0; for(auto& d : dirs) { F best_dot = INT_MAX; FOR(m,n) { F cand = max(d * mags[m].towers[0], d * mags[m].towers[1]); REMIN(best_dot, cand); } //cerr << "best_dot is " << best_dot << endl; // 6 digits, integer // poly_vert_coords * dot -> 6+6=12 digits // update cvert while((poly[cvert+1] - poly[cvert]) * d < 0) ++cvert; // 6+18 digits! while((poly[cvert-1] - poly[cvert]) * d < 0) --cvert; if(d * poly[cvert] >= best_dot) continue; // find range Range range = {cvert, cvert}; while(d * poly[range.fr-1] < best_dot) { --range.fr; if(poly.fix_range(range.fr) == poly.fix_range(range.to)) { cout << 0 << endl; return; } } while(d * poly[range.to+1] < best_dot) ++range.to; //cerr << " range is " << range << endl; auto weight_by_dot = [](const Vec& close, const Vec& far, const Vec& dd, F target_dot) { auto close_dot = close * dd; auto far_dot = far * dd; if(close_dot == far_dot) { assert(target_dot == close_dot); return close; // close is priority } F alpha = (target_dot - close_dot) / (far_dot - close_dot); return close + alpha * (far - close); }; array<Vec,2> v = { weight_by_dot(poly[range.fr], poly[range.fr-1], d, best_dot), weight_by_dot(poly[range.to], poly[range.to+1], d, best_dot) }; //cerr << endl; //cerr << "new point v0 " << v[0] << endl; //cerr << "new point v1 " << v[1] << endl; cvert = poly.remove_range(range); poly.pts.insert(poly.pts.begin() + cvert, ALL(v)); } cout.precision(500); cout << poly.area() << endl; //debug_svg(); } int icase = -1; }; // struct Test_Case int main() { int num_cases = READ_NUM_TEST_CASES ? RI : 1; FOR(i, num_cases) { Test_Case tc; tc.icase = i; tc.solve(); } return 0; } |