#include "dzialka.h" #include "message.h" #include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& out, const vector<T>& xs) { out << '['; for (auto&& x : xs) out << ' ' << x; return out << " ]"; } enum class DivlineDir { Horizontal, Vertical }; ostream& operator<<(ostream& out, DivlineDir divdir) { return out << (divdir == DivlineDir::Horizontal ? "Horizontal" : "Vertical"); } struct FieldSlice { int x1, y1, x2, y2; long long area() const { return static_cast<long long>(x2 - x1) * (y2 - y1); } bool canBeDivided() const { return area() > 1; } DivlineDir divlineDirection() const { return y2 - y1 >= x2 - x1 ? DivlineDir::Horizontal : DivlineDir::Vertical; } bool at(int x, int y) const { auto result = IsUsableCell(y1+y, x1+x); return result; } template <DivlineDir divdir> int divlineLength() const { return divdir == DivlineDir::Horizontal ? x2 - x1 : y2 - y1; } template <DivlineDir divdir> int constLineCoordinate() const { return divdir == DivlineDir::Horizontal ? (y1+y2)/2-y1 : (x1+x2)/2-x1; } template <DivlineDir divdir> bool atFromDivline(int index, int dist) const { return divdir == DivlineDir::Horizontal ? at(index, dist) : at(dist, index); } template <DivlineDir divdir> int maxDistForward() const { return divdir == DivlineDir::Horizontal ? y2 - y1 : x2 - x1; } template <DivlineDir divdir> FieldSlice nextLeft() const { return divdir == DivlineDir::Horizontal ? FieldSlice{ .x1 = x1, .y1 = y1, .x2 = x2, .y2 = (y1 + y2) / 2 } : FieldSlice { .x1 = x1, .y1 = y1, .x2 = (x1 + x2) / 2, .y2 = y2, }; } template <DivlineDir divdir> FieldSlice nextRight() const { return divdir == DivlineDir::Horizontal ? FieldSlice { .x1 = x1, .y1 = (y1 + y2) / 2, .x2 = x2, .y2 = y2 } : FieldSlice { .x1 = (x1 + x2) / 2, .y1 = y1, .x2 = x2, .y2 = y2 }; } static FieldSlice getWhole() { auto fs = FieldSlice{ .x1 = 0, .y1 = 0, .x2 = ::GetFieldWidth(), .y2 = ::GetFieldHeight(), }; return fs; } }; template <typename T> struct ZRange { T from, to; ZRange(T from, T to):from(from),to(to){} struct Iterator { T index; Iterator& operator++() { ++index; return *this; } const T& operator*() { return index; } bool operator!=(Iterator other) const { return index != other.index; } }; Iterator begin() { return Iterator{from}; } Iterator end() { return Iterator{to}; } friend ostream& operator<<(ostream& out, ZRange zr) { return out << '[' << zr.from << ", " << zr.to << ')'; } }; struct MetaThreadGroup { int i1, i2; int myRelativeId() const { return MyNodeId() - i1; } int relativeNodeCount() const { return i2 - i1; } bool amAlone() const { return relativeNodeCount() == 1; } bool shouldGoLeftNext() const { return myRelativeId() < relativeNodeCount() / 2; } MetaThreadGroup nextLeft() const { return MetaThreadGroup { .i1 = i1, .i2 = (i1 + i2) / 2 }; } MetaThreadGroup nextRight() const { return MetaThreadGroup { .i1 = (i1 + i2) / 2, .i2 = i2 }; } ZRange<int> myRange(long long elementsCount) const { return ZRange<int>( static_cast<int>(elementsCount * (myRelativeId())) / relativeNodeCount(), static_cast<int>(elementsCount * (myRelativeId() + 1)) / relativeNodeCount() ); } template <typename F> void forOthers(F f) { auto ithis = MyNodeId(); for (int i=i1; i<ithis; ++i) f(i); for (int i=ithis+1; i<i2; ++i) f(i); } static MetaThreadGroup startingPoint() { return MetaThreadGroup { .i1 = 0, .i2 = NumberOfNodes(), }; } }; /* (0,0) (1,0) ... (0,1) (1,1) ... ... ... ... ..... => forward - down (.y += 1) ..... backward - up (.y -= 1) ----- ..... ..... ..|.. => forward - right (.x += 1) ..|.. backward - left (.x -= 1) ..|.. ..|.. ..|.. */ inline int forward(int coord) { return coord + 1; } inline int backward(int coord) { return coord - 1; } struct IndexDist { int forw, back; friend ostream& operator<<(ostream& out, IndexDist ind) { return out << "IndexDist(" << (ind.forw != -1 ? to_string(ind.forw) : "…") << ", " << (ind.back != -1 ? to_string(ind.back) : "…") << ")"; } }; template <DivlineDir divdir> vector<IndexDist> phase1(FieldSlice field, MetaThreadGroup thrgroup) { // compute my share auto linelen = field.divlineLength<divdir>(); auto myShare = thrgroup.myRange(linelen); auto linepos = field.constLineCoordinate<divdir>(); auto indexdists = vector<IndexDist>(linelen, IndexDist{-1,-1}); for (auto lineIndex : myShare) { auto distForw = 0; auto distBack = 0; while (linepos+distForw < field.maxDistForward<divdir>() and field.atFromDivline<divdir>(lineIndex, linepos+distForw)) ++distForw; while (linepos-distBack-1 >= 0 and field.atFromDivline<divdir>(lineIndex, linepos-distBack-1)) ++distBack; indexdists[lineIndex] = IndexDist{ distForw, distBack }; } //clog << "[" << MyNodeId() << "] " << indexdists << endl; // send it to other threads thrgroup.forOthers([&](int otherThread){ PutInt(otherThread, myShare.from); PutInt(otherThread, myShare.to); for (auto lineIndex : myShare) { PutInt(otherThread, indexdists[lineIndex].forw); PutInt(otherThread, indexdists[lineIndex].back); } Send(otherThread); }); // receive them thrgroup.forOthers([&](int otherThread){ Receive(otherThread); auto indexFrom = GetInt(otherThread); auto indexTo = GetInt(otherThread); auto otherShare = ZRange<int>(indexFrom, indexTo); for (auto lineIndex : otherShare) { auto forw = GetInt(otherThread); auto back = GetInt(otherThread); assert(indexdists[lineIndex].forw == -1); assert(indexdists[lineIndex].back == -1); indexdists[lineIndex].forw = forw; indexdists[lineIndex].back = back; } }); //clog << "[" << MyNodeId() << "] " << indexdists << endl; assert(none_of(indexdists.begin(), indexdists.end(), [&](IndexDist ind){ return ind.forw == -1 or ind.back == -1; })); return indexdists; } using Result = long long; /* ##### ##### ###\ ##### | | | | | | | | | | | | | | | ##### ###/ ##### LONG LONG WILL NOT HOLD (75000)^4 ~=~ 10^20 */ Result phase2(const vector<IndexDist>& maxdists, MetaThreadGroup thrgroup) { // TODO: this spreads work unevenly, but is way simpler auto myShare = thrgroup.myRange(maxdists.size()); Result subr = 0; for (auto i1 : myShare) { auto forwSoFar = maxdists[i1].forw; auto backSoFar = maxdists[i1].back; for (int i2=i1+1; i2<=static_cast<int>(maxdists.size()); ++i2) { forwSoFar = min(forwSoFar, maxdists[i2-1].forw); backSoFar = min(backSoFar, maxdists[i2-1].back); subr += static_cast<long long>(forwSoFar) * backSoFar; } } return subr; } template <DivlineDir divdir> Result phase3(FieldSlice field, MetaThreadGroup thrgroup) { if (thrgroup.amAlone()) { auto subr2a = solve(field.nextLeft<divdir>(), thrgroup); auto subr2b = solve(field.nextRight<divdir>(), thrgroup); return subr2a + subr2b; } else if (thrgroup.shouldGoLeftNext()) { return solve(field.nextLeft<divdir>(), thrgroup.nextLeft()); } else { return solve(field.nextRight<divdir>(), thrgroup.nextRight()); } } Result solve(FieldSlice field, MetaThreadGroup thrgroup) { if (not field.canBeDivided()) return static_cast<long long>(field.at(0, 0) and thrgroup.myRelativeId() == 0); // TODO auto divdir = field.divlineDirection(); //clog << "[" << MyNodeId() << "] " << divdir << endl; auto maxDistances = divdir == DivlineDir::Horizontal ? phase1<DivlineDir::Horizontal>(field, thrgroup) : phase1<DivlineDir::Vertical>(field, thrgroup); auto subresult1 = phase2(maxDistances, thrgroup); auto subresult2 = divdir == DivlineDir::Horizontal ? phase3<DivlineDir::Horizontal>(field, thrgroup) : phase3<DivlineDir::Vertical>(field, thrgroup); return subresult1 + subresult2; } void processMyResult(Result result) { if (MyNodeId() != 0) { PutLL(0, result); Send(0); } else { for (int other=1; other<NumberOfNodes(); ++other) { Receive(other); auto otherResult = GetLL(other); result += otherResult; } cout << result << '\n'; } } int main() { auto field = FieldSlice::getWhole(); auto thrgroup = MetaThreadGroup::startingPoint(); auto result = solve(field, thrgroup); processMyResult(result); }
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | #include "dzialka.h" #include "message.h" #include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& out, const vector<T>& xs) { out << '['; for (auto&& x : xs) out << ' ' << x; return out << " ]"; } enum class DivlineDir { Horizontal, Vertical }; ostream& operator<<(ostream& out, DivlineDir divdir) { return out << (divdir == DivlineDir::Horizontal ? "Horizontal" : "Vertical"); } struct FieldSlice { int x1, y1, x2, y2; long long area() const { return static_cast<long long>(x2 - x1) * (y2 - y1); } bool canBeDivided() const { return area() > 1; } DivlineDir divlineDirection() const { return y2 - y1 >= x2 - x1 ? DivlineDir::Horizontal : DivlineDir::Vertical; } bool at(int x, int y) const { auto result = IsUsableCell(y1+y, x1+x); return result; } template <DivlineDir divdir> int divlineLength() const { return divdir == DivlineDir::Horizontal ? x2 - x1 : y2 - y1; } template <DivlineDir divdir> int constLineCoordinate() const { return divdir == DivlineDir::Horizontal ? (y1+y2)/2-y1 : (x1+x2)/2-x1; } template <DivlineDir divdir> bool atFromDivline(int index, int dist) const { return divdir == DivlineDir::Horizontal ? at(index, dist) : at(dist, index); } template <DivlineDir divdir> int maxDistForward() const { return divdir == DivlineDir::Horizontal ? y2 - y1 : x2 - x1; } template <DivlineDir divdir> FieldSlice nextLeft() const { return divdir == DivlineDir::Horizontal ? FieldSlice{ .x1 = x1, .y1 = y1, .x2 = x2, .y2 = (y1 + y2) / 2 } : FieldSlice { .x1 = x1, .y1 = y1, .x2 = (x1 + x2) / 2, .y2 = y2, }; } template <DivlineDir divdir> FieldSlice nextRight() const { return divdir == DivlineDir::Horizontal ? FieldSlice { .x1 = x1, .y1 = (y1 + y2) / 2, .x2 = x2, .y2 = y2 } : FieldSlice { .x1 = (x1 + x2) / 2, .y1 = y1, .x2 = x2, .y2 = y2 }; } static FieldSlice getWhole() { auto fs = FieldSlice{ .x1 = 0, .y1 = 0, .x2 = ::GetFieldWidth(), .y2 = ::GetFieldHeight(), }; return fs; } }; template <typename T> struct ZRange { T from, to; ZRange(T from, T to):from(from),to(to){} struct Iterator { T index; Iterator& operator++() { ++index; return *this; } const T& operator*() { return index; } bool operator!=(Iterator other) const { return index != other.index; } }; Iterator begin() { return Iterator{from}; } Iterator end() { return Iterator{to}; } friend ostream& operator<<(ostream& out, ZRange zr) { return out << '[' << zr.from << ", " << zr.to << ')'; } }; struct MetaThreadGroup { int i1, i2; int myRelativeId() const { return MyNodeId() - i1; } int relativeNodeCount() const { return i2 - i1; } bool amAlone() const { return relativeNodeCount() == 1; } bool shouldGoLeftNext() const { return myRelativeId() < relativeNodeCount() / 2; } MetaThreadGroup nextLeft() const { return MetaThreadGroup { .i1 = i1, .i2 = (i1 + i2) / 2 }; } MetaThreadGroup nextRight() const { return MetaThreadGroup { .i1 = (i1 + i2) / 2, .i2 = i2 }; } ZRange<int> myRange(long long elementsCount) const { return ZRange<int>( static_cast<int>(elementsCount * (myRelativeId())) / relativeNodeCount(), static_cast<int>(elementsCount * (myRelativeId() + 1)) / relativeNodeCount() ); } template <typename F> void forOthers(F f) { auto ithis = MyNodeId(); for (int i=i1; i<ithis; ++i) f(i); for (int i=ithis+1; i<i2; ++i) f(i); } static MetaThreadGroup startingPoint() { return MetaThreadGroup { .i1 = 0, .i2 = NumberOfNodes(), }; } }; /* (0,0) (1,0) ... (0,1) (1,1) ... ... ... ... ..... => forward - down (.y += 1) ..... backward - up (.y -= 1) ----- ..... ..... ..|.. => forward - right (.x += 1) ..|.. backward - left (.x -= 1) ..|.. ..|.. ..|.. */ inline int forward(int coord) { return coord + 1; } inline int backward(int coord) { return coord - 1; } struct IndexDist { int forw, back; friend ostream& operator<<(ostream& out, IndexDist ind) { return out << "IndexDist(" << (ind.forw != -1 ? to_string(ind.forw) : "…") << ", " << (ind.back != -1 ? to_string(ind.back) : "…") << ")"; } }; template <DivlineDir divdir> vector<IndexDist> phase1(FieldSlice field, MetaThreadGroup thrgroup) { // compute my share auto linelen = field.divlineLength<divdir>(); auto myShare = thrgroup.myRange(linelen); auto linepos = field.constLineCoordinate<divdir>(); auto indexdists = vector<IndexDist>(linelen, IndexDist{-1,-1}); for (auto lineIndex : myShare) { auto distForw = 0; auto distBack = 0; while (linepos+distForw < field.maxDistForward<divdir>() and field.atFromDivline<divdir>(lineIndex, linepos+distForw)) ++distForw; while (linepos-distBack-1 >= 0 and field.atFromDivline<divdir>(lineIndex, linepos-distBack-1)) ++distBack; indexdists[lineIndex] = IndexDist{ distForw, distBack }; } //clog << "[" << MyNodeId() << "] " << indexdists << endl; // send it to other threads thrgroup.forOthers([&](int otherThread){ PutInt(otherThread, myShare.from); PutInt(otherThread, myShare.to); for (auto lineIndex : myShare) { PutInt(otherThread, indexdists[lineIndex].forw); PutInt(otherThread, indexdists[lineIndex].back); } Send(otherThread); }); // receive them thrgroup.forOthers([&](int otherThread){ Receive(otherThread); auto indexFrom = GetInt(otherThread); auto indexTo = GetInt(otherThread); auto otherShare = ZRange<int>(indexFrom, indexTo); for (auto lineIndex : otherShare) { auto forw = GetInt(otherThread); auto back = GetInt(otherThread); assert(indexdists[lineIndex].forw == -1); assert(indexdists[lineIndex].back == -1); indexdists[lineIndex].forw = forw; indexdists[lineIndex].back = back; } }); //clog << "[" << MyNodeId() << "] " << indexdists << endl; assert(none_of(indexdists.begin(), indexdists.end(), [&](IndexDist ind){ return ind.forw == -1 or ind.back == -1; })); return indexdists; } using Result = long long; /* ##### ##### ###\ ##### | | | | | | | | | | | | | | | ##### ###/ ##### LONG LONG WILL NOT HOLD (75000)^4 ~=~ 10^20 */ Result phase2(const vector<IndexDist>& maxdists, MetaThreadGroup thrgroup) { // TODO: this spreads work unevenly, but is way simpler auto myShare = thrgroup.myRange(maxdists.size()); Result subr = 0; for (auto i1 : myShare) { auto forwSoFar = maxdists[i1].forw; auto backSoFar = maxdists[i1].back; for (int i2=i1+1; i2<=static_cast<int>(maxdists.size()); ++i2) { forwSoFar = min(forwSoFar, maxdists[i2-1].forw); backSoFar = min(backSoFar, maxdists[i2-1].back); subr += static_cast<long long>(forwSoFar) * backSoFar; } } return subr; } template <DivlineDir divdir> Result phase3(FieldSlice field, MetaThreadGroup thrgroup) { if (thrgroup.amAlone()) { auto subr2a = solve(field.nextLeft<divdir>(), thrgroup); auto subr2b = solve(field.nextRight<divdir>(), thrgroup); return subr2a + subr2b; } else if (thrgroup.shouldGoLeftNext()) { return solve(field.nextLeft<divdir>(), thrgroup.nextLeft()); } else { return solve(field.nextRight<divdir>(), thrgroup.nextRight()); } } Result solve(FieldSlice field, MetaThreadGroup thrgroup) { if (not field.canBeDivided()) return static_cast<long long>(field.at(0, 0) and thrgroup.myRelativeId() == 0); // TODO auto divdir = field.divlineDirection(); //clog << "[" << MyNodeId() << "] " << divdir << endl; auto maxDistances = divdir == DivlineDir::Horizontal ? phase1<DivlineDir::Horizontal>(field, thrgroup) : phase1<DivlineDir::Vertical>(field, thrgroup); auto subresult1 = phase2(maxDistances, thrgroup); auto subresult2 = divdir == DivlineDir::Horizontal ? phase3<DivlineDir::Horizontal>(field, thrgroup) : phase3<DivlineDir::Vertical>(field, thrgroup); return subresult1 + subresult2; } void processMyResult(Result result) { if (MyNodeId() != 0) { PutLL(0, result); Send(0); } else { for (int other=1; other<NumberOfNodes(); ++other) { Receive(other); auto otherResult = GetLL(other); result += otherResult; } cout << result << '\n'; } } int main() { auto field = FieldSlice::getWhole(); auto thrgroup = MetaThreadGroup::startingPoint(); auto result = solve(field, thrgroup); processMyResult(result); } |