#include <iostream> #include "message.h" #include "dzialka.h" #define MIN_ROWS 10 #define MIN_COLS 10 #define PHASE_TRESHOLD 25000000LL int divceil(int a, int b) { return (a + b - 1) / b; } using namespace std; typedef pair<int, int> pii; typedef long long ll; int nodes; int id; bool rotate = false; int width, height; pii parts; pii columns; int phase_count; int ** up; int * zero_row; ll counter = 0; bool is_usable(int y, int x) { return rotate ? IsUsableCell(x, y) : IsUsableCell(y, x); } pii make_column(int index) { return pii( index * columns.second, min(width, (index+1) * columns.second) ); } void send_last_row(pii column, int row) { int next_id = (id+1) % nodes; for (int i = column.first; i < column.second; i++) { PutInt(next_id, up[row][i]); } Send(next_id); } void get_zero(pii column) { int prev_id = (id + nodes - 1) % nodes; Receive(prev_id); for (int i = column.first; i < column.second; i++) { zero_row[i] = GetInt(prev_id); } } void fill_missing_col(pii column, int range_size) { for (int i = column.first; i < column.second; i++) { int pv = zero_row[i]; for (int j = 0; j < range_size && up[j][i] == -1; j++) { up[j][i] = ++pv; } } } pii decide_parts() { int max_part_height = PHASE_TRESHOLD / width; if (max_part_height * nodes >= height) { int part_height = max(divceil(height, nodes), MIN_ROWS); return pii(divceil(height, part_height), part_height); } int min_part_count = divceil(height, max_part_height); return pii(min_part_count, divceil(height, min_part_count)); } void prep() { up = new int*[parts.second]; for (int i = 0; i < parts.second; i++) { up[i] = new int[width]; } zero_row = new int[width](); int max_cols = 900 / phase_count; columns.second = max(MIN_COLS, divceil(width, max_cols)); columns.first = divceil(width, columns.second); } void prefill(pii range, int range_size) { if (id > 0) { for (int i = 0; i < width; i++) { zero_row[i] = -1; } } for (int i = 0; i < width; i++) { int a = zero_row[i]; for (int j = 0; j < range_size; j++) { if (is_usable(range.first + j, i)) { if (a != -1) a++; } else { a = 0; } up[j][i] = a; } } } bool is_filled(pii col_range, int last_row) { for (int i = col_range.first; i < col_range.second; i++) { if (up[last_row][i] == -1) return false; } return true; } void fill_missing(pii range, int range_size) { int col_idx = 0; bool send_next = range.second < height; while (col_idx < columns.first) { pii col_range = make_column(col_idx); bool dependency = send_next && !is_filled(col_range, range_size-1); if (send_next && !dependency) { send_last_row(col_range, range_size-1); } if (id > 0) { get_zero(col_range); fill_missing_col(col_range, range_size); if (dependency) { send_last_row(col_range, range_size-1); } } col_idx++; } } void count_rectangles(int range_size) { for (int j = 0; j < range_size; j++) { for (int si = 0; si < width; si++) { int far = height * 2; for (int i = si; i < width; i++) { far = min(far, up[j][i]); if (far == 0) break; counter += far; } } } } void do_phase(int phase) { int index = phase * nodes + id; if (index >= parts.first) return; pii range = pii( index * parts.second, min((index+1) * parts.second, height) ); int range_size = range.second - range.first; prefill(range, range_size); fill_missing(range, range_size); count_rectangles(range_size); if (id == 0 && phase + 1 < phase_count) { int col_idx = 0; while (col_idx < columns.first) { get_zero(make_column(col_idx)); col_idx++; } } } void sum_up() { if (id == 0) { ll total_counter = counter; for (int i = 1; i < min(nodes, parts.first); i++) { Receive(i); total_counter += GetLL(i); } cout << total_counter << endl; } else { PutLL(0, counter); Send(0); } } int main() { ios_base::sync_with_stdio(0); id = MyNodeId(); nodes = NumberOfNodes(); height = GetFieldHeight(); width = GetFieldWidth(); if (width > height) { rotate = true; swap(width, height); } parts = decide_parts(); phase_count = divceil(parts.first, nodes); if (id >= parts.first) return 0; prep(); for (int i = 0; i < phase_count; i++) { do_phase(i); } sum_up(); 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | #include <iostream> #include "message.h" #include "dzialka.h" #define MIN_ROWS 10 #define MIN_COLS 10 #define PHASE_TRESHOLD 25000000LL int divceil(int a, int b) { return (a + b - 1) / b; } using namespace std; typedef pair<int, int> pii; typedef long long ll; int nodes; int id; bool rotate = false; int width, height; pii parts; pii columns; int phase_count; int ** up; int * zero_row; ll counter = 0; bool is_usable(int y, int x) { return rotate ? IsUsableCell(x, y) : IsUsableCell(y, x); } pii make_column(int index) { return pii( index * columns.second, min(width, (index+1) * columns.second) ); } void send_last_row(pii column, int row) { int next_id = (id+1) % nodes; for (int i = column.first; i < column.second; i++) { PutInt(next_id, up[row][i]); } Send(next_id); } void get_zero(pii column) { int prev_id = (id + nodes - 1) % nodes; Receive(prev_id); for (int i = column.first; i < column.second; i++) { zero_row[i] = GetInt(prev_id); } } void fill_missing_col(pii column, int range_size) { for (int i = column.first; i < column.second; i++) { int pv = zero_row[i]; for (int j = 0; j < range_size && up[j][i] == -1; j++) { up[j][i] = ++pv; } } } pii decide_parts() { int max_part_height = PHASE_TRESHOLD / width; if (max_part_height * nodes >= height) { int part_height = max(divceil(height, nodes), MIN_ROWS); return pii(divceil(height, part_height), part_height); } int min_part_count = divceil(height, max_part_height); return pii(min_part_count, divceil(height, min_part_count)); } void prep() { up = new int*[parts.second]; for (int i = 0; i < parts.second; i++) { up[i] = new int[width]; } zero_row = new int[width](); int max_cols = 900 / phase_count; columns.second = max(MIN_COLS, divceil(width, max_cols)); columns.first = divceil(width, columns.second); } void prefill(pii range, int range_size) { if (id > 0) { for (int i = 0; i < width; i++) { zero_row[i] = -1; } } for (int i = 0; i < width; i++) { int a = zero_row[i]; for (int j = 0; j < range_size; j++) { if (is_usable(range.first + j, i)) { if (a != -1) a++; } else { a = 0; } up[j][i] = a; } } } bool is_filled(pii col_range, int last_row) { for (int i = col_range.first; i < col_range.second; i++) { if (up[last_row][i] == -1) return false; } return true; } void fill_missing(pii range, int range_size) { int col_idx = 0; bool send_next = range.second < height; while (col_idx < columns.first) { pii col_range = make_column(col_idx); bool dependency = send_next && !is_filled(col_range, range_size-1); if (send_next && !dependency) { send_last_row(col_range, range_size-1); } if (id > 0) { get_zero(col_range); fill_missing_col(col_range, range_size); if (dependency) { send_last_row(col_range, range_size-1); } } col_idx++; } } void count_rectangles(int range_size) { for (int j = 0; j < range_size; j++) { for (int si = 0; si < width; si++) { int far = height * 2; for (int i = si; i < width; i++) { far = min(far, up[j][i]); if (far == 0) break; counter += far; } } } } void do_phase(int phase) { int index = phase * nodes + id; if (index >= parts.first) return; pii range = pii( index * parts.second, min((index+1) * parts.second, height) ); int range_size = range.second - range.first; prefill(range, range_size); fill_missing(range, range_size); count_rectangles(range_size); if (id == 0 && phase + 1 < phase_count) { int col_idx = 0; while (col_idx < columns.first) { get_zero(make_column(col_idx)); col_idx++; } } } void sum_up() { if (id == 0) { ll total_counter = counter; for (int i = 1; i < min(nodes, parts.first); i++) { Receive(i); total_counter += GetLL(i); } cout << total_counter << endl; } else { PutLL(0, counter); Send(0); } } int main() { ios_base::sync_with_stdio(0); id = MyNodeId(); nodes = NumberOfNodes(); height = GetFieldHeight(); width = GetFieldWidth(); if (width > height) { rotate = true; swap(width, height); } parts = decide_parts(); phase_count = divceil(parts.first, nodes); if (id >= parts.first) return 0; prep(); for (int i = 0; i < phase_count; i++) { do_phase(i); } sum_up(); return 0; } |