#include "bits/stdc++.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 rge<c>{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 (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #include "message.h" #include "dzialka.h" using ll = long long; using ld = long double; constexpr int naxNodes = 100 + 105; constexpr int nax = 75 * 1000 + 105; constexpr int infty = 1000 * 1000 * 1000 + 5; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int MASTER_NODE = 0; int n; int me; int W, H; int minW[naxNodes], maxW[naxNodes]; int minH[naxNodes], maxH[naxNodes]; int height_line[nax]; ll ComputeAnswerInRow(int r) { debug() << "ComputeAnswerInRow(" imie(r) ")" imie(range(height_line, height_line + W)); ll result = 0; ll sum_stos = 0; vector<pair<ll, ll>> stos; for (int c = 0; c < W; c++) { if (IsUsableCell(r, c)) { height_line[c]++; } else { height_line[c] = 0; } ll cnt = 1; while (!stos.empty() and stos.back().first >= height_line[c]) { sum_stos -= stos.back().first * stos.back().second; cnt += stos.back().second; stos.pop_back(); } sum_stos += height_line[c] * cnt; stos.emplace_back(height_line[c], cnt); result += sum_stos; } return result; } void ComputeAnswersInRows() { ll result = 0; for (int r = minH[me]; r <= maxH[me]; r++) { result += ComputeAnswerInRow(r); } PutLL(MASTER_NODE, result); Send(MASTER_NODE); if (me == MASTER_NODE) { ll sum = 0; for (int node = 0; node < n; node++) { Receive(node); sum += GetLL(node); } printf("%lld\n", sum); } } void ComputeHeightLine() { static int height_above[naxNodes][nax]; for (int c = minW[me]; c <= maxW[me]; c++) { int height = 0; int next_node = 0; for (int r = 0; r < H; r++) { if (r == minH[next_node]) { height_above[next_node][c] = height; next_node++; if (next_node == n) { break; } } if (IsUsableCell(r, c)) { height++; } else { height = 0; } } } for (int node = 0; node < n; node++) { for (int c = minW[me]; c <= maxW[me]; c++) { PutInt(node, height_above[node][c]); } Send(node); } for (int node = 0; node < n; node++) { Receive(node); for (int c = minW[node]; c <= maxW[node]; c++) { height_line[c] = GetInt(node); } } debug() << "height_line: " << range(height_line, height_line + W); } void Algorytm() { ComputeHeightLine(); ComputeAnswersInRows(); } pair<int, int> Podziel(int a, int b, int N, int id) { assert(0 <= id and id < N); assert(a <= b); const int len = b - a + 1; const int small = len / N; const int big_count = len - N * small; assert(0 <= big_count and big_count < N); return {a + small * id + min(big_count, id), a + small * (id + 1) + min(big_count, id + 1) - 1}; } int main() { n = NumberOfNodes(); me = MyNodeId(); assert(1 <= n and n <= 100); assert(0 <= me and me < n); W = GetFieldWidth(); H = GetFieldHeight(); assert(1 <= W and W <= 75000); assert(1 <= H and H <= 75000); n = min(n, min(W, H)); if (me >= n) { debug() << "Jest za mało pracy, umieram sobie :("; return 0; } for (int node = 0; node < n; node++) { auto pw = Podziel(0, W - 1, n, node); minW[node] = pw.first; maxW[node] = pw.second; auto ph = Podziel(0, H - 1, n, node); minH[node] = ph.first; maxH[node] = ph.second; } debug() << imie(W) imie(Podziel(0, W - 1, n, me)); Algorytm(); 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 | #include "bits/stdc++.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 rge<c>{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 (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #include "message.h" #include "dzialka.h" using ll = long long; using ld = long double; constexpr int naxNodes = 100 + 105; constexpr int nax = 75 * 1000 + 105; constexpr int infty = 1000 * 1000 * 1000 + 5; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int MASTER_NODE = 0; int n; int me; int W, H; int minW[naxNodes], maxW[naxNodes]; int minH[naxNodes], maxH[naxNodes]; int height_line[nax]; ll ComputeAnswerInRow(int r) { debug() << "ComputeAnswerInRow(" imie(r) ")" imie(range(height_line, height_line + W)); ll result = 0; ll sum_stos = 0; vector<pair<ll, ll>> stos; for (int c = 0; c < W; c++) { if (IsUsableCell(r, c)) { height_line[c]++; } else { height_line[c] = 0; } ll cnt = 1; while (!stos.empty() and stos.back().first >= height_line[c]) { sum_stos -= stos.back().first * stos.back().second; cnt += stos.back().second; stos.pop_back(); } sum_stos += height_line[c] * cnt; stos.emplace_back(height_line[c], cnt); result += sum_stos; } return result; } void ComputeAnswersInRows() { ll result = 0; for (int r = minH[me]; r <= maxH[me]; r++) { result += ComputeAnswerInRow(r); } PutLL(MASTER_NODE, result); Send(MASTER_NODE); if (me == MASTER_NODE) { ll sum = 0; for (int node = 0; node < n; node++) { Receive(node); sum += GetLL(node); } printf("%lld\n", sum); } } void ComputeHeightLine() { static int height_above[naxNodes][nax]; for (int c = minW[me]; c <= maxW[me]; c++) { int height = 0; int next_node = 0; for (int r = 0; r < H; r++) { if (r == minH[next_node]) { height_above[next_node][c] = height; next_node++; if (next_node == n) { break; } } if (IsUsableCell(r, c)) { height++; } else { height = 0; } } } for (int node = 0; node < n; node++) { for (int c = minW[me]; c <= maxW[me]; c++) { PutInt(node, height_above[node][c]); } Send(node); } for (int node = 0; node < n; node++) { Receive(node); for (int c = minW[node]; c <= maxW[node]; c++) { height_line[c] = GetInt(node); } } debug() << "height_line: " << range(height_line, height_line + W); } void Algorytm() { ComputeHeightLine(); ComputeAnswersInRows(); } pair<int, int> Podziel(int a, int b, int N, int id) { assert(0 <= id and id < N); assert(a <= b); const int len = b - a + 1; const int small = len / N; const int big_count = len - N * small; assert(0 <= big_count and big_count < N); return {a + small * id + min(big_count, id), a + small * (id + 1) + min(big_count, id + 1) - 1}; } int main() { n = NumberOfNodes(); me = MyNodeId(); assert(1 <= n and n <= 100); assert(0 <= me and me < n); W = GetFieldWidth(); H = GetFieldHeight(); assert(1 <= W and W <= 75000); assert(1 <= H and H <= 75000); n = min(n, min(W, H)); if (me >= n) { debug() << "Jest za mało pracy, umieram sobie :("; return 0; } for (int node = 0; node < n; node++) { auto pw = Podziel(0, W - 1, n, node); minW[node] = pw.first; maxW[node] = pw.second; auto ph = Podziel(0, H - 1, n, node); minH[node] = ph.first; maxH[node] = ph.second; } debug() << imie(W) imie(Podziel(0, W - 1, n, me)); Algorytm(); return 0; } |