#include <bits/stdc++.h> #include "message.h" #include "dzialka.h" using namespace std; const int N = 75005, INF = 1e9; int m, id; int height[N], heightUpper[N]; pair<int, int > S[N]; int size; vector<bool> isUsable[1000]; long long solve() { size = 0; S[++size] = {-INF - 1, -1}; long long ret = 0; long long area = 0; for (int i = 0; i < m; i++) { while (S[size].first >= height[i]) { area -= (long long)S[size].first * (S[size].second - S[size - 1].second); size--; } area += (long long)(i - S[size].second) * height[i]; ret += area; S[++size] = {height[i], i}; } return ret; } int readArray(int target, int *tab) { int start = 0; if (m >= 50000) { Receive(target); for (int i = 0; i < 50000; i++) { tab[i] = GetInt(target); } start = 50000; } Receive(target); for (int i = start; i < m; i++) { tab[i] = GetInt(target); } return GetInt(target); } void sendArray(int target, int *tab, int length) { int start = 0; if (m >= 50000) { for (int i = 0; i < 50000; i++) { PutInt(target, tab[i]); } Send(target); start = 50000; } for (int i = start; i < m; i++) { PutInt(target, tab[i]); } PutInt(target, length); Send(target); } void joinArrays(int *tab, int *upper, int rows) { for (int i = 0; i < m; i++) { if (tab[i] < 0) { tab[i] = upper[i] + rows; } } } int main() { for (int i = 0; i < 1000; i++) { isUsable[i] = vector<bool>(N); } int n = GetFieldHeight(); m = GetFieldWidth(); int nodes = min(n, NumberOfNodes()); id = MyNodeId(); if (id >= nodes) return 0; int rowFrom = id * n / nodes; int rowTo = min(n, (id + 1) * n / nodes); int rows = rowTo - rowFrom; int oldRows = rows; for (int i = 0; i < rows; i++) { for (int j = 0; j < m; j++) { int usable = IsUsableCell(i + rowFrom, j); isUsable[i][j] = usable; height[j] = usable ? (i == 0 ? (rowFrom == 0 ? 1 : -INF) : (height[j] + 1)) : 0; } } for (int x = 1; x < 2 * nodes; x *= 2) { if (id + x < nodes) { sendArray(id + x, height, rows); } int r = 0; if (id - x >= 0) { r = readArray(id - x, heightUpper); } joinArrays(height, heightUpper, rows); rows += r; } if (id + 1 < nodes) { sendArray(id + 1, height, rows); } for (int i = 0; i < m; i++) { heightUpper[i] = 0; } if (id - 1 >= 0) { readArray(id - 1, heightUpper); } for (int i = 0; i < m; i++) { height[i] = 0; } unsigned long long ans = 0; for (int i = 0; i < oldRows; i++) { for (int j = 0; j < m; j++) { int usable = isUsable[i][j]; height[j] = usable ? (i == 0 ? heightUpper[j] + 1 : (height[j] + 1)) : 0; } ans += solve(); } PutLL(0, ans); Send(0); if (id == 0) { ans = 0; for (int i = 0; i < nodes; i++) { int inst = Receive(-1); ans += (unsigned long long)GetLL(inst); } printf("%llu\n", ans); } 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 | #include <bits/stdc++.h> #include "message.h" #include "dzialka.h" using namespace std; const int N = 75005, INF = 1e9; int m, id; int height[N], heightUpper[N]; pair<int, int > S[N]; int size; vector<bool> isUsable[1000]; long long solve() { size = 0; S[++size] = {-INF - 1, -1}; long long ret = 0; long long area = 0; for (int i = 0; i < m; i++) { while (S[size].first >= height[i]) { area -= (long long)S[size].first * (S[size].second - S[size - 1].second); size--; } area += (long long)(i - S[size].second) * height[i]; ret += area; S[++size] = {height[i], i}; } return ret; } int readArray(int target, int *tab) { int start = 0; if (m >= 50000) { Receive(target); for (int i = 0; i < 50000; i++) { tab[i] = GetInt(target); } start = 50000; } Receive(target); for (int i = start; i < m; i++) { tab[i] = GetInt(target); } return GetInt(target); } void sendArray(int target, int *tab, int length) { int start = 0; if (m >= 50000) { for (int i = 0; i < 50000; i++) { PutInt(target, tab[i]); } Send(target); start = 50000; } for (int i = start; i < m; i++) { PutInt(target, tab[i]); } PutInt(target, length); Send(target); } void joinArrays(int *tab, int *upper, int rows) { for (int i = 0; i < m; i++) { if (tab[i] < 0) { tab[i] = upper[i] + rows; } } } int main() { for (int i = 0; i < 1000; i++) { isUsable[i] = vector<bool>(N); } int n = GetFieldHeight(); m = GetFieldWidth(); int nodes = min(n, NumberOfNodes()); id = MyNodeId(); if (id >= nodes) return 0; int rowFrom = id * n / nodes; int rowTo = min(n, (id + 1) * n / nodes); int rows = rowTo - rowFrom; int oldRows = rows; for (int i = 0; i < rows; i++) { for (int j = 0; j < m; j++) { int usable = IsUsableCell(i + rowFrom, j); isUsable[i][j] = usable; height[j] = usable ? (i == 0 ? (rowFrom == 0 ? 1 : -INF) : (height[j] + 1)) : 0; } } for (int x = 1; x < 2 * nodes; x *= 2) { if (id + x < nodes) { sendArray(id + x, height, rows); } int r = 0; if (id - x >= 0) { r = readArray(id - x, heightUpper); } joinArrays(height, heightUpper, rows); rows += r; } if (id + 1 < nodes) { sendArray(id + 1, height, rows); } for (int i = 0; i < m; i++) { heightUpper[i] = 0; } if (id - 1 >= 0) { readArray(id - 1, heightUpper); } for (int i = 0; i < m; i++) { height[i] = 0; } unsigned long long ans = 0; for (int i = 0; i < oldRows; i++) { for (int j = 0; j < m; j++) { int usable = isUsable[i][j]; height[j] = usable ? (i == 0 ? heightUpper[j] + 1 : (height[j] + 1)) : 0; } ans += solve(); } PutLL(0, ans); Send(0); if (id == 0) { ans = 0; for (int i = 0; i < nodes; i++) { int inst = Receive(-1); ans += (unsigned long long)GetLL(inst); } printf("%llu\n", ans); } return 0; } |