#include <cmath> #include <algorithm> #include <stack> #include <cstdio> #include "dzialka.h" #include "message.h" using Col = int; using Height = int; // // BEGIN: dzialka.c // #include <cassert> // #include <cstdlib> // static char IsInitialized = 0; // static int FieldH, FieldW; // static char **IsUsable; // static void Initialize() { // scanf("%d%d", &FieldH, &FieldW); // char *str = (char *)malloc(FieldW + 1); // IsUsable = (char **)malloc(FieldH * sizeof(char *)); // for (int i = 0; i < FieldH; ++i) { // IsUsable[i] = (char *)malloc(FieldW); // scanf("%s", str); // for (int j = 0; j < FieldW; ++j) // IsUsable[i][j] = (str[j] == '1'); // } // free(str); // IsInitialized = true; // } // int GetFieldHeight() { // if (!IsInitialized) { Initialize(); } // return FieldH; // } // int GetFieldWidth() { // if (!IsInitialized) { Initialize(); } // return FieldW; // } // int IsUsableCell(int Row, int Col) { // if (!IsInitialized) { Initialize(); } // assert(0 <= Row && Row < FieldH && "Nieprawidlowy numer wiersza!"); // assert(0 <= Col && Col < FieldW && "Nieprawidlowy numer kolumny!"); // return IsUsable[Row][Col]; // } // // END dzialka.c int main() { int numOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); int height = GetFieldHeight(); int width = GetFieldWidth(); int firstRow = myNodeId * height / numOfNodes; int lastRow = ((myNodeId + 1) * height / numOfNodes) - 1; // printf("My node id: %d, firstRow: %d, lastRow: %d\n", myNodeId, firstRow, lastRow); int usableColumnHeight[width]; for (int c = 0; c < width; ++c) { usableColumnHeight[c] = 0; } if (firstRow <= lastRow) { for (int c = 0; c < width; ++c) { int r = lastRow; while (firstRow <= r && IsUsableCell(r, c)) { ++usableColumnHeight[c]; --r; } } } const int numChunks = 10; int prevUsableColumnHeight[width]; if (myNodeId != 0) { for (int chunk = 0; chunk < numChunks; ++chunk) { Receive(myNodeId - 1); for (int c = chunk * width / numChunks; c < (chunk + 1) * width / numChunks; ++c) { prevUsableColumnHeight[c] = GetInt(myNodeId - 1); } } } else { for (int c = 0; c < width; ++c) { prevUsableColumnHeight[c] = 0; } } if (myNodeId != numOfNodes - 1) { for (int chunk = 0; chunk < numChunks; ++chunk) { for (int c = chunk * width / numChunks; c < (chunk + 1) * width / numChunks; ++c) { int h = usableColumnHeight[c]; if (usableColumnHeight[c] == lastRow - firstRow + 1) { h += prevUsableColumnHeight[c]; } PutInt(myNodeId + 1, h); } Send(myNodeId + 1); } } long long localRes = 0; for (int r = firstRow; r <= lastRow; ++r) { // printf("Row: %d\n", r); long long sum = 0; std::stack<std::pair<Col, Height>> s; for (int c = 0; c < width; ++c) { // printf("Col: %d\n", c); if (IsUsableCell(r, c)) ++prevUsableColumnHeight[c]; else prevUsableColumnHeight[c] = 0; int takenIntoAccountColumn = c; while (!s.empty() && s.top().second > prevUsableColumnHeight[c]) { std::pair<Col, Height> elem = s.top(); s.pop(); Col k = elem.first; Height g = elem.second; // printf("Pop: (%d, %d)\n", k, g); sum -= (takenIntoAccountColumn - k) * g; // printf("Subtract from stack sum: %d\n", (takenIntoAccountColumn - k) * g); takenIntoAccountColumn = k; } if (prevUsableColumnHeight[c] != 0 && (s.empty() || s.top().second < prevUsableColumnHeight[c])) { s.push(std::make_pair(takenIntoAccountColumn, prevUsableColumnHeight[c])); // printf("Push: (%d, %d)\n", takenIntoAccountColumn, prevUsableColumnHeight[c]); } sum += (c + 1 - takenIntoAccountColumn) * prevUsableColumnHeight[c]; // printf("Add to sum: %d\n", (c + 1 - takenIntoAccountColumn) * prevUsableColumnHeight[c]); localRes += sum; } // printf("localRes for row: %d = %lld\n", r, localRes); } // printf("LocalRes: %lld\n", localRes); if (myNodeId != 0) { PutLL(0, localRes); Send(0); } else { for (int i = 0; i < numOfNodes - 1; ++i) { int source = Receive(-1); localRes += GetLL(source); } printf("%lld\n", localRes); } }
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 | #include <cmath> #include <algorithm> #include <stack> #include <cstdio> #include "dzialka.h" #include "message.h" using Col = int; using Height = int; // // BEGIN: dzialka.c // #include <cassert> // #include <cstdlib> // static char IsInitialized = 0; // static int FieldH, FieldW; // static char **IsUsable; // static void Initialize() { // scanf("%d%d", &FieldH, &FieldW); // char *str = (char *)malloc(FieldW + 1); // IsUsable = (char **)malloc(FieldH * sizeof(char *)); // for (int i = 0; i < FieldH; ++i) { // IsUsable[i] = (char *)malloc(FieldW); // scanf("%s", str); // for (int j = 0; j < FieldW; ++j) // IsUsable[i][j] = (str[j] == '1'); // } // free(str); // IsInitialized = true; // } // int GetFieldHeight() { // if (!IsInitialized) { Initialize(); } // return FieldH; // } // int GetFieldWidth() { // if (!IsInitialized) { Initialize(); } // return FieldW; // } // int IsUsableCell(int Row, int Col) { // if (!IsInitialized) { Initialize(); } // assert(0 <= Row && Row < FieldH && "Nieprawidlowy numer wiersza!"); // assert(0 <= Col && Col < FieldW && "Nieprawidlowy numer kolumny!"); // return IsUsable[Row][Col]; // } // // END dzialka.c int main() { int numOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); int height = GetFieldHeight(); int width = GetFieldWidth(); int firstRow = myNodeId * height / numOfNodes; int lastRow = ((myNodeId + 1) * height / numOfNodes) - 1; // printf("My node id: %d, firstRow: %d, lastRow: %d\n", myNodeId, firstRow, lastRow); int usableColumnHeight[width]; for (int c = 0; c < width; ++c) { usableColumnHeight[c] = 0; } if (firstRow <= lastRow) { for (int c = 0; c < width; ++c) { int r = lastRow; while (firstRow <= r && IsUsableCell(r, c)) { ++usableColumnHeight[c]; --r; } } } const int numChunks = 10; int prevUsableColumnHeight[width]; if (myNodeId != 0) { for (int chunk = 0; chunk < numChunks; ++chunk) { Receive(myNodeId - 1); for (int c = chunk * width / numChunks; c < (chunk + 1) * width / numChunks; ++c) { prevUsableColumnHeight[c] = GetInt(myNodeId - 1); } } } else { for (int c = 0; c < width; ++c) { prevUsableColumnHeight[c] = 0; } } if (myNodeId != numOfNodes - 1) { for (int chunk = 0; chunk < numChunks; ++chunk) { for (int c = chunk * width / numChunks; c < (chunk + 1) * width / numChunks; ++c) { int h = usableColumnHeight[c]; if (usableColumnHeight[c] == lastRow - firstRow + 1) { h += prevUsableColumnHeight[c]; } PutInt(myNodeId + 1, h); } Send(myNodeId + 1); } } long long localRes = 0; for (int r = firstRow; r <= lastRow; ++r) { // printf("Row: %d\n", r); long long sum = 0; std::stack<std::pair<Col, Height>> s; for (int c = 0; c < width; ++c) { // printf("Col: %d\n", c); if (IsUsableCell(r, c)) ++prevUsableColumnHeight[c]; else prevUsableColumnHeight[c] = 0; int takenIntoAccountColumn = c; while (!s.empty() && s.top().second > prevUsableColumnHeight[c]) { std::pair<Col, Height> elem = s.top(); s.pop(); Col k = elem.first; Height g = elem.second; // printf("Pop: (%d, %d)\n", k, g); sum -= (takenIntoAccountColumn - k) * g; // printf("Subtract from stack sum: %d\n", (takenIntoAccountColumn - k) * g); takenIntoAccountColumn = k; } if (prevUsableColumnHeight[c] != 0 && (s.empty() || s.top().second < prevUsableColumnHeight[c])) { s.push(std::make_pair(takenIntoAccountColumn, prevUsableColumnHeight[c])); // printf("Push: (%d, %d)\n", takenIntoAccountColumn, prevUsableColumnHeight[c]); } sum += (c + 1 - takenIntoAccountColumn) * prevUsableColumnHeight[c]; // printf("Add to sum: %d\n", (c + 1 - takenIntoAccountColumn) * prevUsableColumnHeight[c]); localRes += sum; } // printf("localRes for row: %d = %lld\n", r, localRes); } // printf("LocalRes: %lld\n", localRes); if (myNodeId != 0) { PutLL(0, localRes); Send(0); } else { for (int i = 0; i < numOfNodes - 1; ++i) { int source = Receive(-1); localRes += GetLL(source); } printf("%lld\n", localRes); } } |