#include <bits/stdc++.h> #include "palindromy.h" #include "message.h" using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) 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 TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.st << ", " << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int SendAfter = 63990; struct Sender { enum FieldType { FLD_CHAR, FLD_INT, FLD_LL }; struct field { FieldType type; union { char ch; int n; LL l; }; }; struct Submitter {}; vector<field> toSend; int dest; int cap; Sender() : dest(-10000), cap(SendAfter) {} Sender(int dst){ dest = dst; cap = SendAfter; toSend.clear(); } ~Sender(){ if(toSend.size() > 0) submit(); } int capacity() const { return cap; } void capacity(int newCapacity) { cap = newCapacity; } void submit(){ LL msgsize = 4; if(toSend.size() == 0) return; PutInt(dest, (int)toSend.size()); for(auto& f : toSend){ switch(f.type){ case FLD_CHAR: PutChar(dest, f.ch); msgsize++; break; case FLD_INT: PutInt(dest, f.n); msgsize += 4; break; case FLD_LL: PutLL(dest, f.l); msgsize += 8; break; default: assert(false); } } Send(dest); toSend.clear(); } void add_field(field f){ assert(dest >= 0); toSend.push_back(f); if(toSend.size() >= cap) submit(); } friend Sender& operator<<(Sender& snd, char ch){ field f; f.type = FLD_CHAR; f.ch = ch; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, int n){ field f; f.type = FLD_INT; f.n = n; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, LL l){ field f; f.type = FLD_LL; f.l = l; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, Sender::Submitter){ snd.submit(); return snd; } }; Sender::Submitter submit; struct Receiver { int numEnqueued; int node; Receiver() : node(-10000) {} Receiver(int nod) : node(nod), numEnqueued(0) {} void try_fetch(){ assert(node >= -1); assert(numEnqueued >= 0); if(numEnqueued > 0){ numEnqueued--; return; } Receive(node); numEnqueued = GetInt(node); numEnqueued--; } char read_char(){ try_fetch(); return GetChar(node); } int read_int(){ try_fetch(); return GetInt(node); } LL read_ll(){ try_fetch(); return GetLL(node); } friend Receiver& operator>>(Receiver& recv, char& ch){ ch = recv.read_char(); return recv; } friend Receiver& operator>>(Receiver& recv, int& n){ n = recv.read_int(); return recv; } friend Receiver& operator>>(Receiver& recv, LL& l){ l = recv.read_ll(); return recv; } }; template<typename T> Sender& operator<<(Sender& snd, const vector<T>& vec){ snd << (int)vec.size(); for(T v : vec) snd << v; return snd; } template<typename T> Receiver& operator>>(Receiver& recv, vector<T>& vec){ int sz; recv >> sz; vec = vector<T>(sz); for(int i = 0; i < sz; i++){ recv >> vec[i]; } return recv; } LL MyNode, TotalNodes; vector<Sender> senders; vector<Receiver> receivers; void init(){ MyNode = MyNodeId(); TotalNodes = NumberOfNodes(); for(LL i = 0; i < TotalNodes; i++){ senders.emplace_back(i); receivers.emplace_back(i); } } /////////////////////////////////////////////////////////////////////////// struct Manacher { // IGNORES FIRST LETTER - in[0] must be space // par[i] = k <=> [i - k + 1, i + k] is maximal palindrome // npar[i] = k <=> [i - k, i + k] is maximal palindrome VI par, npar; Manacher(string in) { assert(in[0] == ' '); int orig_n = SZ(in) - 1; string s = " #"; for (int i = 1; i <= orig_n; i++) { s += in[i]; s += '#'; } s += '$'; int new_n = SZ(s) - 2; npar.resize(new_n + 2); int furth_beg = 0; int furth_end = 0; for (int i = 1; i <= new_n; i++) { if (furth_end < i) { furth_beg = i; furth_end = i; } int corr_npar = furth_beg + furth_end - i; if (furth_end > i + npar[corr_npar]) { npar[i] = npar[corr_npar]; } else { npar[i] = furth_end - i; furth_beg = i - npar[i]; while (s[furth_beg - 1] == s[furth_end + 1]) { furth_beg--; furth_end++; npar[i]++; } } } par.resize(orig_n + 2); for (int i = 1; i <= orig_n; i++) { if (i < orig_n) { par[i] = npar[2 * i + 1] / 2; } npar[i] = npar[2 * i] / 2; } npar.resize(orig_n + 2); } }; const int Mod = 1e9 + 7, Base = 129847121; vector<int> pwrsBase, invPwrsBase; int powMod(int a, int n) { int res = 1; while (n) { if (n & 1) { res = ((LL)res * a) % Mod; } n >>= 1; a = ((LL)a * a) % Mod; } return res; } struct Hasher { vector<int> hashes; int size; template <typename Iter> Hasher(Iter first, Iter last) { size = distance(first, last); hashes.resize(size + 1); for (int i = 0; i < size; i++) { LL coef = (LL)pwrsBase[i] * (*first); hashes[i + 1] = (hashes[i] + coef) % Mod; first++; } } int getHash(int left, int right) { assert(left <= right); int res = hashes[right] - hashes[left]; if (res < 0) { res += Mod; } return ((LL)res * invPwrsBase[left]) % Mod; } }; int N; int maxRad; int from, to, size; string bucket; vector<int> palisEven, palisOdd; vector<int> nodesFrom; Hasher *hashLR, *hashRL; int seqBound; int getBucket(int pos) { int res = 0; while (res < TotalNodes && nodesFrom[res + 1] <= pos) { res++; } return res; } void preprocPalis() { int leftRad = min(from, maxRad + 1), rightRad = min(N - to, maxRad + 1); int left = from - leftRad, right = to + rightRad; string forPali = " "; for (int i = left; i < right; i++) { char ch = GetLetter(i); forPali += ch; if (i >= from && i < to) { bucket += ch; } } Manacher manacher(forPali); size = to - from; palisEven.resize(size); palisOdd.resize(size); //debug(from, to, left, right); int ptrOdd = leftRad + 1, ptrEven = leftRad + 1; for (int i = 0; i < size; i++) { palisOdd[i] = manacher.npar[ptrOdd++]; palisEven[i] = manacher.par[ptrEven++]; } //debug(palisOdd, palisEven); } void preprocHashes() { pwrsBase.resize(size + 1); invPwrsBase.resize(size + 1); pwrsBase[0] = invPwrsBase[0] = 1; for (int i = 1; i <= size; i++) { pwrsBase[i] = ((LL)pwrsBase[i - 1] * Base) % Mod; } int invBase = powMod(Base, Mod - 2); for (int i = 1; i <= size; i++) { invPwrsBase[i] = ((LL)invPwrsBase[i - 1] * invBase) % Mod; } hashLR = new Hasher(ALL(bucket)); hashRL = new Hasher(bucket.rbegin(), bucket.rend()); } vector<vector<int>> makeSeq(const vector<int> &starts, int seqBound) { vector<VI> result; int last = -1e9; for (int pos : starts) { if (pos - last <= seqBound) { result.back()[1]++; result.back()[2] = pos - last; } else { result.push_back(VI{pos, 1, 0}); } last = pos; } for (auto &data : result) { data[0] += from; } return result; } void submitPalisInfo() { LL result = 0; vector<int> largePalisOdd, largePalisEven; for (int i = 0; i < size; i++) { if (palisOdd[i] <= maxRad) { result += palisOdd[i] + 1; } else { largePalisOdd.push_back(i); } } for (int i = 0; i < size; i++) { if (palisEven[i] <= maxRad) { result += palisEven[i]; } else { largePalisEven.push_back(i); } } seqBound = max(1, maxRad / 2 - 1); vector<vector<int>> seqPalisOdd = makeSeq(largePalisOdd, seqBound), seqPalisEven = makeSeq(largePalisEven, seqBound); debug(result, seqPalisOdd, seqPalisEven); senders[0] << result << seqPalisOdd << seqPalisEven << submit; } vector<VI> rootSeqOdd, rootSeqEven; LL rootTotalResult; vector<int> rootSinglesOdd, rootSinglesEven; void rootAddSeq(vector<VI> &result, const VI &input) { if (SZ(result) == 0) { result.PB(input); } else { auto &left = result.back(); bool leftSingle = left[1] == 1, rightSingle = input[1] == 1; int dist = input[0] - left[0] - (left[1] - 1) * left[2]; if (leftSingle && rightSingle && dist <= seqBound) { left[1] = 2; left[2] = dist; } else if ((leftSingle ^ rightSingle) && input[2] + left[2] == dist) { left[1] += input[1]; left[2] = dist; } else if (!leftSingle && !rightSingle && input[2] == dist && left[2] == dist) { left[1] += input[1]; } else { result.PB(input); } } } void receivePalisInfo(int node) { LL result; vector<VI> seqPalisOdd, seqPalisEven; receivers[node] >> result >> seqPalisOdd >> seqPalisEven; rootTotalResult += result; for (auto &odd : seqPalisOdd) { rootAddSeq(rootSeqOdd, odd); } for (auto &even : seqPalisEven) { rootAddSeq(rootSeqEven, even); } debug(rootTotalResult, rootSeqOdd, rootSeqEven); } void setupPaliCenter() { int pos, even; while (true) { receivers[0] >> pos; if (pos == -1) { senders[0] << submit; return; } receivers[0] >> even; pos -= from; senders[0] << (even ? palisEven : palisOdd)[pos]; } } void resolveSequences() { bool getResults = false; auto askRadius = [&](int pos, int even) { debug(pos, even, getResults); int node = 0; while (nodesFrom[node + 1] <= pos) { node++; } if (!getResults) { senders[node] << pos << even; return -1; } else { int res; receivers[node] >> res; debug(res); return res; } }; for (bool even : {false, true}) { auto &cont = even ? rootSeqEven : rootSeqOdd; for (auto seq : cont) { if (!seq[2]) { continue; } askRadius(seq[0] - seq[2], even); askRadius(seq[0] + seq[1] * seq[2], even); } } for (int i = 0; i < TotalNodes; i++) { senders[i] << -1 << submit; } setupPaliCenter(); getResults = true; for (bool even : {false, true}) { auto &cont = even ? rootSeqEven : rootSeqOdd; for (auto seq : cont) { if (!seq[2]) { (even ? rootSinglesEven : rootSinglesOdd).PB(seq[0]); continue; } int leftR = askRadius(seq[0] - seq[2], even); int rightR = askRadius(seq[0] + seq[1] * seq[2], even); int R = maxRad; int cnt = seq[1]; int step = seq[2]; LL seqResult = 0; int cntSide = cnt / 2; seqResult += (LL)(leftR + rightR) * cntSide + (LL)cntSide * (cntSide + 1) * step; if (cnt % 2 == 1) { (even ? rootSinglesEven : rootSinglesOdd).PB(seq[0] + cntSide * seq[2]); } if (!even) { seqResult += seq[1] - (cnt % 2 == 1); } debug(leftR, rightR, R, cnt, step, cntSide, seqResult); rootTotalResult += seqResult; } } } void setupHashCenter() { int start, end, reverse; while (true) { receivers[0] >> start; if (start == -1) { senders[0] << submit; return; } receivers[0] >> end >> reverse; start -= from; end -= from; int result; if (!reverse) { result = hashLR->getHash(start, end); } else { result = hashRL->getHash(size - end, size - start); } senders[0] << result; } } void setupExtendingCenter() { int left, right; LL curTotal = 0; while (true) { receivers[0] >> left; if (left == -42) { senders[0] << curTotal << submit; return; } receivers[0] >> right; while (left >= 0 && right < N) { char lch = GetLetter(left), rch = GetLetter(right); if (lch == rch) { left--; right++; } else { break; } } curTotal += (right - left) / 2; } } void resolveOrphans() { vector<PII> orphanPalis; for (int v : rootSinglesEven) { orphanPalis.emplace_back(v, v + 1); // ??? FIXME } for (int v : rootSinglesOdd) { orphanPalis.emplace_back(v, v); } debug(orphanPalis); vector<vector<vector<int>>> toCheck; for (auto pali : orphanPalis) { int left = pali.first, right = pali.second; int leftBuck = getBucket(left), rightBuck = getBucket(right); vector<vector<int>> checksForPali; while (left >= 0 && right < N) { int cntLeft = left - nodesFrom[leftBuck] + 1, cntRight = nodesFrom[rightBuck + 1] - right; int cntJump = min(cntLeft, cntRight); senders[leftBuck] << left - cntJump + 1 << left + 1 << 0; senders[rightBuck] << right << right + cntJump << 1; left -= cntJump; right += cntJump; checksForPali.push_back({leftBuck, rightBuck, left, right}); if (cntLeft == cntJump) { leftBuck--; } if (cntRight == cntJump) { rightBuck++; } } toCheck.push_back(checksForPali); } for (int i = 0; i < TotalNodes; i++) { senders[i] << -1 << submit; } setupHashCenter(); int ptr = 0; for (auto &pali : orphanPalis) { bool failed = false; for (auto checks : toCheck[ptr]) { int lbuck = checks[0], rbuck = checks[1], left = checks[2], right = checks[3]; int lhash, rhash; receivers[lbuck] >> lhash; receivers[rbuck] >> rhash; if (!failed && lhash == rhash) { pali.first = left; pali.second = right; } else { failed = true; } } ptr++; } int nodeOrder = 0; for (auto pali : orphanPalis) { nodeOrder = (nodeOrder + 1) % TotalNodes; senders[nodeOrder] << pali.first << pali.second; } for (int i = 0; i < TotalNodes; i++) { senders[i] << -42 << submit; } setupExtendingCenter(); for (int i = 0; i < TotalNodes; i++) { LL incr; receivers[i] >> incr; rootTotalResult += incr; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); init(); N = (int)GetLength(); maxRad = (N + TotalNodes - 1) / TotalNodes; for (int i = 0; i <= TotalNodes; i++) { nodesFrom.PB(((LL)N * i) / TotalNodes); } from = nodesFrom[MyNode]; to = nodesFrom[MyNode + 1]; preprocPalis(); preprocHashes(); submitPalisInfo(); if (!MyNode) { // Rozwiaz ciagi arytmetyczne palindromow. for (int i = 0; i < TotalNodes; i++) { receivePalisInfo(i); } resolveSequences(); // Rozwiaz samotne palindromy. resolveOrphans(); cout << rootTotalResult << "\n"; } else { setupPaliCenter(); setupHashCenter(); setupExtendingCenter(); } }
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 | #include <bits/stdc++.h> #include "palindromy.h" #include "message.h" using namespace std; #define PB push_back #define MP make_pair #define LL long long #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) 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 TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; os << "]"; return os; } template<class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.st << ", " << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int SendAfter = 63990; struct Sender { enum FieldType { FLD_CHAR, FLD_INT, FLD_LL }; struct field { FieldType type; union { char ch; int n; LL l; }; }; struct Submitter {}; vector<field> toSend; int dest; int cap; Sender() : dest(-10000), cap(SendAfter) {} Sender(int dst){ dest = dst; cap = SendAfter; toSend.clear(); } ~Sender(){ if(toSend.size() > 0) submit(); } int capacity() const { return cap; } void capacity(int newCapacity) { cap = newCapacity; } void submit(){ LL msgsize = 4; if(toSend.size() == 0) return; PutInt(dest, (int)toSend.size()); for(auto& f : toSend){ switch(f.type){ case FLD_CHAR: PutChar(dest, f.ch); msgsize++; break; case FLD_INT: PutInt(dest, f.n); msgsize += 4; break; case FLD_LL: PutLL(dest, f.l); msgsize += 8; break; default: assert(false); } } Send(dest); toSend.clear(); } void add_field(field f){ assert(dest >= 0); toSend.push_back(f); if(toSend.size() >= cap) submit(); } friend Sender& operator<<(Sender& snd, char ch){ field f; f.type = FLD_CHAR; f.ch = ch; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, int n){ field f; f.type = FLD_INT; f.n = n; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, LL l){ field f; f.type = FLD_LL; f.l = l; snd.add_field(f); return snd; } friend Sender& operator<<(Sender& snd, Sender::Submitter){ snd.submit(); return snd; } }; Sender::Submitter submit; struct Receiver { int numEnqueued; int node; Receiver() : node(-10000) {} Receiver(int nod) : node(nod), numEnqueued(0) {} void try_fetch(){ assert(node >= -1); assert(numEnqueued >= 0); if(numEnqueued > 0){ numEnqueued--; return; } Receive(node); numEnqueued = GetInt(node); numEnqueued--; } char read_char(){ try_fetch(); return GetChar(node); } int read_int(){ try_fetch(); return GetInt(node); } LL read_ll(){ try_fetch(); return GetLL(node); } friend Receiver& operator>>(Receiver& recv, char& ch){ ch = recv.read_char(); return recv; } friend Receiver& operator>>(Receiver& recv, int& n){ n = recv.read_int(); return recv; } friend Receiver& operator>>(Receiver& recv, LL& l){ l = recv.read_ll(); return recv; } }; template<typename T> Sender& operator<<(Sender& snd, const vector<T>& vec){ snd << (int)vec.size(); for(T v : vec) snd << v; return snd; } template<typename T> Receiver& operator>>(Receiver& recv, vector<T>& vec){ int sz; recv >> sz; vec = vector<T>(sz); for(int i = 0; i < sz; i++){ recv >> vec[i]; } return recv; } LL MyNode, TotalNodes; vector<Sender> senders; vector<Receiver> receivers; void init(){ MyNode = MyNodeId(); TotalNodes = NumberOfNodes(); for(LL i = 0; i < TotalNodes; i++){ senders.emplace_back(i); receivers.emplace_back(i); } } /////////////////////////////////////////////////////////////////////////// struct Manacher { // IGNORES FIRST LETTER - in[0] must be space // par[i] = k <=> [i - k + 1, i + k] is maximal palindrome // npar[i] = k <=> [i - k, i + k] is maximal palindrome VI par, npar; Manacher(string in) { assert(in[0] == ' '); int orig_n = SZ(in) - 1; string s = " #"; for (int i = 1; i <= orig_n; i++) { s += in[i]; s += '#'; } s += '$'; int new_n = SZ(s) - 2; npar.resize(new_n + 2); int furth_beg = 0; int furth_end = 0; for (int i = 1; i <= new_n; i++) { if (furth_end < i) { furth_beg = i; furth_end = i; } int corr_npar = furth_beg + furth_end - i; if (furth_end > i + npar[corr_npar]) { npar[i] = npar[corr_npar]; } else { npar[i] = furth_end - i; furth_beg = i - npar[i]; while (s[furth_beg - 1] == s[furth_end + 1]) { furth_beg--; furth_end++; npar[i]++; } } } par.resize(orig_n + 2); for (int i = 1; i <= orig_n; i++) { if (i < orig_n) { par[i] = npar[2 * i + 1] / 2; } npar[i] = npar[2 * i] / 2; } npar.resize(orig_n + 2); } }; const int Mod = 1e9 + 7, Base = 129847121; vector<int> pwrsBase, invPwrsBase; int powMod(int a, int n) { int res = 1; while (n) { if (n & 1) { res = ((LL)res * a) % Mod; } n >>= 1; a = ((LL)a * a) % Mod; } return res; } struct Hasher { vector<int> hashes; int size; template <typename Iter> Hasher(Iter first, Iter last) { size = distance(first, last); hashes.resize(size + 1); for (int i = 0; i < size; i++) { LL coef = (LL)pwrsBase[i] * (*first); hashes[i + 1] = (hashes[i] + coef) % Mod; first++; } } int getHash(int left, int right) { assert(left <= right); int res = hashes[right] - hashes[left]; if (res < 0) { res += Mod; } return ((LL)res * invPwrsBase[left]) % Mod; } }; int N; int maxRad; int from, to, size; string bucket; vector<int> palisEven, palisOdd; vector<int> nodesFrom; Hasher *hashLR, *hashRL; int seqBound; int getBucket(int pos) { int res = 0; while (res < TotalNodes && nodesFrom[res + 1] <= pos) { res++; } return res; } void preprocPalis() { int leftRad = min(from, maxRad + 1), rightRad = min(N - to, maxRad + 1); int left = from - leftRad, right = to + rightRad; string forPali = " "; for (int i = left; i < right; i++) { char ch = GetLetter(i); forPali += ch; if (i >= from && i < to) { bucket += ch; } } Manacher manacher(forPali); size = to - from; palisEven.resize(size); palisOdd.resize(size); //debug(from, to, left, right); int ptrOdd = leftRad + 1, ptrEven = leftRad + 1; for (int i = 0; i < size; i++) { palisOdd[i] = manacher.npar[ptrOdd++]; palisEven[i] = manacher.par[ptrEven++]; } //debug(palisOdd, palisEven); } void preprocHashes() { pwrsBase.resize(size + 1); invPwrsBase.resize(size + 1); pwrsBase[0] = invPwrsBase[0] = 1; for (int i = 1; i <= size; i++) { pwrsBase[i] = ((LL)pwrsBase[i - 1] * Base) % Mod; } int invBase = powMod(Base, Mod - 2); for (int i = 1; i <= size; i++) { invPwrsBase[i] = ((LL)invPwrsBase[i - 1] * invBase) % Mod; } hashLR = new Hasher(ALL(bucket)); hashRL = new Hasher(bucket.rbegin(), bucket.rend()); } vector<vector<int>> makeSeq(const vector<int> &starts, int seqBound) { vector<VI> result; int last = -1e9; for (int pos : starts) { if (pos - last <= seqBound) { result.back()[1]++; result.back()[2] = pos - last; } else { result.push_back(VI{pos, 1, 0}); } last = pos; } for (auto &data : result) { data[0] += from; } return result; } void submitPalisInfo() { LL result = 0; vector<int> largePalisOdd, largePalisEven; for (int i = 0; i < size; i++) { if (palisOdd[i] <= maxRad) { result += palisOdd[i] + 1; } else { largePalisOdd.push_back(i); } } for (int i = 0; i < size; i++) { if (palisEven[i] <= maxRad) { result += palisEven[i]; } else { largePalisEven.push_back(i); } } seqBound = max(1, maxRad / 2 - 1); vector<vector<int>> seqPalisOdd = makeSeq(largePalisOdd, seqBound), seqPalisEven = makeSeq(largePalisEven, seqBound); debug(result, seqPalisOdd, seqPalisEven); senders[0] << result << seqPalisOdd << seqPalisEven << submit; } vector<VI> rootSeqOdd, rootSeqEven; LL rootTotalResult; vector<int> rootSinglesOdd, rootSinglesEven; void rootAddSeq(vector<VI> &result, const VI &input) { if (SZ(result) == 0) { result.PB(input); } else { auto &left = result.back(); bool leftSingle = left[1] == 1, rightSingle = input[1] == 1; int dist = input[0] - left[0] - (left[1] - 1) * left[2]; if (leftSingle && rightSingle && dist <= seqBound) { left[1] = 2; left[2] = dist; } else if ((leftSingle ^ rightSingle) && input[2] + left[2] == dist) { left[1] += input[1]; left[2] = dist; } else if (!leftSingle && !rightSingle && input[2] == dist && left[2] == dist) { left[1] += input[1]; } else { result.PB(input); } } } void receivePalisInfo(int node) { LL result; vector<VI> seqPalisOdd, seqPalisEven; receivers[node] >> result >> seqPalisOdd >> seqPalisEven; rootTotalResult += result; for (auto &odd : seqPalisOdd) { rootAddSeq(rootSeqOdd, odd); } for (auto &even : seqPalisEven) { rootAddSeq(rootSeqEven, even); } debug(rootTotalResult, rootSeqOdd, rootSeqEven); } void setupPaliCenter() { int pos, even; while (true) { receivers[0] >> pos; if (pos == -1) { senders[0] << submit; return; } receivers[0] >> even; pos -= from; senders[0] << (even ? palisEven : palisOdd)[pos]; } } void resolveSequences() { bool getResults = false; auto askRadius = [&](int pos, int even) { debug(pos, even, getResults); int node = 0; while (nodesFrom[node + 1] <= pos) { node++; } if (!getResults) { senders[node] << pos << even; return -1; } else { int res; receivers[node] >> res; debug(res); return res; } }; for (bool even : {false, true}) { auto &cont = even ? rootSeqEven : rootSeqOdd; for (auto seq : cont) { if (!seq[2]) { continue; } askRadius(seq[0] - seq[2], even); askRadius(seq[0] + seq[1] * seq[2], even); } } for (int i = 0; i < TotalNodes; i++) { senders[i] << -1 << submit; } setupPaliCenter(); getResults = true; for (bool even : {false, true}) { auto &cont = even ? rootSeqEven : rootSeqOdd; for (auto seq : cont) { if (!seq[2]) { (even ? rootSinglesEven : rootSinglesOdd).PB(seq[0]); continue; } int leftR = askRadius(seq[0] - seq[2], even); int rightR = askRadius(seq[0] + seq[1] * seq[2], even); int R = maxRad; int cnt = seq[1]; int step = seq[2]; LL seqResult = 0; int cntSide = cnt / 2; seqResult += (LL)(leftR + rightR) * cntSide + (LL)cntSide * (cntSide + 1) * step; if (cnt % 2 == 1) { (even ? rootSinglesEven : rootSinglesOdd).PB(seq[0] + cntSide * seq[2]); } if (!even) { seqResult += seq[1] - (cnt % 2 == 1); } debug(leftR, rightR, R, cnt, step, cntSide, seqResult); rootTotalResult += seqResult; } } } void setupHashCenter() { int start, end, reverse; while (true) { receivers[0] >> start; if (start == -1) { senders[0] << submit; return; } receivers[0] >> end >> reverse; start -= from; end -= from; int result; if (!reverse) { result = hashLR->getHash(start, end); } else { result = hashRL->getHash(size - end, size - start); } senders[0] << result; } } void setupExtendingCenter() { int left, right; LL curTotal = 0; while (true) { receivers[0] >> left; if (left == -42) { senders[0] << curTotal << submit; return; } receivers[0] >> right; while (left >= 0 && right < N) { char lch = GetLetter(left), rch = GetLetter(right); if (lch == rch) { left--; right++; } else { break; } } curTotal += (right - left) / 2; } } void resolveOrphans() { vector<PII> orphanPalis; for (int v : rootSinglesEven) { orphanPalis.emplace_back(v, v + 1); // ??? FIXME } for (int v : rootSinglesOdd) { orphanPalis.emplace_back(v, v); } debug(orphanPalis); vector<vector<vector<int>>> toCheck; for (auto pali : orphanPalis) { int left = pali.first, right = pali.second; int leftBuck = getBucket(left), rightBuck = getBucket(right); vector<vector<int>> checksForPali; while (left >= 0 && right < N) { int cntLeft = left - nodesFrom[leftBuck] + 1, cntRight = nodesFrom[rightBuck + 1] - right; int cntJump = min(cntLeft, cntRight); senders[leftBuck] << left - cntJump + 1 << left + 1 << 0; senders[rightBuck] << right << right + cntJump << 1; left -= cntJump; right += cntJump; checksForPali.push_back({leftBuck, rightBuck, left, right}); if (cntLeft == cntJump) { leftBuck--; } if (cntRight == cntJump) { rightBuck++; } } toCheck.push_back(checksForPali); } for (int i = 0; i < TotalNodes; i++) { senders[i] << -1 << submit; } setupHashCenter(); int ptr = 0; for (auto &pali : orphanPalis) { bool failed = false; for (auto checks : toCheck[ptr]) { int lbuck = checks[0], rbuck = checks[1], left = checks[2], right = checks[3]; int lhash, rhash; receivers[lbuck] >> lhash; receivers[rbuck] >> rhash; if (!failed && lhash == rhash) { pali.first = left; pali.second = right; } else { failed = true; } } ptr++; } int nodeOrder = 0; for (auto pali : orphanPalis) { nodeOrder = (nodeOrder + 1) % TotalNodes; senders[nodeOrder] << pali.first << pali.second; } for (int i = 0; i < TotalNodes; i++) { senders[i] << -42 << submit; } setupExtendingCenter(); for (int i = 0; i < TotalNodes; i++) { LL incr; receivers[i] >> incr; rootTotalResult += incr; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); init(); N = (int)GetLength(); maxRad = (N + TotalNodes - 1) / TotalNodes; for (int i = 0; i <= TotalNodes; i++) { nodesFrom.PB(((LL)N * i) / TotalNodes); } from = nodesFrom[MyNode]; to = nodesFrom[MyNode + 1]; preprocPalis(); preprocHashes(); submitPalisInfo(); if (!MyNode) { // Rozwiaz ciagi arytmetyczne palindromow. for (int i = 0; i < TotalNodes; i++) { receivePalisInfo(i); } resolveSequences(); // Rozwiaz samotne palindromy. resolveOrphans(); cout << rootTotalResult << "\n"; } else { setupPaliCenter(); setupHashCenter(); setupExtendingCenter(); } } |