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
#include <cstdio>
#include <cstdlib>

#include <algorithm>
#include <vector>

#include "message.h"
#include "dzialka.h"

typedef unsigned long long int llu;

static const int MAX_ROW = 75 * 1000;

struct point_t {
	int row, col;
};

unsigned int row_phase1[MAX_ROW] = { 0 };
unsigned int row_phase2[MAX_ROW + 1] = { 0 };

int my_id;
int n_machines;

int num_rows, num_cols;

inline int compute_col(int machine) {
	return (machine * num_cols) / n_machines;
}

inline int compute_row(int machine) {
	return (machine * num_rows) / n_machines;
}

void compute_column() {
	const int col_begin = compute_col(my_id);
	const int col_end = compute_col(my_id + 1);

	for (int c = col_begin; c < col_end; c++) {
		row_phase1[c] = 0;
	}
	int recipient = 0;
	int next_sending_row = 0;
	for (int row = 0; row < num_rows; row++) {
		if (row == next_sending_row) {
			if (recipient == my_id) {
				std::copy(row_phase1 + col_begin, row_phase1 + col_end, row_phase2 + col_begin);
			}
			else {
				for (int c = col_begin; c < col_end; c++) {
					PutInt(recipient, row_phase1[c]);
				}
				Send(recipient);
				// printf("[%d] Send %d..%d to %d\n", my_id, col_begin, col_end, recipient);
			}

			recipient++;
			if (recipient == n_machines) {
				break;
			}
			next_sending_row = compute_row(recipient);
		}

		for (int c = col_begin; c < col_end; c++) {
			if (IsUsableCell(row, c)) {
				row_phase1[c]++;
			}
			else {
				row_phase1[c] = 0;
			}
		}
	}
}

void construct_row() {
	for (int i = 0; i < n_machines; i++) {
		if (i == my_id) {
			continue;
		}

		const int sender = Receive(i);
		const int col_begin = compute_col(sender);
		const int col_end = compute_col(sender + 1);

		for (int c = col_begin; c < col_end; c++) {
			row_phase2[c] = GetInt(sender);
		}
		// printf("[%d] Received %d..%d from %d\n", my_id, col_begin, col_end, sender);
	}
}

llu compute_single() {
	llu possibilities = 0;
	const int row_begin = compute_row(my_id);
	const int row_end = compute_row(my_id + 1);

	std::vector<point_t> stack;

	for (int row = row_begin; row < row_end; row++) {
		// Compute next row
		for (int c = 0; c < num_cols; c++) {
			if (IsUsableCell(row, c)) {
				row_phase2[c]++;
			}
			else {
				row_phase2[c] = 0;
			}
		}

		/*printf("[%d] Row:", my_id, row);
		for (int c = 0; c < num_cols; c++) {
			printf(" %d", row_phase2[c]);
		}
		puts("");*/

		// Compute best rectangles for this row
		stack.clear();
		llu current_field = 0;
		for (int c = 0; c <= num_cols; c++) {
			const unsigned int depth = row_phase2[c];
			int last_popped = c;
			while (!stack.empty() && stack.back().row > depth) {
				// Decrease field appropriately
				const llu col_diff = last_popped - stack.back().col;
				const llu row_diff = stack.back().row - depth;
				current_field -= row_diff * col_diff;
				last_popped = stack.back().col;
				stack.pop_back();
			}

			if (depth > 0 && (stack.empty() || stack.back().row < depth)) {
				point_t p;
				p.row = depth;
				p.col = last_popped;
				stack.push_back(p);
			}

			current_field += depth;
			possibilities += current_field;

			// printf("[%d]   Column: %d\n", my_id, c);
			// printf("[%d]   Possibilities: %lld\n", my_id, possibilities);
			// printf("[%d]   State:", my_id);
			/*for (const auto & p : stack) {
				printf(" (%d, %d)", p.col, p.row);
			}
			puts("");*/
		}
	}

	return possibilities;
}

void gather_results(llu my_result) {
	if (my_id == 0) {
		// Master
		llu result = my_result;
		for (int i = 1; i < n_machines; i++) {
			Receive(i);
			result += GetLL(i);
			result += ((llu)GetLL(i) << 32ULL);
		}

		printf("%llu\n", result);
	}
	else {
		// Slave
		PutLL(0, my_result & 0xFFFFFFFFULL);
		PutLL(0, (my_result >> 32ULL) & 0xFFFFFFFFULL);
		Send(0);
	}
}

int main() {
	num_rows = GetFieldHeight();
	num_cols = GetFieldWidth();
	n_machines = std::min(
		NumberOfNodes(),
		std::min(num_cols, num_rows)
	);
	my_id = MyNodeId();

	if (my_id >= n_machines) {
		return 0;
	}

	compute_column();
	construct_row();
	const llu my_result = compute_single();
	gather_results(my_result);

	return 0;
}