#include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Point { int x, y; bool operator<(const Point& oth) const { if (x != oth.x) { return x < oth.x; } return y < oth.y; } friend ostream& operator<<(ostream& out, Point p); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } bool TestWithin(vector<Point>& line, Point p) { bool wind = 0; for (int i = 0; i < SZ(line); i += 2) { int mi = min(line[i].x, line[i + 1].x); int ma = max(line[i].x, line[i + 1].x); if (mi <= p.x && p.x < ma && p.y < line[i].y) { wind ^= 1; } } return wind; } int DisPtPt(Point& p1, Point p2) { return max(abs(p1.x - p2.x), abs(p1.y - p2.y)); } inline bool Between(int a, int b, int c) { return (b <= a && a <= c) || (c <= a && a <= b); } const int kInf = 1e9; //int DisPtSeg(Point& p, Point& q1, int DisLines(vector<Point>& l1, vector<Point>& l2) { int res = kInf; for (auto& p1 : l1) { for (auto& p2 : l2) { mini(res, DisPtPt(p1, p2) - 1); } } REP (tr, 2) { for (int i = 0; i < SZ(l1); i++) { for (auto& p2 : l2) { if (i % 2 == 0) { if (Between(p2.x, l1[i].x, l1[i + 1].x)) { mini(res, abs(p2.y - l1[i].y) - 1); } } else { if (Between(p2.y, l1[i].y, l1[(i + 1) % SZ(l1)].y)) { mini(res, abs(p2.x - l1[i].x) - 1); } } } } swap(l1, l2); } assert(res >= 0); return res; } struct Rec { int x1, y1, x2, y2, val; friend ostream& operator<<(ostream& out, Rec r); }; ostream& operator<<(ostream& out, Rec r) { return out<<"("<<r.x1<<" - "<<r.x2<<") x ("<<r.y1<<" - "<<r.y2<<"), v="<<r.val; } struct Seg { int x, y1, y2, val; bool operator<(const Seg& oth) const { if (x != oth.x) { return x < oth.x; } return val < oth.val; } }; struct Node1 { int m, to_push; }; struct Tree { vector<Node1> nodes; int M; Tree(int MM) { M = MM; nodes.resize(2 * M); } void Add(int a, int b, int val) { AddRec(a, b, 1, M, 1, val); } void Touch(int v) { assert(v < M); FOR (ch, 2 * v, 2 * v + 1) { nodes[ch].m += nodes[v].to_push; nodes[ch].to_push += nodes[v].to_push; } nodes[v].to_push = 0; } void AddRec(int a, int b, int bl, int br, int ind, int val) { if (b < bl || a > br) { return; } if (a <= bl && br <= b) { nodes[ind].m += val; nodes[ind].to_push += val; return; } int m = (bl + br) / 2; Touch(ind); AddRec(a, b, bl, m, 2 * ind, val); AddRec(a, b, m + 1, br, 2 * ind + 1, val); nodes[ind].m = max(nodes[2 * ind].m, nodes[2 * ind + 1].m); } }; int dbg; bool AnyFree(vector<Rec>& recs) { map<int, int> /*sx,*/ sy; for (auto& rec : recs) { //sx[rec.x1] = 1; //sx[rec.x2] = 1; // if (rec.y1 >= rec.y2) { // debug(rec.x1, rec.y1, rec.x2, rec.y2, rec.val); // } assert(rec.x1 < rec.x2 && rec.y1 < rec.y2); sy[rec.y1] = 1; sy[rec.y2 - 1] = 1; sy[rec.y2] = 1; } int cnt = 0; // for (auto& p : sx) { // cnt++; // p.nd = cnt; // } cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } vector<Seg> segs; for (auto& rec : recs) { segs.PB({rec.x1, sy[rec.y1], sy[rec.y2 - 1], rec.val}); segs.PB({rec.x2, sy[rec.y1], sy[rec.y2 - 1], -rec.val}); } sort(ALL(segs)); int M = 1; while (M <= cnt) { M = 2 * M; } Tree tree(M); for (auto seg : segs) { // if (dbg) { // debug(seg.y1, seg.y2, seg.val); // } tree.Add(seg.y1, seg.y2, seg.val); if (tree.nodes[1].m > 0) { return true; } } return false; } Point Rot(Point p) { return {-p.y, p.x}; } // struct Node2 { // array<PII, 2> cur; // } typedef array<PII, 2> Node2; struct Tree2 { int M; vector<Node2> nodes; Tree2(int n) { M = 1; while (M <= n) { M *= 2; } Node2 dummy; dummy[0] = dummy[1] = {-kInf, -1}; nodes = vector<Node2>(2 * M, dummy); } void Add(int a, int b, int v, int t) { AddRec(a, b, 1, M, v, t, 1); } void AddRec(int a, int b, int bl, int br, int val, int typ, int ind) { if (b < bl || a > br) { return; } if (a <= bl && br <= b) { Node2& n = nodes[ind]; if (n[0].nd == typ) { maxi(n[0].st, val); } else if (n[1].nd == typ) { maxi(n[1].st, val); } else { if (val > n[1].st) { n[1] = {val, typ}; } } if (n[1].st > n[0].st) { swap(n[0], n[1]); } return; } int m = (bl + br) / 2; AddRec(a, b, bl, m, val, typ, 2 * ind); AddRec(a, b, m + 1, br, val, typ, 2 * ind + 1); } PII Read(int a, int typ) { a += M - 1; PII res{-kInf, -1}; while (a) { if (nodes[a][0].nd == typ) { maxi(res, nodes[a][1]); } else { maxi(res, nodes[a][0]); } a /= 2; } return res; //return ReadRec(a, 1, M, t, 1); } // PII ReadRec(int a, int bl, int br, int typ, int ind) { // if (b < bl || a > br) { return; } // if (a <= bl && br <= b) { // if (node[ind][0].nd == typ) { // return node[ind][1]; // } else { // return node[ind][0]; // } // } // int m = (bl + br) / 2; // PII r1 = ReadRec(a, bl, m, typ, 2 * ind); // PII r2 = ReadRec(a, m + 1, br, typ, 2 * ind + 1); // return max(r1, r2); // } }; int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int n; cin>>n; vector<pair<int, vector<Point>>> lines_areas; vector<vector<Point>> lines; RE (_, n) { int k; cin>>k; VI x; RE (i, k) { int a; cin>>a; x.PB(a); } vector<Point> line; int area = 0; REP (i, k) { if (i % 2 == 0) { line.PB({x[i], x[i + 1]}); } else { line.PB({x[(i + 1) % k], x[i]}); } } for (int i = 0; i < k; i += 2) { int y = line[i].y; int dx = line[i + 1].x - line[i].x; area += y * dx; } // debug(area); lines_areas.PB({area, line}); } sort(ALL(lines_areas)); //reverse(ALL(lines_areas)); for (auto p : lines_areas) { lines.PB(p.nd); } for (auto line : lines) { debug(line); } //debug(lines); VI parent(n); VVI children(n); VVI dis(n); RE (me, n - 1) { FORD (cand, me - 1, 0) { if (TestWithin(lines[cand], lines[me][0])) { parent[me] = cand; children[cand].PB(me); cerr<<me<<" -> "<<cand<<endl; break; } } } debug(parent); VI res(n); REP (v, n) { auto& C = children[v]; int sz = SZ(C); dis[v].resize(sz); #define CHKa #ifdef CHK vector<bool> taken(sz); REP (i, sz) { dis[v][i] = DisLines(lines[v], lines[C[i]]); taken[i] = 0; } REP (phase, sz) { int who = -1; REP (i, sz) { if (taken[i]) { continue; } if (who == -1) { who = i; } else if (dis[v][i] < dis[v][who]) { who = i; } } taken[who] = 1; REP (i, sz) { if (taken[i]) { continue; } mini(dis[v][i], dis[v][who] + DisLines(lines[C[i]], lines[C[who]])); } } REP (i, sz) { cerr<<v<<" ---"<<dis[v][i]<<"---> "<<C[i]<<endl; } #endif vector<VPII> slo(SZ(C) + 1); REP (tr, 4) { // begin odcinki map<int, int> sy; for (auto& p : lines[v]) { sy[p.y] = 1; } for (int ch : C) { for (auto& p : lines[ch]) { sy[p.y] = 1; } } int cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } vector<Seg> segs; int st = (tr + 1) % 2; for (int i = st; i < SZ(lines[v]); i += 2) { int j = (i + 1) % SZ(lines[v]); int y1 = sy[lines[v][i].y], y2 = sy[lines[v][j].y]; segs.PB({lines[v][i].x, min(y1, y2), max(y1, y2), SZ(C)}); } REP (ii, SZ(C)) { int ch = C[ii]; for (int i = st; i < SZ(lines[ch]); i += 2) { int j = (i + 1) % SZ(lines[ch]); int y1 = sy[lines[ch][i].y], y2 = sy[lines[ch][j].y]; segs.PB({lines[ch][i].x, min(y1, y2), max(y1, y2), ii}); } } sort(ALL(segs)); auto PutEdge = [&](int a, int b, int c) { // if (c < 0) { // debug(a, b, c); // assert(false); // } slo[a].PB({b, c}); slo[b].PB({a, c}); }; Tree2 tree1(cnt + 2); for (auto& seg : segs) { PII r1 = tree1.Read(seg.y1, seg.val); PII r2 = tree1.Read(seg.y2, seg.val); if (r1.nd != -1) { PutEdge(seg.val, r1.nd, seg.x - r1.st - 1); } if (r2.nd != -1) { PutEdge(seg.val, r2.nd, seg.x - r2.st - 1); } tree1.Add(seg.y1, seg.y2, seg.x, seg.val); } // end odcinki // debug("skonczylem odcinki"); // begin punkty if (tr < 2) { // TODO chyba wystarcza tylko 2 iteracje vector<pair<Point, int>> pts; for (auto& p : lines[v]) { pts.PB({p, SZ(C)}); } REP (ii, SZ(C)) { int ch = C[ii]; for (auto& p : lines[ch]) { pts.PB({p, ii}); } } sy.clear(); for (auto& p : pts) { p.st = {p.st.x - p.st.y, p.st.x + p.st.y}; // p.st.y = (p.y - p.x) / 2, p.x = (p.x + p.y) / 2 sy[p.st.y] = 1; } cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } Tree2 tree2(cnt + 2); sort(ALL(pts)); for (auto& pa : pts) { Point& pt = pa.st; PII r = tree2.Read(sy[pt.y], pa.nd); int X = (pt.x + pt.y) / 2; if (r.nd != -1) { PutEdge(pa.nd, r.nd, X - r.st - 1); } //cerr<<"Put "<<X<<" on ["<<sy[pt.y]<<", "<<cnt<<"]"<<endl; tree2.Add(sy[pt.y], cnt, X, pa.nd); } // end punkty } for (auto& p : lines[v]) { p = Rot(p); } for (int ch : C) { for (auto& p : lines[ch]) { p = Rot(p); } } } // REP (ii, SZ(C) + 1) { // cerr<<"krawedzie od "<<ii<<endl; // for (auto e : slo[ii]) { // debug(e); // } // } VI dis2(SZ(C), kInf); set<PII> que; for (auto e : slo[SZ(C)]) { mini(dis2[e.st], e.nd); } REP (i, SZ(C)) { que.insert({dis2[i], i}); } while (SZ(que)) { auto it = que.begin(); auto p = *it; // debug(p); que.erase(it); int cur = p.nd; for (auto e : slo[cur]) { int nei = e.st; if (nei == SZ(C)) { continue; } int cand = p.st + e.nd; if (cand < dis2[nei]) { que.erase({dis2[nei], nei}); dis2[nei] = cand; que.insert({dis2[nei], nei}); } } } #ifdef CHK debug(dis[v]); debug(dis2); REP (i, SZ(dis[v])) { if (dis[v][i] != dis2[i]) { int ch = children[v][i]; debug(v, ch, i); debug(lines[v]); debug(lines[ch]); int mix = lines[v][0].x, mx = lines[v][0].x, miy = lines[v][0].y, may = lines[v][0].y; for (auto& p : lines[v]) { mini(mix, p.x); mini(miy, p.y); maxi(mx, p.x); maxi(may, p.y); } debug(mix, mx, miy, may); } } assert(dis[v] == dis2); #else dis[v] = dis2; #endif int kl = 1, kp = 1e8 + 5; while (kl <= kp) { int aktc = (kl + kp) / 2; vector<Rec> recs; for (int i = 0; i < SZ(lines[v]); i += 2) { recs.PB({lines[v][i].x, -kInf, lines[v][i + 1].x, lines[v][i].y, -1}); } for (auto ch : children[v]) { for (int i = 0; i < SZ(lines[ch]); i += 2) { recs.PB({lines[ch][i].x, -kInf, lines[ch][i + 1].x, lines[ch][i].y, 1}); } } auto PutForbRec = [&](Point& p1, Point& p2, int L) { if (p1.x == p2.x) { recs.PB({p1.x - L, min(p1.y, p2.y) - L, p1.x + L, max(p1.y, p2.y) + L, -1}); } else { recs.PB({min(p1.x, p2.x) - L, p1.y - L, max(p1.x, p2.x) + L, p1.y + L, -1}); } }; for (int i = 0; i < SZ(lines[v]); i++) { PutForbRec(lines[v][i], lines[v][(i + 1) % SZ(lines[v])], aktc); } REP (ii, SZ(children[v])) { int ch = children[v][ii]; if (aktc - dis[v][ii] > 0) { for (int i = 0; i < SZ(lines[ch]); i++) { PutForbRec(lines[ch][i], lines[ch][(i + 1) % SZ(lines[ch])], aktc - dis[v][ii]); } } } for (auto& rec : recs) { if (rec.x1 > rec.x2) { swap(rec.x1, rec.x2); rec.val *= -1; } } // if (v == 2 && aktc == 1) { // // for (auto r : recs) { // // debug(r); // // } // dbg = 1; // } if (AnyFree(recs)) { res[v] = aktc; kl = aktc + 1; } else { kp = aktc - 1; } if (v == 2 && aktc == 1) { dbg = 0; } } } debug(res); FORD (i, n - 1, 0) { REP (j, SZ(children[i])) { maxi(res[i], res[children[i][j]] + dis[i][j]); } res[i]++; } cout<<res[0]<<endl; 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 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Point { int x, y; bool operator<(const Point& oth) const { if (x != oth.x) { return x < oth.x; } return y < oth.y; } friend ostream& operator<<(ostream& out, Point p); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } bool TestWithin(vector<Point>& line, Point p) { bool wind = 0; for (int i = 0; i < SZ(line); i += 2) { int mi = min(line[i].x, line[i + 1].x); int ma = max(line[i].x, line[i + 1].x); if (mi <= p.x && p.x < ma && p.y < line[i].y) { wind ^= 1; } } return wind; } int DisPtPt(Point& p1, Point p2) { return max(abs(p1.x - p2.x), abs(p1.y - p2.y)); } inline bool Between(int a, int b, int c) { return (b <= a && a <= c) || (c <= a && a <= b); } const int kInf = 1e9; //int DisPtSeg(Point& p, Point& q1, int DisLines(vector<Point>& l1, vector<Point>& l2) { int res = kInf; for (auto& p1 : l1) { for (auto& p2 : l2) { mini(res, DisPtPt(p1, p2) - 1); } } REP (tr, 2) { for (int i = 0; i < SZ(l1); i++) { for (auto& p2 : l2) { if (i % 2 == 0) { if (Between(p2.x, l1[i].x, l1[i + 1].x)) { mini(res, abs(p2.y - l1[i].y) - 1); } } else { if (Between(p2.y, l1[i].y, l1[(i + 1) % SZ(l1)].y)) { mini(res, abs(p2.x - l1[i].x) - 1); } } } } swap(l1, l2); } assert(res >= 0); return res; } struct Rec { int x1, y1, x2, y2, val; friend ostream& operator<<(ostream& out, Rec r); }; ostream& operator<<(ostream& out, Rec r) { return out<<"("<<r.x1<<" - "<<r.x2<<") x ("<<r.y1<<" - "<<r.y2<<"), v="<<r.val; } struct Seg { int x, y1, y2, val; bool operator<(const Seg& oth) const { if (x != oth.x) { return x < oth.x; } return val < oth.val; } }; struct Node1 { int m, to_push; }; struct Tree { vector<Node1> nodes; int M; Tree(int MM) { M = MM; nodes.resize(2 * M); } void Add(int a, int b, int val) { AddRec(a, b, 1, M, 1, val); } void Touch(int v) { assert(v < M); FOR (ch, 2 * v, 2 * v + 1) { nodes[ch].m += nodes[v].to_push; nodes[ch].to_push += nodes[v].to_push; } nodes[v].to_push = 0; } void AddRec(int a, int b, int bl, int br, int ind, int val) { if (b < bl || a > br) { return; } if (a <= bl && br <= b) { nodes[ind].m += val; nodes[ind].to_push += val; return; } int m = (bl + br) / 2; Touch(ind); AddRec(a, b, bl, m, 2 * ind, val); AddRec(a, b, m + 1, br, 2 * ind + 1, val); nodes[ind].m = max(nodes[2 * ind].m, nodes[2 * ind + 1].m); } }; int dbg; bool AnyFree(vector<Rec>& recs) { map<int, int> /*sx,*/ sy; for (auto& rec : recs) { //sx[rec.x1] = 1; //sx[rec.x2] = 1; // if (rec.y1 >= rec.y2) { // debug(rec.x1, rec.y1, rec.x2, rec.y2, rec.val); // } assert(rec.x1 < rec.x2 && rec.y1 < rec.y2); sy[rec.y1] = 1; sy[rec.y2 - 1] = 1; sy[rec.y2] = 1; } int cnt = 0; // for (auto& p : sx) { // cnt++; // p.nd = cnt; // } cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } vector<Seg> segs; for (auto& rec : recs) { segs.PB({rec.x1, sy[rec.y1], sy[rec.y2 - 1], rec.val}); segs.PB({rec.x2, sy[rec.y1], sy[rec.y2 - 1], -rec.val}); } sort(ALL(segs)); int M = 1; while (M <= cnt) { M = 2 * M; } Tree tree(M); for (auto seg : segs) { // if (dbg) { // debug(seg.y1, seg.y2, seg.val); // } tree.Add(seg.y1, seg.y2, seg.val); if (tree.nodes[1].m > 0) { return true; } } return false; } Point Rot(Point p) { return {-p.y, p.x}; } // struct Node2 { // array<PII, 2> cur; // } typedef array<PII, 2> Node2; struct Tree2 { int M; vector<Node2> nodes; Tree2(int n) { M = 1; while (M <= n) { M *= 2; } Node2 dummy; dummy[0] = dummy[1] = {-kInf, -1}; nodes = vector<Node2>(2 * M, dummy); } void Add(int a, int b, int v, int t) { AddRec(a, b, 1, M, v, t, 1); } void AddRec(int a, int b, int bl, int br, int val, int typ, int ind) { if (b < bl || a > br) { return; } if (a <= bl && br <= b) { Node2& n = nodes[ind]; if (n[0].nd == typ) { maxi(n[0].st, val); } else if (n[1].nd == typ) { maxi(n[1].st, val); } else { if (val > n[1].st) { n[1] = {val, typ}; } } if (n[1].st > n[0].st) { swap(n[0], n[1]); } return; } int m = (bl + br) / 2; AddRec(a, b, bl, m, val, typ, 2 * ind); AddRec(a, b, m + 1, br, val, typ, 2 * ind + 1); } PII Read(int a, int typ) { a += M - 1; PII res{-kInf, -1}; while (a) { if (nodes[a][0].nd == typ) { maxi(res, nodes[a][1]); } else { maxi(res, nodes[a][0]); } a /= 2; } return res; //return ReadRec(a, 1, M, t, 1); } // PII ReadRec(int a, int bl, int br, int typ, int ind) { // if (b < bl || a > br) { return; } // if (a <= bl && br <= b) { // if (node[ind][0].nd == typ) { // return node[ind][1]; // } else { // return node[ind][0]; // } // } // int m = (bl + br) / 2; // PII r1 = ReadRec(a, bl, m, typ, 2 * ind); // PII r2 = ReadRec(a, m + 1, br, typ, 2 * ind + 1); // return max(r1, r2); // } }; int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int n; cin>>n; vector<pair<int, vector<Point>>> lines_areas; vector<vector<Point>> lines; RE (_, n) { int k; cin>>k; VI x; RE (i, k) { int a; cin>>a; x.PB(a); } vector<Point> line; int area = 0; REP (i, k) { if (i % 2 == 0) { line.PB({x[i], x[i + 1]}); } else { line.PB({x[(i + 1) % k], x[i]}); } } for (int i = 0; i < k; i += 2) { int y = line[i].y; int dx = line[i + 1].x - line[i].x; area += y * dx; } // debug(area); lines_areas.PB({area, line}); } sort(ALL(lines_areas)); //reverse(ALL(lines_areas)); for (auto p : lines_areas) { lines.PB(p.nd); } for (auto line : lines) { debug(line); } //debug(lines); VI parent(n); VVI children(n); VVI dis(n); RE (me, n - 1) { FORD (cand, me - 1, 0) { if (TestWithin(lines[cand], lines[me][0])) { parent[me] = cand; children[cand].PB(me); cerr<<me<<" -> "<<cand<<endl; break; } } } debug(parent); VI res(n); REP (v, n) { auto& C = children[v]; int sz = SZ(C); dis[v].resize(sz); #define CHKa #ifdef CHK vector<bool> taken(sz); REP (i, sz) { dis[v][i] = DisLines(lines[v], lines[C[i]]); taken[i] = 0; } REP (phase, sz) { int who = -1; REP (i, sz) { if (taken[i]) { continue; } if (who == -1) { who = i; } else if (dis[v][i] < dis[v][who]) { who = i; } } taken[who] = 1; REP (i, sz) { if (taken[i]) { continue; } mini(dis[v][i], dis[v][who] + DisLines(lines[C[i]], lines[C[who]])); } } REP (i, sz) { cerr<<v<<" ---"<<dis[v][i]<<"---> "<<C[i]<<endl; } #endif vector<VPII> slo(SZ(C) + 1); REP (tr, 4) { // begin odcinki map<int, int> sy; for (auto& p : lines[v]) { sy[p.y] = 1; } for (int ch : C) { for (auto& p : lines[ch]) { sy[p.y] = 1; } } int cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } vector<Seg> segs; int st = (tr + 1) % 2; for (int i = st; i < SZ(lines[v]); i += 2) { int j = (i + 1) % SZ(lines[v]); int y1 = sy[lines[v][i].y], y2 = sy[lines[v][j].y]; segs.PB({lines[v][i].x, min(y1, y2), max(y1, y2), SZ(C)}); } REP (ii, SZ(C)) { int ch = C[ii]; for (int i = st; i < SZ(lines[ch]); i += 2) { int j = (i + 1) % SZ(lines[ch]); int y1 = sy[lines[ch][i].y], y2 = sy[lines[ch][j].y]; segs.PB({lines[ch][i].x, min(y1, y2), max(y1, y2), ii}); } } sort(ALL(segs)); auto PutEdge = [&](int a, int b, int c) { // if (c < 0) { // debug(a, b, c); // assert(false); // } slo[a].PB({b, c}); slo[b].PB({a, c}); }; Tree2 tree1(cnt + 2); for (auto& seg : segs) { PII r1 = tree1.Read(seg.y1, seg.val); PII r2 = tree1.Read(seg.y2, seg.val); if (r1.nd != -1) { PutEdge(seg.val, r1.nd, seg.x - r1.st - 1); } if (r2.nd != -1) { PutEdge(seg.val, r2.nd, seg.x - r2.st - 1); } tree1.Add(seg.y1, seg.y2, seg.x, seg.val); } // end odcinki // debug("skonczylem odcinki"); // begin punkty if (tr < 2) { // TODO chyba wystarcza tylko 2 iteracje vector<pair<Point, int>> pts; for (auto& p : lines[v]) { pts.PB({p, SZ(C)}); } REP (ii, SZ(C)) { int ch = C[ii]; for (auto& p : lines[ch]) { pts.PB({p, ii}); } } sy.clear(); for (auto& p : pts) { p.st = {p.st.x - p.st.y, p.st.x + p.st.y}; // p.st.y = (p.y - p.x) / 2, p.x = (p.x + p.y) / 2 sy[p.st.y] = 1; } cnt = 0; for (auto& p : sy) { cnt++; p.nd = cnt; } Tree2 tree2(cnt + 2); sort(ALL(pts)); for (auto& pa : pts) { Point& pt = pa.st; PII r = tree2.Read(sy[pt.y], pa.nd); int X = (pt.x + pt.y) / 2; if (r.nd != -1) { PutEdge(pa.nd, r.nd, X - r.st - 1); } //cerr<<"Put "<<X<<" on ["<<sy[pt.y]<<", "<<cnt<<"]"<<endl; tree2.Add(sy[pt.y], cnt, X, pa.nd); } // end punkty } for (auto& p : lines[v]) { p = Rot(p); } for (int ch : C) { for (auto& p : lines[ch]) { p = Rot(p); } } } // REP (ii, SZ(C) + 1) { // cerr<<"krawedzie od "<<ii<<endl; // for (auto e : slo[ii]) { // debug(e); // } // } VI dis2(SZ(C), kInf); set<PII> que; for (auto e : slo[SZ(C)]) { mini(dis2[e.st], e.nd); } REP (i, SZ(C)) { que.insert({dis2[i], i}); } while (SZ(que)) { auto it = que.begin(); auto p = *it; // debug(p); que.erase(it); int cur = p.nd; for (auto e : slo[cur]) { int nei = e.st; if (nei == SZ(C)) { continue; } int cand = p.st + e.nd; if (cand < dis2[nei]) { que.erase({dis2[nei], nei}); dis2[nei] = cand; que.insert({dis2[nei], nei}); } } } #ifdef CHK debug(dis[v]); debug(dis2); REP (i, SZ(dis[v])) { if (dis[v][i] != dis2[i]) { int ch = children[v][i]; debug(v, ch, i); debug(lines[v]); debug(lines[ch]); int mix = lines[v][0].x, mx = lines[v][0].x, miy = lines[v][0].y, may = lines[v][0].y; for (auto& p : lines[v]) { mini(mix, p.x); mini(miy, p.y); maxi(mx, p.x); maxi(may, p.y); } debug(mix, mx, miy, may); } } assert(dis[v] == dis2); #else dis[v] = dis2; #endif int kl = 1, kp = 1e8 + 5; while (kl <= kp) { int aktc = (kl + kp) / 2; vector<Rec> recs; for (int i = 0; i < SZ(lines[v]); i += 2) { recs.PB({lines[v][i].x, -kInf, lines[v][i + 1].x, lines[v][i].y, -1}); } for (auto ch : children[v]) { for (int i = 0; i < SZ(lines[ch]); i += 2) { recs.PB({lines[ch][i].x, -kInf, lines[ch][i + 1].x, lines[ch][i].y, 1}); } } auto PutForbRec = [&](Point& p1, Point& p2, int L) { if (p1.x == p2.x) { recs.PB({p1.x - L, min(p1.y, p2.y) - L, p1.x + L, max(p1.y, p2.y) + L, -1}); } else { recs.PB({min(p1.x, p2.x) - L, p1.y - L, max(p1.x, p2.x) + L, p1.y + L, -1}); } }; for (int i = 0; i < SZ(lines[v]); i++) { PutForbRec(lines[v][i], lines[v][(i + 1) % SZ(lines[v])], aktc); } REP (ii, SZ(children[v])) { int ch = children[v][ii]; if (aktc - dis[v][ii] > 0) { for (int i = 0; i < SZ(lines[ch]); i++) { PutForbRec(lines[ch][i], lines[ch][(i + 1) % SZ(lines[ch])], aktc - dis[v][ii]); } } } for (auto& rec : recs) { if (rec.x1 > rec.x2) { swap(rec.x1, rec.x2); rec.val *= -1; } } // if (v == 2 && aktc == 1) { // // for (auto r : recs) { // // debug(r); // // } // dbg = 1; // } if (AnyFree(recs)) { res[v] = aktc; kl = aktc + 1; } else { kp = aktc - 1; } if (v == 2 && aktc == 1) { dbg = 0; } } } debug(res); FORD (i, n - 1, 0) { REP (j, SZ(children[i])) { maxi(res[i], res[children[i][j]] + dis[i][j]); } res[i]++; } cout<<res[0]<<endl; return 0; } |