#include "dzialka.h" #include "message.h" #include <iostream> #include <vector> #include <cassert> #include <cstdlib> #include <algorithm> #include <climits> #include <cstdint> using namespace std; const int kNodes = NumberOfNodes(); const int kId = MyNodeId(); bool transposed = false; int rows = GetFieldHeight(); int cols = GetFieldWidth(); int my_row_start; int my_row_end; int my_col_start; int my_col_end; int IsUsable(int row, int col) { if (!transposed) { return IsUsableCell(row, col); } else { return IsUsableCell(col, row); } } void SendRow(int target, int* const row) { for (int i = 0; i < cols; ++i) { PutInt(target, row[i]); } Send(target); } void SendOne(int target, uint64_t value) { PutLL(target, value); Send(target); } void ReceiveRow(int* const row) { const int source = Receive(-1); for (int i = 0; i < cols; ++i) { row[i] = GetInt(source); } } uint64_t ReceiveOne() { const int source = Receive(-1); return GetLL(source); } int T[2][75001]; int Lower[75001]; int NbLower[75001]; int Sorted[75001]; uint64_t FastSums(int start, int end, int* const row) { if (end <= start) { return 0; } if (end - start == 1) { return row[start]; } if (end - start == 2) { return row[start] + row[end - 1] + min(row[start], row[end - 1]); } const int middle = (start + end) / 2; int s_val = INT_MAX; int s_idx = -1; for (int i = start; i < end; ++i) { const bool equal_better = ( row[i] == s_val && abs(middle - i) < abs(middle - s_idx)); if (row[i] < s_val || equal_better) { s_val = row[i]; s_idx = i; } } if (s_idx == start || s_idx == end - 1) { for (int i = start; i < end; ++i) { Sorted[i] = row[i]; } sort(Sorted + start, Sorted + end); uint64_t result = 0; int min_idx = start; while (true) { if (start == end) { return result; } else if (row[start] == Sorted[min_idx]) { result += row[start] * (end - start); ++start; ++min_idx; } else if (row[end - 1] == Sorted[min_idx]) { result += row[end - 1] * (end - start); --end; ++min_idx; } else { break; } } return result + FastSums(start, end, row); } return FastSums(start, s_idx, row) + FastSums(s_idx + 1, end, row) + (uint64_t) s_val * (s_idx - start + 1) * (end - s_idx); } uint64_t Solve(int r_start, int r_end, int c_start, int c_end) { uint64_t result = 0; for (int r = r_start; r < r_end; ++r) { for (int c = c_start; c < c_end; ++c) { const int usable = IsUsable(r, c); if (r == r_start) { T[r % 2][c] = usable == 0 ? 0 : NbLower[c] + usable; } else { T[r % 2][c] = usable == 0 ? 0 : T[(r + 1) % 2][c] + usable; } } result += FastSums(c_start, c_end, T[r % 2]); } return result; } void Depths(int r_start, int r_end, int c_start, int c_end) { for (int r = r_start; r < r_end; ++r) { for (int c = c_start; c < c_end; ++c) { const int usable = IsUsable(r, c); if (r == 0) { T[r % 2][c] = usable; } else { T[r % 2][c] = usable == 0 ? 0 : T[(r + 1) % 2][c] + usable; } } } for (int c = c_start; c < c_end; ++c) { Lower[c] = T[(r_end - 1) % 2][c]; } } void Transpose() { transposed = !transposed; swap(rows, cols); swap(my_row_start, my_col_start); swap(my_row_end, my_col_end); } int main() { ios::sync_with_stdio(false); if (rows < cols) { Transpose(); } my_row_start = kId * rows / kNodes; my_row_end = (kId + 1) * rows / kNodes; my_col_start = 0; my_col_end = cols; if ((uint64_t) cols * cols < 100000LL || kNodes == 1) { if (kId == 0) { cout << Solve(0, rows, 0, cols) << endl; } } else { uint64_t result = 0; Depths(my_row_start, my_row_end, my_col_start, my_col_end); if (kId != 0) { ReceiveRow(NbLower); } if (kId != kNodes - 1) { const int my_rows = my_row_end - my_row_start; for (int c = my_col_start; c < my_col_end; ++c) { if (Lower[c] == my_rows) { Lower[c] += NbLower[c]; } } SendRow(kId + 1, Lower); } result = Solve(my_row_start, my_row_end, my_col_start, my_col_end); if (kId != 0) { SendOne(0, result); } else { for (int i = 1; i < kNodes; ++i) { result += ReceiveOne(); } cout << result << endl; } } 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | #include "dzialka.h" #include "message.h" #include <iostream> #include <vector> #include <cassert> #include <cstdlib> #include <algorithm> #include <climits> #include <cstdint> using namespace std; const int kNodes = NumberOfNodes(); const int kId = MyNodeId(); bool transposed = false; int rows = GetFieldHeight(); int cols = GetFieldWidth(); int my_row_start; int my_row_end; int my_col_start; int my_col_end; int IsUsable(int row, int col) { if (!transposed) { return IsUsableCell(row, col); } else { return IsUsableCell(col, row); } } void SendRow(int target, int* const row) { for (int i = 0; i < cols; ++i) { PutInt(target, row[i]); } Send(target); } void SendOne(int target, uint64_t value) { PutLL(target, value); Send(target); } void ReceiveRow(int* const row) { const int source = Receive(-1); for (int i = 0; i < cols; ++i) { row[i] = GetInt(source); } } uint64_t ReceiveOne() { const int source = Receive(-1); return GetLL(source); } int T[2][75001]; int Lower[75001]; int NbLower[75001]; int Sorted[75001]; uint64_t FastSums(int start, int end, int* const row) { if (end <= start) { return 0; } if (end - start == 1) { return row[start]; } if (end - start == 2) { return row[start] + row[end - 1] + min(row[start], row[end - 1]); } const int middle = (start + end) / 2; int s_val = INT_MAX; int s_idx = -1; for (int i = start; i < end; ++i) { const bool equal_better = ( row[i] == s_val && abs(middle - i) < abs(middle - s_idx)); if (row[i] < s_val || equal_better) { s_val = row[i]; s_idx = i; } } if (s_idx == start || s_idx == end - 1) { for (int i = start; i < end; ++i) { Sorted[i] = row[i]; } sort(Sorted + start, Sorted + end); uint64_t result = 0; int min_idx = start; while (true) { if (start == end) { return result; } else if (row[start] == Sorted[min_idx]) { result += row[start] * (end - start); ++start; ++min_idx; } else if (row[end - 1] == Sorted[min_idx]) { result += row[end - 1] * (end - start); --end; ++min_idx; } else { break; } } return result + FastSums(start, end, row); } return FastSums(start, s_idx, row) + FastSums(s_idx + 1, end, row) + (uint64_t) s_val * (s_idx - start + 1) * (end - s_idx); } uint64_t Solve(int r_start, int r_end, int c_start, int c_end) { uint64_t result = 0; for (int r = r_start; r < r_end; ++r) { for (int c = c_start; c < c_end; ++c) { const int usable = IsUsable(r, c); if (r == r_start) { T[r % 2][c] = usable == 0 ? 0 : NbLower[c] + usable; } else { T[r % 2][c] = usable == 0 ? 0 : T[(r + 1) % 2][c] + usable; } } result += FastSums(c_start, c_end, T[r % 2]); } return result; } void Depths(int r_start, int r_end, int c_start, int c_end) { for (int r = r_start; r < r_end; ++r) { for (int c = c_start; c < c_end; ++c) { const int usable = IsUsable(r, c); if (r == 0) { T[r % 2][c] = usable; } else { T[r % 2][c] = usable == 0 ? 0 : T[(r + 1) % 2][c] + usable; } } } for (int c = c_start; c < c_end; ++c) { Lower[c] = T[(r_end - 1) % 2][c]; } } void Transpose() { transposed = !transposed; swap(rows, cols); swap(my_row_start, my_col_start); swap(my_row_end, my_col_end); } int main() { ios::sync_with_stdio(false); if (rows < cols) { Transpose(); } my_row_start = kId * rows / kNodes; my_row_end = (kId + 1) * rows / kNodes; my_col_start = 0; my_col_end = cols; if ((uint64_t) cols * cols < 100000LL || kNodes == 1) { if (kId == 0) { cout << Solve(0, rows, 0, cols) << endl; } } else { uint64_t result = 0; Depths(my_row_start, my_row_end, my_col_start, my_col_end); if (kId != 0) { ReceiveRow(NbLower); } if (kId != kNodes - 1) { const int my_rows = my_row_end - my_row_start; for (int c = my_col_start; c < my_col_end; ++c) { if (Lower[c] == my_rows) { Lower[c] += NbLower[c]; } } SendRow(kId + 1, Lower); } result = Solve(my_row_start, my_row_end, my_col_start, my_col_end); if (kId != 0) { SendOne(0, result); } else { for (int i = 1; i < kNodes; ++i) { result += ReceiveOne(); } cout << result << endl; } } return 0; } |