#include <iostream> #include <vector> #include <stack> #include "message.h" #include "dzialka.h" int ID, nInst; int width, height; int rowsPerInst; std::vector<int> depth; int64_t result; struct Rect { int begin, height; }; void computeBases() { std::vector<int> base(width); for (int y = 0; y < height; y++) { if ((y % rowsPerInst) == 0) { int otherID = y / rowsPerInst; if (ID == otherID) { depth = base; } else if (ID < width) { for (int x = ID; x < width; x += nInst) { PutInt(otherID, base[x]); } Send(otherID); } } for (int x = ID; x < width; x += nInst) { if (IsUsableCell(y, x)) { base[x]++; } else { base[x] = 0; } } } if (ID*rowsPerInst >= height) { return; } for (int i = 0; i < nInst && i < width; i++) { if (i == ID) { continue; } Receive(i); for (int x = i; x < width; x += nInst) { depth[x] = GetInt(i); } } } void addResult(int64_t width, int64_t height) { result += width*(width+1)/2 * height; } void findRect() { result = 0; for (int y = ID*rowsPerInst; y < (ID+1)*rowsPerInst && y < height; y++) { std::stack<Rect> rects; for (int x = 0; x < width; x++) { if (IsUsableCell(y, x)) { depth[x]++; } else { depth[x] = 0; } Rect part{x, depth[x]}; while (!rects.empty() && rects.top().height > part.height) { auto top = rects.top(); rects.pop(); int yLimit = part.height; if (!rects.empty()) { yLimit = std::max(yLimit, rects.top().height); } addResult(x-top.begin, top.height-yLimit); part.begin = top.begin; } if (rects.empty() || rects.top().height != part.height) { rects.push(part); } } while (!rects.empty()) { auto top = rects.top(); rects.pop(); int yLimit = (rects.empty() ? 0 : rects.top().height); addResult(width-top.begin, top.height-yLimit); } } // Send result or print if (ID > 0) { PutLL(0, result); Send(0); return; } for (int i = 1; i < nInst; i++) { Receive(i); result += GetLL(i); } std::cout << result << std::endl; } int main() { ID = MyNodeId(); nInst = NumberOfNodes(); width = GetFieldWidth(); height = GetFieldHeight(); rowsPerInst = std::max(1, (height-1) / nInst + 1); computeBases(); findRect(); 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 | #include <iostream> #include <vector> #include <stack> #include "message.h" #include "dzialka.h" int ID, nInst; int width, height; int rowsPerInst; std::vector<int> depth; int64_t result; struct Rect { int begin, height; }; void computeBases() { std::vector<int> base(width); for (int y = 0; y < height; y++) { if ((y % rowsPerInst) == 0) { int otherID = y / rowsPerInst; if (ID == otherID) { depth = base; } else if (ID < width) { for (int x = ID; x < width; x += nInst) { PutInt(otherID, base[x]); } Send(otherID); } } for (int x = ID; x < width; x += nInst) { if (IsUsableCell(y, x)) { base[x]++; } else { base[x] = 0; } } } if (ID*rowsPerInst >= height) { return; } for (int i = 0; i < nInst && i < width; i++) { if (i == ID) { continue; } Receive(i); for (int x = i; x < width; x += nInst) { depth[x] = GetInt(i); } } } void addResult(int64_t width, int64_t height) { result += width*(width+1)/2 * height; } void findRect() { result = 0; for (int y = ID*rowsPerInst; y < (ID+1)*rowsPerInst && y < height; y++) { std::stack<Rect> rects; for (int x = 0; x < width; x++) { if (IsUsableCell(y, x)) { depth[x]++; } else { depth[x] = 0; } Rect part{x, depth[x]}; while (!rects.empty() && rects.top().height > part.height) { auto top = rects.top(); rects.pop(); int yLimit = part.height; if (!rects.empty()) { yLimit = std::max(yLimit, rects.top().height); } addResult(x-top.begin, top.height-yLimit); part.begin = top.begin; } if (rects.empty() || rects.top().height != part.height) { rects.push(part); } } while (!rects.empty()) { auto top = rects.top(); rects.pop(); int yLimit = (rects.empty() ? 0 : rects.top().height); addResult(width-top.begin, top.height-yLimit); } } // Send result or print if (ID > 0) { PutLL(0, result); Send(0); return; } for (int i = 1; i < nInst; i++) { Receive(i); result += GetLL(i); } std::cout << result << std::endl; } int main() { ID = MyNodeId(); nInst = NumberOfNodes(); width = GetFieldWidth(); height = GetFieldHeight(); rowsPerInst = std::max(1, (height-1) / nInst + 1); computeBases(); findRect(); return 0; } |