#include <cstdio> #include <stack> #include <vector> #include "dzialka.h" #include "message.h" using namespace std; using LL = long long int; const int MAX_M = 75007; const int MAX_N = 20000; const int MAX_MESSAGE_SIZE = 100; LL field[MAX_M], depth[2][MAX_M], firstZero[MAX_M], missingDepth[MAX_M]; LL result; int n, m, M, nodesAmount, node, firstRow, lastRow; void initParameters() { nodesAmount = NumberOfNodes(); node = MyNodeId(); n = GetFieldHeight(); m = GetFieldWidth(); // n = 75000; // m = 75000; firstRow = (n / nodesAmount) * node; lastRow = (node == nodesAmount - 1) ? n - 1 : (n / nodesAmount) * (node + 1) - 1; n = lastRow - firstRow + 1; M = m / MAX_MESSAGE_SIZE; } LL getDepth(int i, int j) { return (i < firstZero[j]) ? depth[i % 2][j] + missingDepth[j] : depth[i % 2][j]; } void initDepths() { int row = firstRow; for (int j = 1; j <= m; ++j) { firstZero[j] = MAX_N; } for (int i = 1; i <= n; ++i) { row++; for (int j = 1; j <= m; ++j) { depth[i % 2][j] = depth[(i + 1) % 2][j]; // if (row != j) { // ++depth[i % 2][j]; // } else { // depth[i % 2][j] = 0; // if (firstZero[j] == MAX_N) { // firstZero[j] = i; // } // } if (IsUsableCell(i + firstRow - 1, j - 1)) { ++depth[i % 2][j]; } else { depth[i % 2][j] = 0; if (firstZero[j] == MAX_N) { firstZero[j] = i; } } } } } void getMissingDepth() { if (node != 0) { for (int k = 0; k < M; ++k) { Receive(node - 1); for (int i = MAX_MESSAGE_SIZE * k + 1; i <= MAX_MESSAGE_SIZE * (k + 1); ++i) { missingDepth[i] = GetLL(node - 1); } } if (MAX_MESSAGE_SIZE * (M) < m) { Receive(node - 1); } for (int i = MAX_MESSAGE_SIZE * (M) + 1; i <= m; ++i) { missingDepth[i] = GetLL(node - 1); } } if (node != nodesAmount - 1) { for (int k = 0; k < M; ++k) { for (int i = MAX_MESSAGE_SIZE * k + 1; i <= MAX_MESSAGE_SIZE * (k + 1); ++i) { PutLL(node + 1, getDepth(n, i)); } Send(node + 1); } if (MAX_MESSAGE_SIZE * (M) < m) { for (int i = MAX_MESSAGE_SIZE * (M) + 1; i <= m; ++i) { PutLL(node + 1, getDepth(n, i)); } Send(node + 1); } } } void clearDepths() { for (int i = 0; i < 2; ++i) { for (int j = 1; j <= m; ++j) { depth[i][j] = 0; } } } void getRow(int i, int row) { for (int j = 1; j <= m; ++j) { depth[i % 2][j] = depth[(i + 1) % 2][j]; if (IsUsableCell(i + firstRow - 1, j - 1)) { ++depth[i % 2][j]; } else { depth[i % 2][j] = 0; } // if (row != j) { // ++depth[i % 2][j]; // } else { // depth[i % 2][j] = 0; // } } } void solve() { int row = firstRow; for (int i = 1; i <= n; ++i) { row++; getRow(i, row); stack<pair<LL, LL>> S; S.push(make_pair(0, 0)); for (int j = 1; j <= m; ++j) { int left = j; while (getDepth(i, j) < S.top().second) { auto top = S.top(); S.pop(); auto height = top.second; auto width = j - top.first; if (S.top().second > getDepth(i, j)) { height -= S.top().second; } else if (getDepth(i, j) > 0) { height -= getDepth(i, j); } result += (height * width * (width + 1)) / 2; left = top.first; } if (S.top().second < getDepth(i, j)) { S.push(make_pair(left, getDepth(i, j))); } } while (S.size() > 1) { auto top = S.top(); S.pop(); auto height = top.second; auto width = m + 1 - top.first; height -= S.top().second; result += (height * width * (width + 1)) / 2; } } if (node == 0) { for (int i = 1; i < nodesAmount; ++i) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } else { PutLL(0, result); Send(0); } } int main() { initParameters(); initDepths(); getMissingDepth(); clearDepths(); solve(); }
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 | #include <cstdio> #include <stack> #include <vector> #include "dzialka.h" #include "message.h" using namespace std; using LL = long long int; const int MAX_M = 75007; const int MAX_N = 20000; const int MAX_MESSAGE_SIZE = 100; LL field[MAX_M], depth[2][MAX_M], firstZero[MAX_M], missingDepth[MAX_M]; LL result; int n, m, M, nodesAmount, node, firstRow, lastRow; void initParameters() { nodesAmount = NumberOfNodes(); node = MyNodeId(); n = GetFieldHeight(); m = GetFieldWidth(); // n = 75000; // m = 75000; firstRow = (n / nodesAmount) * node; lastRow = (node == nodesAmount - 1) ? n - 1 : (n / nodesAmount) * (node + 1) - 1; n = lastRow - firstRow + 1; M = m / MAX_MESSAGE_SIZE; } LL getDepth(int i, int j) { return (i < firstZero[j]) ? depth[i % 2][j] + missingDepth[j] : depth[i % 2][j]; } void initDepths() { int row = firstRow; for (int j = 1; j <= m; ++j) { firstZero[j] = MAX_N; } for (int i = 1; i <= n; ++i) { row++; for (int j = 1; j <= m; ++j) { depth[i % 2][j] = depth[(i + 1) % 2][j]; // if (row != j) { // ++depth[i % 2][j]; // } else { // depth[i % 2][j] = 0; // if (firstZero[j] == MAX_N) { // firstZero[j] = i; // } // } if (IsUsableCell(i + firstRow - 1, j - 1)) { ++depth[i % 2][j]; } else { depth[i % 2][j] = 0; if (firstZero[j] == MAX_N) { firstZero[j] = i; } } } } } void getMissingDepth() { if (node != 0) { for (int k = 0; k < M; ++k) { Receive(node - 1); for (int i = MAX_MESSAGE_SIZE * k + 1; i <= MAX_MESSAGE_SIZE * (k + 1); ++i) { missingDepth[i] = GetLL(node - 1); } } if (MAX_MESSAGE_SIZE * (M) < m) { Receive(node - 1); } for (int i = MAX_MESSAGE_SIZE * (M) + 1; i <= m; ++i) { missingDepth[i] = GetLL(node - 1); } } if (node != nodesAmount - 1) { for (int k = 0; k < M; ++k) { for (int i = MAX_MESSAGE_SIZE * k + 1; i <= MAX_MESSAGE_SIZE * (k + 1); ++i) { PutLL(node + 1, getDepth(n, i)); } Send(node + 1); } if (MAX_MESSAGE_SIZE * (M) < m) { for (int i = MAX_MESSAGE_SIZE * (M) + 1; i <= m; ++i) { PutLL(node + 1, getDepth(n, i)); } Send(node + 1); } } } void clearDepths() { for (int i = 0; i < 2; ++i) { for (int j = 1; j <= m; ++j) { depth[i][j] = 0; } } } void getRow(int i, int row) { for (int j = 1; j <= m; ++j) { depth[i % 2][j] = depth[(i + 1) % 2][j]; if (IsUsableCell(i + firstRow - 1, j - 1)) { ++depth[i % 2][j]; } else { depth[i % 2][j] = 0; } // if (row != j) { // ++depth[i % 2][j]; // } else { // depth[i % 2][j] = 0; // } } } void solve() { int row = firstRow; for (int i = 1; i <= n; ++i) { row++; getRow(i, row); stack<pair<LL, LL>> S; S.push(make_pair(0, 0)); for (int j = 1; j <= m; ++j) { int left = j; while (getDepth(i, j) < S.top().second) { auto top = S.top(); S.pop(); auto height = top.second; auto width = j - top.first; if (S.top().second > getDepth(i, j)) { height -= S.top().second; } else if (getDepth(i, j) > 0) { height -= getDepth(i, j); } result += (height * width * (width + 1)) / 2; left = top.first; } if (S.top().second < getDepth(i, j)) { S.push(make_pair(left, getDepth(i, j))); } } while (S.size() > 1) { auto top = S.top(); S.pop(); auto height = top.second; auto width = m + 1 - top.first; height -= S.top().second; result += (height * width * (width + 1)) / 2; } } if (node == 0) { for (int i = 1; i < nodesAmount; ++i) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } else { PutLL(0, result); Send(0); } } int main() { initParameters(); initDepths(); getMissingDepth(); clearDepths(); solve(); } |