#include "dzialka.h" #include "message.h" #include <algorithm> #include <stdint.h> #include <cstdio> #include <vector> using namespace std; /*****************************************/ static uint32_t W, H, ID, N; static uint32_t COL_CHUNKS, COL_CHUNK_SIZE; static bool reversed; static void init() { H = GetFieldHeight(); W = GetFieldWidth(); N = NumberOfNodes(); ID = MyNodeId(); reversed = (W<H); if (reversed) swap(W,H); COL_CHUNK_SIZE = 15000; COL_CHUNKS = (H + COL_CHUNK_SIZE - 1) / COL_CHUNK_SIZE; } static int getPixel(int Row, int Col) { if (reversed) return IsUsableCell(Col, Row); return IsUsableCell(Row, Col); } /*****************************************/ struct QElem { int32_t y; uint32_t h, value; uint64_t sum; QElem(int32_t Y=-1, uint32_t H=1, uint32_t VALUE=0, uint64_t SUM=0):y(Y),h(H),value(VALUE),sum(SUM){} }; inline bool cmpQElem(const QElem &it, const uint32_t &y) { return it.y + it.h < y; } uint64_t find_sum(vector<QElem> &Q, uint32_t y) { vector<QElem>::iterator it = lower_bound(Q.begin(), Q.end(), y, cmpQElem); if (it == Q.end()) return 0; return it->sum + (uint64_t)(y + 1 - it->y) * (uint64_t)it->value - (uint64_t)it->h * (uint64_t)it->value; } struct Column { uint64_t result; uint32_t width; vector<uint32_t> L, R; void send(int destID) { PutLL(destID, result); PutInt(destID, width); Send(destID); for (int i=0; i<COL_CHUNKS; ++i) { for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == L.size()) break; PutInt(destID, L[idx]); } Send(destID); } for (int i=0; i<COL_CHUNKS; ++i) { for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == R.size()) break; PutInt(destID, R[idx]); } Send(destID); } } void receive(int srcID) { Receive(srcID); result = GetLL(srcID); width = GetInt(srcID); for (int i=0; i<COL_CHUNKS; ++i) { Receive(srcID); for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == L.size()) break; L[idx] = GetInt(srcID); } } for (int i=0; i<COL_CHUNKS; ++i) { Receive(srcID); for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == R.size()) break; R[idx] = GetInt(srcID); } } } void merge(Column &c) { vector<QElem> QL, QR; vector<uint64_t> merge_result(L.size(), 0); QL.push_back(QElem()); QR.push_back(QElem()); for(int32_t i=0; i<L.size(); ++i) { uint32_t Lvalue = R[i], Rvalue = c.L[i]; if (Lvalue == 0 || Rvalue == 0) { QL.resize(1); QR.resize(1); QL[0].y = QR[0].y = i; continue; } uint32_t QLpos = QL.size() - 1; uint32_t QRpos = QR.size() - 1; while (QL[QLpos].value > Lvalue) --QLpos; while (QR[QRpos].value > Rvalue) --QRpos; uint32_t min_Ly = QL[QLpos].y + (QL[QLpos].value == Lvalue ? 0 : QL[QLpos].h); uint32_t min_Ry = QR[QRpos].y + (QR[QRpos].value == Rvalue ? 0 : QR[QRpos].h); uint32_t min_result_y = min(min_Ly, min_Ry); uint64_t min_sum; if (min_Ly < min_Ry) { min_sum = min_Ly > 0 ? find_sum(QR, min_Ly - 1) : 0; } else if (min_Ly > min_Ry) { min_sum = min_Ry > 0 ? find_sum(QL, min_Ry - 1) : 0; } QL.resize(QLpos+1); QR.resize(QRpos+1); uint32_t new_Ly = QL.back().y + QL.back().h; uint32_t delta_Lheight = i + 1 - new_Ly; if (QL.back().value == Lvalue) { QL.back().h += delta_Lheight; QL.back().sum += (uint64_t)delta_Lheight * (uint64_t)Lvalue; } else QL.push_back(QElem(new_Ly, delta_Lheight, Lvalue, QL.back().sum + (uint64_t)delta_Lheight * (uint64_t)Lvalue)); uint32_t new_Ry = QR.back().y + QR.back().h; uint32_t delta_Rheight = i + 1 - new_Ry; if (QR.back().value == Rvalue) { QR.back().h += delta_Rheight; QR.back().sum += (uint64_t)delta_Rheight * (uint64_t)Rvalue; } else QR.push_back(QElem(new_Ry, delta_Rheight, Rvalue, QR.back().sum + (uint64_t)delta_Rheight * (uint64_t)Rvalue)); merge_result[i] += (uint64_t)Lvalue * (uint64_t)Rvalue * (uint64_t)min(QL.back().h, QR.back().h); if (min_Ly < min_Ry) merge_result[i] += (uint64_t)Lvalue * (uint64_t)(QR[QR.size()-2].sum - min_sum); else if (min_Ly > min_Ry) merge_result[i] += (uint64_t)Rvalue * (uint64_t)(QL[QL.size()-2].sum - min_sum); if (min_result_y > 0) merge_result[i] += merge_result[min_result_y - 1]; result += merge_result[i]; } for(int i=0; i<L.size(); ++i) { L[i] = L[i] + (L[i]==width ? c.L[i] : 0); R[i] = c.R[i] + (c.R[i]==c.width ? R[i] : 0); } width += c.width; result += c.result; } Column(uint32_t H, uint32_t w, int32_t x = -1) { width = w; result = 0; L.resize(H, 0); R.resize(H, 0); if (width == 1) { int32_t last_usable_y = -1; for (int y=0; y<H; ++y) { L[y] = R[y] = getPixel(y, x); if (L[y] == 1) { if (last_usable_y == -1) last_usable_y = y; result += (last_usable_y == -1 ? 1 : y-last_usable_y+1); } else last_usable_y = -1; } } } }; int main() { init(); if (W < 500) { if (ID != 0) return 0; N = 1; } Column result(H, 0); int left = (W/N)*ID, right = (ID==N-1 ? W-1 : (W/N)*(ID+1) - 1); for(int i=left; i<=right; ++i) { Column d(H, 1, i); result.merge(d); } if (ID == 0) { for (int workerID=1; workerID<N; ++workerID) { Column workerResult(H, 0); workerResult.receive(workerID); result.merge(workerResult); } printf("%llu\n", result.result); } else { result.send(0); } 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 | #include "dzialka.h" #include "message.h" #include <algorithm> #include <stdint.h> #include <cstdio> #include <vector> using namespace std; /*****************************************/ static uint32_t W, H, ID, N; static uint32_t COL_CHUNKS, COL_CHUNK_SIZE; static bool reversed; static void init() { H = GetFieldHeight(); W = GetFieldWidth(); N = NumberOfNodes(); ID = MyNodeId(); reversed = (W<H); if (reversed) swap(W,H); COL_CHUNK_SIZE = 15000; COL_CHUNKS = (H + COL_CHUNK_SIZE - 1) / COL_CHUNK_SIZE; } static int getPixel(int Row, int Col) { if (reversed) return IsUsableCell(Col, Row); return IsUsableCell(Row, Col); } /*****************************************/ struct QElem { int32_t y; uint32_t h, value; uint64_t sum; QElem(int32_t Y=-1, uint32_t H=1, uint32_t VALUE=0, uint64_t SUM=0):y(Y),h(H),value(VALUE),sum(SUM){} }; inline bool cmpQElem(const QElem &it, const uint32_t &y) { return it.y + it.h < y; } uint64_t find_sum(vector<QElem> &Q, uint32_t y) { vector<QElem>::iterator it = lower_bound(Q.begin(), Q.end(), y, cmpQElem); if (it == Q.end()) return 0; return it->sum + (uint64_t)(y + 1 - it->y) * (uint64_t)it->value - (uint64_t)it->h * (uint64_t)it->value; } struct Column { uint64_t result; uint32_t width; vector<uint32_t> L, R; void send(int destID) { PutLL(destID, result); PutInt(destID, width); Send(destID); for (int i=0; i<COL_CHUNKS; ++i) { for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == L.size()) break; PutInt(destID, L[idx]); } Send(destID); } for (int i=0; i<COL_CHUNKS; ++i) { for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == R.size()) break; PutInt(destID, R[idx]); } Send(destID); } } void receive(int srcID) { Receive(srcID); result = GetLL(srcID); width = GetInt(srcID); for (int i=0; i<COL_CHUNKS; ++i) { Receive(srcID); for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == L.size()) break; L[idx] = GetInt(srcID); } } for (int i=0; i<COL_CHUNKS; ++i) { Receive(srcID); for (int j=0; j<COL_CHUNK_SIZE; ++j) { int idx = i*COL_CHUNK_SIZE + j; if (idx == R.size()) break; R[idx] = GetInt(srcID); } } } void merge(Column &c) { vector<QElem> QL, QR; vector<uint64_t> merge_result(L.size(), 0); QL.push_back(QElem()); QR.push_back(QElem()); for(int32_t i=0; i<L.size(); ++i) { uint32_t Lvalue = R[i], Rvalue = c.L[i]; if (Lvalue == 0 || Rvalue == 0) { QL.resize(1); QR.resize(1); QL[0].y = QR[0].y = i; continue; } uint32_t QLpos = QL.size() - 1; uint32_t QRpos = QR.size() - 1; while (QL[QLpos].value > Lvalue) --QLpos; while (QR[QRpos].value > Rvalue) --QRpos; uint32_t min_Ly = QL[QLpos].y + (QL[QLpos].value == Lvalue ? 0 : QL[QLpos].h); uint32_t min_Ry = QR[QRpos].y + (QR[QRpos].value == Rvalue ? 0 : QR[QRpos].h); uint32_t min_result_y = min(min_Ly, min_Ry); uint64_t min_sum; if (min_Ly < min_Ry) { min_sum = min_Ly > 0 ? find_sum(QR, min_Ly - 1) : 0; } else if (min_Ly > min_Ry) { min_sum = min_Ry > 0 ? find_sum(QL, min_Ry - 1) : 0; } QL.resize(QLpos+1); QR.resize(QRpos+1); uint32_t new_Ly = QL.back().y + QL.back().h; uint32_t delta_Lheight = i + 1 - new_Ly; if (QL.back().value == Lvalue) { QL.back().h += delta_Lheight; QL.back().sum += (uint64_t)delta_Lheight * (uint64_t)Lvalue; } else QL.push_back(QElem(new_Ly, delta_Lheight, Lvalue, QL.back().sum + (uint64_t)delta_Lheight * (uint64_t)Lvalue)); uint32_t new_Ry = QR.back().y + QR.back().h; uint32_t delta_Rheight = i + 1 - new_Ry; if (QR.back().value == Rvalue) { QR.back().h += delta_Rheight; QR.back().sum += (uint64_t)delta_Rheight * (uint64_t)Rvalue; } else QR.push_back(QElem(new_Ry, delta_Rheight, Rvalue, QR.back().sum + (uint64_t)delta_Rheight * (uint64_t)Rvalue)); merge_result[i] += (uint64_t)Lvalue * (uint64_t)Rvalue * (uint64_t)min(QL.back().h, QR.back().h); if (min_Ly < min_Ry) merge_result[i] += (uint64_t)Lvalue * (uint64_t)(QR[QR.size()-2].sum - min_sum); else if (min_Ly > min_Ry) merge_result[i] += (uint64_t)Rvalue * (uint64_t)(QL[QL.size()-2].sum - min_sum); if (min_result_y > 0) merge_result[i] += merge_result[min_result_y - 1]; result += merge_result[i]; } for(int i=0; i<L.size(); ++i) { L[i] = L[i] + (L[i]==width ? c.L[i] : 0); R[i] = c.R[i] + (c.R[i]==c.width ? R[i] : 0); } width += c.width; result += c.result; } Column(uint32_t H, uint32_t w, int32_t x = -1) { width = w; result = 0; L.resize(H, 0); R.resize(H, 0); if (width == 1) { int32_t last_usable_y = -1; for (int y=0; y<H; ++y) { L[y] = R[y] = getPixel(y, x); if (L[y] == 1) { if (last_usable_y == -1) last_usable_y = y; result += (last_usable_y == -1 ? 1 : y-last_usable_y+1); } else last_usable_y = -1; } } } }; int main() { init(); if (W < 500) { if (ID != 0) return 0; N = 1; } Column result(H, 0); int left = (W/N)*ID, right = (ID==N-1 ? W-1 : (W/N)*(ID+1) - 1); for(int i=left; i<=right; ++i) { Column d(H, 1, i); result.merge(d); } if (ID == 0) { for (int workerID=1; workerID<N; ++workerID) { Column workerResult(H, 0); workerResult.receive(workerID); result.merge(workerResult); } printf("%llu\n", result.result); } else { result.send(0); } return 0; } |