#ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> #include "message.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " template <typename T> typename make_signed<T>::type ToSigned(T t) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-compare" using ST = typename make_signed<T>::type; if (t <= numeric_limits<ST>::max()) { return static_cast<ST>(t); } else if (t >= numeric_limits<ST>::min()) { return static_cast<ST>(t - numeric_limits<ST>::min()) + numeric_limits<ST>::min(); } else { abort(); } #pragma GCC diagnostic pop } using LengthType = uint32_t; class SendMessage; namespace SendMessageInternal { template <typename T> struct __Helper { static void Put(SendMessage& sm, const T& t); }; #define SM_SIMPLE_TYPE(type) \ template <> \ struct __Helper<type> { \ static void Put(SendMessage& sm, const type& t); \ }; SM_SIMPLE_TYPE(bool); SM_SIMPLE_TYPE(char); SM_SIMPLE_TYPE(signed char); SM_SIMPLE_TYPE(unsigned char); SM_SIMPLE_TYPE(signed int); SM_SIMPLE_TYPE(unsigned int); SM_SIMPLE_TYPE(signed long); SM_SIMPLE_TYPE(unsigned long); SM_SIMPLE_TYPE(signed long long); SM_SIMPLE_TYPE(unsigned long long); #undef SM_SIMPLE_TYPE template <typename A, typename B> struct __Helper<pair<A, B>> { static void Put(SendMessage& sm, const pair<A, B>& p); }; template <typename T> struct __Helper<rge<T>> { static void Put(SendMessage& sm, const rge<T>& d); }; } // namespace SendMessageInternal class SendMessage { private: int id_; bool is_sent_; static int nodes_; static int nodes(); template <typename T> using CleanT = typename remove_reference<typename remove_cv<T>::type>::type; public: SendMessage(int target) : id_(target), is_sent_(false) { assert(0 <= id_ and id_ < nodes()); } ~SendMessage() { if (!is_sent_) { Send(); } } int id() const { return id_; } template <typename T> SendMessage& Put(const T& t) { SendMessageInternal::__Helper<CleanT<T>>::Put(*this, t); return *this; } void Send() { assert(!is_sent_); is_sent_ = true; ::Send(id_); } }; int SendMessage::nodes_ = -1; int SendMessage::nodes() { if (nodes_ == -1) nodes_ = NumberOfNodes(); return nodes_; } namespace SendMessageInternal { template <typename T> void __PutResolver(SendMessage& sm, const T& t) {} template <typename T, typename Ptr1, typename ...Ptrs> void __PutResolver(SendMessage& sm, const T& t, Ptr1 ptr1, Ptrs ...ptrs) { sm.Put(t.*ptr1); __PutResolver(sm, t, ptrs...); } template <typename T> void __Helper<T>::Put(SendMessage& sm, const T& t) { sm.Put(range(begin(t), end(t))); } #define SM_SIMPLE_TYPE(type, func1, func2) \ void __Helper<type>::Put(SendMessage& sm, const type& t) { \ func2(sm.id(), func1(t)); \ } SM_SIMPLE_TYPE(bool, static_cast<char>, PutChar); SM_SIMPLE_TYPE(char, , PutChar); SM_SIMPLE_TYPE(signed char, , PutChar); SM_SIMPLE_TYPE(unsigned char, , PutChar); SM_SIMPLE_TYPE(signed int, , PutInt); SM_SIMPLE_TYPE(unsigned int, ToSigned, PutInt); SM_SIMPLE_TYPE(signed long, , PutLL); SM_SIMPLE_TYPE(unsigned long, ToSigned, PutLL); SM_SIMPLE_TYPE(signed long long, , PutLL); SM_SIMPLE_TYPE(unsigned long long, ToSigned, PutLL); #undef SM_SIMPLE_TYPE template <typename A, typename B> void __Helper<pair<A, B>>::Put(SendMessage& sm, const pair<A, B>& p) { sm.Put(p.first); sm.Put(p.second); } template <typename T> void __Helper<rge<T>>::Put(SendMessage& sm, const rge<T>& d) { sm.Put(static_cast<LengthType>(distance(d.b, d.e))); for (T it = d.b; it != d.e; ++it) { sm.Put(*it); } } } // namespace SendMessageInternal class ReceiveMessage; namespace ReceiveMessageInternal { template <typename T> struct __Helper { static T Get(ReceiveMessage& rm); }; #define RM_SIMPLE_TYPE(type) \ template <> \ struct __Helper<type> { \ static type Get(ReceiveMessage& rm); \ } RM_SIMPLE_TYPE(bool); RM_SIMPLE_TYPE(char); RM_SIMPLE_TYPE(signed char); RM_SIMPLE_TYPE(unsigned char); RM_SIMPLE_TYPE(signed int); RM_SIMPLE_TYPE(unsigned int); RM_SIMPLE_TYPE(signed long); RM_SIMPLE_TYPE(unsigned long); RM_SIMPLE_TYPE(signed long long); RM_SIMPLE_TYPE(unsigned long long); #undef RM_SIMPLE_TYPE template <typename A, typename B> struct __Helper<pair<A, B>> { static pair<A, B> Get(ReceiveMessage& rm); }; } // namespace ReceiveMessageInternal class ReceiveMessage { private: int id_; static int nodes_; static int nodes(); template <typename T> using CleanT = typename remove_reference<typename remove_cv<T>::type>::type; public: int id() const { return id_; } ReceiveMessage(int source) : id_(source) { assert(-1 <= id_ and id_ < nodes()); id_ = Receive(id_); assert(0 <= id_ and id_ < nodes()); } template <typename T> CleanT<T> Get() { return ReceiveMessageInternal::__Helper<CleanT<T>>::Get(*this); } }; int ReceiveMessage::nodes_ = -1; int ReceiveMessage::nodes() { if (nodes_ == -1) nodes_ = NumberOfNodes(); return nodes_; } namespace ReceiveMessageInternal { template <typename T> void __GetResolver(ReceiveMessage& rm, T& t) {} template <typename T, typename Ptr1, typename ...Ptrs> void __GetResolver(ReceiveMessage& rm, T& t, Ptr1 ptr1, Ptrs ...ptrs) { t.*ptr1 = rm.Get<decltype(t.*ptr1)>(); __GetResolver(rm, t, ptrs...); } template <typename T> T __Helper<T>::Get(ReceiveMessage& rm) { T t; auto it = inserter(t, end(t)); const LengthType len = rm.Get<LengthType>(); for (LengthType i = 0; i < len; i++) { *it = rm.Get<typename T::value_type>(); } return t; } #define RM_SIMPLE_TYPE(type, func1, func2) \ type __Helper<type>::Get(ReceiveMessage& rm) { \ return func1(func2(rm.id())); \ } RM_SIMPLE_TYPE(bool, static_cast<char>, GetChar); RM_SIMPLE_TYPE(char, , GetChar); RM_SIMPLE_TYPE(signed char, ToSigned, GetChar); RM_SIMPLE_TYPE(unsigned char, , GetChar); RM_SIMPLE_TYPE(signed int, , GetInt); RM_SIMPLE_TYPE(unsigned int, , GetInt); RM_SIMPLE_TYPE(signed long, , GetLL); RM_SIMPLE_TYPE(unsigned long, , GetLL); RM_SIMPLE_TYPE(signed long long, , GetLL); RM_SIMPLE_TYPE(unsigned long long, , GetLL); #undef RM_SIMPLE_TYPE template <typename A, typename B> pair<A, B> __Helper<pair<A, B>>::Get(ReceiveMessage& rm) { A a = rm.Get<A>(); B b = rm.Get<B>(); return {a, b}; } } // namespace ReceiveMessageInternal #define REGISTER_MESSAGE(type, fields...) \ namespace SendMessageInternal { \ template <> \ struct __Helper<type> { \ static void Put(SendMessage& sm, const type& d) { \ __PutResolver(sm, d, fields); \ } \ }; \ } \ namespace ReceiveMessageInternal { \ template <> \ struct __Helper<type> { \ static type Get(ReceiveMessage& rm) { \ type t; \ __GetResolver(rm, t, fields); \ return t; \ } \ }; \ } #define REGISTER_MESSAGE_CLASS(type, mclass) \ namespace SendMessageInternal { \ template <> struct __Helper<type> : public mclass {}; \ } \ namespace ReceiveMessageInternal { \ template <> struct __Helper<type> : public mclass {}; \ } using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, int b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } template <typename T> class Range { private: T a_, b_; public: Range() : Range(T(1), T(0)) {} Range(T a, T b) : a_(a), b_(b) {} Range GetPart(int i, int n) const { assert(0 <= i and i < n); const T len = length(); const T part = len / n; const int ile_duzych = len % n; auto PartBeginning = [this, part, ile_duzych, n](int j) -> T { return a_ + j * part + min(ile_duzych, j); }; return Range(PartBeginning(i), PartBeginning(i + 1) - 1); } vector<Range> Divide(int n) const { assert(0 < n); vector<Range> res(n); for (int i = 0; i < n; i++) { res[i] = GetPart(i, n); } return res; } T a() const { return a_; } T b() const { return b_; } bool empty() const { return a_ > b_; } const T length() const { if (empty()) return 0; else return b_ - a_ + 1; } Range& operator&=(const Range& r) { Maxi(a_, r.a_); Mini(b_, r.b_); return *this; } Range operator&(const Range& r) const { return Range(*this) &= r; } friend debug& operator<<(debug& deb, const Range& r) { if (r.empty()) return deb << "[empty]"; else return deb << "[" << r.a() << ", " << r.b() << "]"; } }; template <typename T> struct RangeMessageHelper { static void Put(SendMessage& sm, const Range<T>& r) { sm.Put(r.a()).Put(r.b()); } static Range<T> Get(ReceiveMessage& rm) { const int a = rm.Get<int>(); const int b = rm.Get<int>(); return Range<T>(a, b); } }; REGISTER_MESSAGE_CLASS(Range<int>, RangeMessageHelper<int>); REGISTER_MESSAGE_CLASS(Range<ll>, RangeMessageHelper<ll>); #include "dzialka.h" int N, me; int H, W, n; bool Get(int r, int c) { if (0 <= r and r < H and 0 <= c and c < W) { return IsUsableCell(r, c); } return false; } //REGISTER_MESSAGE(Data, &Data::a, &Data::b); ll Policz(const vector<int>& row) { ll pole = 0; ll wynik = 0; vector<pair<int, int>> stos = {{-1, 0}}; for (int x = 0; x < (int) row.size(); x++) { const int h = row[x]; while (stos.back().second > h) { const int xb = stos.back().first; const int hb = stos.back().second; stos.pop_back(); pole -= ll(xb - stos.back().first) * hb; } pole += ll(x - stos.back().first) * h; stos.emplace_back(x, h); assert(pole >= 0); #ifdef LOCAL ll pole2 = 0; for (int i = 1; i < (int) stos.size(); i++) { pole2 += ll(stos[i].first - stos[i - 1].first) * stos[i].second; } assert(pole == pole2); #endif wynik += pole; } return wynik; } int main() { N = NumberOfNodes(); me = MyNodeId(); H = GetFieldHeight(); W = GetFieldWidth(); n = max(H, W); Mini(N, n); if (me >= N) { return 0; } auto ranges = Range<int>(0, n - 1).Divide(N); vector<int> vals(ranges[me].length(), 0); for (int i = 0; i < N; i++) { SendMessage(i).Put(vals); for (int j = ranges[i].a(); j <= ranges[i].b(); j++) { for (int k = ranges[me].a(); k <= ranges[me].b(); k++) { const bool czy = Get(j, k); int& val = vals[k - ranges[me].a()]; if (czy) { val++; } else { val = 0; } } } } vector<int> row; for (int i = 0; i < N; i++) { for (int x : ReceiveMessage(i).Get<vector<int>>()) { row.push_back(x); } } assert((int) row.size() == n); ll wynik = 0; for (int i = ranges[me].a(); i <= ranges[me].b(); i++) { for (int j = 0; j < n; j++) { if (Get(i, j)) { row[j]++; } else { row[j] = 0; } } wynik += Policz(row); } if (me == 0) { for (int i = 1; i < N; i++) { wynik += ReceiveMessage(i).Get<ll>(); } cout << wynik << endl; } else { SendMessage(0).Put(wynik); } 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 | #ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> #include "message.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " template <typename T> typename make_signed<T>::type ToSigned(T t) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-compare" using ST = typename make_signed<T>::type; if (t <= numeric_limits<ST>::max()) { return static_cast<ST>(t); } else if (t >= numeric_limits<ST>::min()) { return static_cast<ST>(t - numeric_limits<ST>::min()) + numeric_limits<ST>::min(); } else { abort(); } #pragma GCC diagnostic pop } using LengthType = uint32_t; class SendMessage; namespace SendMessageInternal { template <typename T> struct __Helper { static void Put(SendMessage& sm, const T& t); }; #define SM_SIMPLE_TYPE(type) \ template <> \ struct __Helper<type> { \ static void Put(SendMessage& sm, const type& t); \ }; SM_SIMPLE_TYPE(bool); SM_SIMPLE_TYPE(char); SM_SIMPLE_TYPE(signed char); SM_SIMPLE_TYPE(unsigned char); SM_SIMPLE_TYPE(signed int); SM_SIMPLE_TYPE(unsigned int); SM_SIMPLE_TYPE(signed long); SM_SIMPLE_TYPE(unsigned long); SM_SIMPLE_TYPE(signed long long); SM_SIMPLE_TYPE(unsigned long long); #undef SM_SIMPLE_TYPE template <typename A, typename B> struct __Helper<pair<A, B>> { static void Put(SendMessage& sm, const pair<A, B>& p); }; template <typename T> struct __Helper<rge<T>> { static void Put(SendMessage& sm, const rge<T>& d); }; } // namespace SendMessageInternal class SendMessage { private: int id_; bool is_sent_; static int nodes_; static int nodes(); template <typename T> using CleanT = typename remove_reference<typename remove_cv<T>::type>::type; public: SendMessage(int target) : id_(target), is_sent_(false) { assert(0 <= id_ and id_ < nodes()); } ~SendMessage() { if (!is_sent_) { Send(); } } int id() const { return id_; } template <typename T> SendMessage& Put(const T& t) { SendMessageInternal::__Helper<CleanT<T>>::Put(*this, t); return *this; } void Send() { assert(!is_sent_); is_sent_ = true; ::Send(id_); } }; int SendMessage::nodes_ = -1; int SendMessage::nodes() { if (nodes_ == -1) nodes_ = NumberOfNodes(); return nodes_; } namespace SendMessageInternal { template <typename T> void __PutResolver(SendMessage& sm, const T& t) {} template <typename T, typename Ptr1, typename ...Ptrs> void __PutResolver(SendMessage& sm, const T& t, Ptr1 ptr1, Ptrs ...ptrs) { sm.Put(t.*ptr1); __PutResolver(sm, t, ptrs...); } template <typename T> void __Helper<T>::Put(SendMessage& sm, const T& t) { sm.Put(range(begin(t), end(t))); } #define SM_SIMPLE_TYPE(type, func1, func2) \ void __Helper<type>::Put(SendMessage& sm, const type& t) { \ func2(sm.id(), func1(t)); \ } SM_SIMPLE_TYPE(bool, static_cast<char>, PutChar); SM_SIMPLE_TYPE(char, , PutChar); SM_SIMPLE_TYPE(signed char, , PutChar); SM_SIMPLE_TYPE(unsigned char, , PutChar); SM_SIMPLE_TYPE(signed int, , PutInt); SM_SIMPLE_TYPE(unsigned int, ToSigned, PutInt); SM_SIMPLE_TYPE(signed long, , PutLL); SM_SIMPLE_TYPE(unsigned long, ToSigned, PutLL); SM_SIMPLE_TYPE(signed long long, , PutLL); SM_SIMPLE_TYPE(unsigned long long, ToSigned, PutLL); #undef SM_SIMPLE_TYPE template <typename A, typename B> void __Helper<pair<A, B>>::Put(SendMessage& sm, const pair<A, B>& p) { sm.Put(p.first); sm.Put(p.second); } template <typename T> void __Helper<rge<T>>::Put(SendMessage& sm, const rge<T>& d) { sm.Put(static_cast<LengthType>(distance(d.b, d.e))); for (T it = d.b; it != d.e; ++it) { sm.Put(*it); } } } // namespace SendMessageInternal class ReceiveMessage; namespace ReceiveMessageInternal { template <typename T> struct __Helper { static T Get(ReceiveMessage& rm); }; #define RM_SIMPLE_TYPE(type) \ template <> \ struct __Helper<type> { \ static type Get(ReceiveMessage& rm); \ } RM_SIMPLE_TYPE(bool); RM_SIMPLE_TYPE(char); RM_SIMPLE_TYPE(signed char); RM_SIMPLE_TYPE(unsigned char); RM_SIMPLE_TYPE(signed int); RM_SIMPLE_TYPE(unsigned int); RM_SIMPLE_TYPE(signed long); RM_SIMPLE_TYPE(unsigned long); RM_SIMPLE_TYPE(signed long long); RM_SIMPLE_TYPE(unsigned long long); #undef RM_SIMPLE_TYPE template <typename A, typename B> struct __Helper<pair<A, B>> { static pair<A, B> Get(ReceiveMessage& rm); }; } // namespace ReceiveMessageInternal class ReceiveMessage { private: int id_; static int nodes_; static int nodes(); template <typename T> using CleanT = typename remove_reference<typename remove_cv<T>::type>::type; public: int id() const { return id_; } ReceiveMessage(int source) : id_(source) { assert(-1 <= id_ and id_ < nodes()); id_ = Receive(id_); assert(0 <= id_ and id_ < nodes()); } template <typename T> CleanT<T> Get() { return ReceiveMessageInternal::__Helper<CleanT<T>>::Get(*this); } }; int ReceiveMessage::nodes_ = -1; int ReceiveMessage::nodes() { if (nodes_ == -1) nodes_ = NumberOfNodes(); return nodes_; } namespace ReceiveMessageInternal { template <typename T> void __GetResolver(ReceiveMessage& rm, T& t) {} template <typename T, typename Ptr1, typename ...Ptrs> void __GetResolver(ReceiveMessage& rm, T& t, Ptr1 ptr1, Ptrs ...ptrs) { t.*ptr1 = rm.Get<decltype(t.*ptr1)>(); __GetResolver(rm, t, ptrs...); } template <typename T> T __Helper<T>::Get(ReceiveMessage& rm) { T t; auto it = inserter(t, end(t)); const LengthType len = rm.Get<LengthType>(); for (LengthType i = 0; i < len; i++) { *it = rm.Get<typename T::value_type>(); } return t; } #define RM_SIMPLE_TYPE(type, func1, func2) \ type __Helper<type>::Get(ReceiveMessage& rm) { \ return func1(func2(rm.id())); \ } RM_SIMPLE_TYPE(bool, static_cast<char>, GetChar); RM_SIMPLE_TYPE(char, , GetChar); RM_SIMPLE_TYPE(signed char, ToSigned, GetChar); RM_SIMPLE_TYPE(unsigned char, , GetChar); RM_SIMPLE_TYPE(signed int, , GetInt); RM_SIMPLE_TYPE(unsigned int, , GetInt); RM_SIMPLE_TYPE(signed long, , GetLL); RM_SIMPLE_TYPE(unsigned long, , GetLL); RM_SIMPLE_TYPE(signed long long, , GetLL); RM_SIMPLE_TYPE(unsigned long long, , GetLL); #undef RM_SIMPLE_TYPE template <typename A, typename B> pair<A, B> __Helper<pair<A, B>>::Get(ReceiveMessage& rm) { A a = rm.Get<A>(); B b = rm.Get<B>(); return {a, b}; } } // namespace ReceiveMessageInternal #define REGISTER_MESSAGE(type, fields...) \ namespace SendMessageInternal { \ template <> \ struct __Helper<type> { \ static void Put(SendMessage& sm, const type& d) { \ __PutResolver(sm, d, fields); \ } \ }; \ } \ namespace ReceiveMessageInternal { \ template <> \ struct __Helper<type> { \ static type Get(ReceiveMessage& rm) { \ type t; \ __GetResolver(rm, t, fields); \ return t; \ } \ }; \ } #define REGISTER_MESSAGE_CLASS(type, mclass) \ namespace SendMessageInternal { \ template <> struct __Helper<type> : public mclass {}; \ } \ namespace ReceiveMessageInternal { \ template <> struct __Helper<type> : public mclass {}; \ } using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, int b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } template <typename T> class Range { private: T a_, b_; public: Range() : Range(T(1), T(0)) {} Range(T a, T b) : a_(a), b_(b) {} Range GetPart(int i, int n) const { assert(0 <= i and i < n); const T len = length(); const T part = len / n; const int ile_duzych = len % n; auto PartBeginning = [this, part, ile_duzych, n](int j) -> T { return a_ + j * part + min(ile_duzych, j); }; return Range(PartBeginning(i), PartBeginning(i + 1) - 1); } vector<Range> Divide(int n) const { assert(0 < n); vector<Range> res(n); for (int i = 0; i < n; i++) { res[i] = GetPart(i, n); } return res; } T a() const { return a_; } T b() const { return b_; } bool empty() const { return a_ > b_; } const T length() const { if (empty()) return 0; else return b_ - a_ + 1; } Range& operator&=(const Range& r) { Maxi(a_, r.a_); Mini(b_, r.b_); return *this; } Range operator&(const Range& r) const { return Range(*this) &= r; } friend debug& operator<<(debug& deb, const Range& r) { if (r.empty()) return deb << "[empty]"; else return deb << "[" << r.a() << ", " << r.b() << "]"; } }; template <typename T> struct RangeMessageHelper { static void Put(SendMessage& sm, const Range<T>& r) { sm.Put(r.a()).Put(r.b()); } static Range<T> Get(ReceiveMessage& rm) { const int a = rm.Get<int>(); const int b = rm.Get<int>(); return Range<T>(a, b); } }; REGISTER_MESSAGE_CLASS(Range<int>, RangeMessageHelper<int>); REGISTER_MESSAGE_CLASS(Range<ll>, RangeMessageHelper<ll>); #include "dzialka.h" int N, me; int H, W, n; bool Get(int r, int c) { if (0 <= r and r < H and 0 <= c and c < W) { return IsUsableCell(r, c); } return false; } //REGISTER_MESSAGE(Data, &Data::a, &Data::b); ll Policz(const vector<int>& row) { ll pole = 0; ll wynik = 0; vector<pair<int, int>> stos = {{-1, 0}}; for (int x = 0; x < (int) row.size(); x++) { const int h = row[x]; while (stos.back().second > h) { const int xb = stos.back().first; const int hb = stos.back().second; stos.pop_back(); pole -= ll(xb - stos.back().first) * hb; } pole += ll(x - stos.back().first) * h; stos.emplace_back(x, h); assert(pole >= 0); #ifdef LOCAL ll pole2 = 0; for (int i = 1; i < (int) stos.size(); i++) { pole2 += ll(stos[i].first - stos[i - 1].first) * stos[i].second; } assert(pole == pole2); #endif wynik += pole; } return wynik; } int main() { N = NumberOfNodes(); me = MyNodeId(); H = GetFieldHeight(); W = GetFieldWidth(); n = max(H, W); Mini(N, n); if (me >= N) { return 0; } auto ranges = Range<int>(0, n - 1).Divide(N); vector<int> vals(ranges[me].length(), 0); for (int i = 0; i < N; i++) { SendMessage(i).Put(vals); for (int j = ranges[i].a(); j <= ranges[i].b(); j++) { for (int k = ranges[me].a(); k <= ranges[me].b(); k++) { const bool czy = Get(j, k); int& val = vals[k - ranges[me].a()]; if (czy) { val++; } else { val = 0; } } } } vector<int> row; for (int i = 0; i < N; i++) { for (int x : ReceiveMessage(i).Get<vector<int>>()) { row.push_back(x); } } assert((int) row.size() == n); ll wynik = 0; for (int i = ranges[me].a(); i <= ranges[me].b(); i++) { for (int j = 0; j < n; j++) { if (Get(i, j)) { row[j]++; } else { row[j] = 0; } } wynik += Policz(row); } if (me == 0) { for (int i = 1; i < N; i++) { wynik += ReceiveMessage(i).Get<ll>(); } cout << wynik << endl; } else { SendMessage(0).Put(wynik); } return 0; } |