#include <iostream> #include <vector> #include <stack> #include <cstdio> #include <algorithm> #include <map> #include "dzialka.h" #include "message.h" using std::vector; //http://www.deltami.edu.pl/temat/informatyka/algorytmy/2015/10/27/Zliczamy_puste_prostokaty/ //int GetFieldHeight() {return 50000;} //int GetFieldWidth() {return 50000;} //int IsUsableCell(int r, int c) {return (r == c) ? 0 : 1;} struct Rect { int w, h; }; struct Stripe { int64_t total; vector<int> di; vector<int> Di; int height; }; int64_t process(const vector<int>& di) { int64_t total = 0; std::stack< Rect, vector<Rect> > s; for (auto const &dij : di) { int w = 0; while (!s.empty() && s.top().h >= dij) { Rect sm = s.top(); s.pop(); int h = dij; if (!s.empty()) { if (h < s.top().h) h = s.top().h; } w += sm.w; int64_t part = w * int64_t(w + 1) / 2 * (sm.h - h); total += part; } if (dij > 0) { if (s.empty() || s.top().h < dij) { s.push({w + 1, dij}); } } } return total; } Stripe delta(int iBegin, int iEnd) { const int width = GetFieldWidth(); Stripe result; result.total = 0; result.di.resize(width + 2); result.Di.resize(width + 2); result.height = iEnd - iBegin; for (int i = iBegin; i < iEnd; i++) { for (int j = 0; j < width; j++) { bool usable = IsUsableCell(i, j) == 1; if (usable) { result.di[j + 1]++; } else { result.di[j + 1] = 0; } if (result.Di[j + 1] == (i - iBegin) && usable) { result.Di[j + 1]++; } } result.total += process(result.di); } return result; } int64_t process2(const std::map<int,int>& diComp) { int64_t total = 0; std::stack< Rect, vector<Rect> > s; auto i1 = diComp.begin(); auto i2 = diComp.begin(); int dij = 0; for (i2++; i1 != diComp.end(); ++i1, ++i2) { dij += i1->second; int cnt = 1; if (i2 != diComp.end()) cnt = i2->first - i1->first; //std::cout << "C: "<< val << " " << cnt << std::endl; int w = 0; while (!s.empty() && s.top().h >= dij) { Rect sm = s.top(); s.pop(); int h = dij; if (!s.empty()) { if (h < s.top().h) h = s.top().h; } w += sm.w; int64_t part = w * int64_t(w + 1) / 2 * (sm.h - h); total += part; } if (dij > 0) { //if (s.empty() || s.top().h < dij) { s.push({w + cnt, dij}); //} } } return total; } int64_t merge_util(const vector<int>& ups, const vector<int>& dns) { int64_t total = 0; std::map<int,int> diComp0; for (int i=0; i < ups.size(); i++) { int zerosEnd = -ups[i]; int incEnd = +dns[i]; int sumBeg = 0; auto it = diComp0.begin(); for (; it != diComp0.end() && it->first <= zerosEnd; ++it) { sumBeg += it->second; } diComp0.erase(diComp0.begin(), it); if (sumBeg + 1 != 0) diComp0[zerosEnd] = sumBeg + 1; auto newEnd = diComp0.lower_bound(incEnd); int sumEnd = 0; for (auto it = newEnd; it != diComp0.end(); ++it) { sumEnd += it->second; } diComp0.erase(newEnd, diComp0.end()); if (sumEnd - 1 != 0) diComp0[incEnd] = sumEnd - 1; if (!diComp0.empty()) { total += process2(diComp0); } } return total; } int64_t mergeCnt(const vector<int>& a, const vector<int>& b) { // shit... I'm too old & stupid to implement it correctly in O(n) - let's go bruteforce int64_t AB = merge_util(a, b); int s = a.size(); int64_t A = merge_util(a, vector<int>(s)); int64_t B = merge_util(b, vector<int>(s)); return AB - A - B; } const int every_n = 2000; void sendStripes(int node, const vector<int>& a) { for (int i = 0; i < a.size(); i++) { PutInt(node, a[i]); if (!((i + 1) % every_n) && i || i == a.size() - 1) { Send(node); } } } vector<int> getStripes(int node, int size) { vector<int> a(size); for (int i = 0; i < size; i++) { if (i % every_n == 0) { Receive(node); } a[i] = GetInt(node); } return a; } void merge(vector<Stripe>& stripes) { int64_t total = 0; //#define LOCAL //propagate closings #ifdef LOCAL for (int i = stripes.size() - 2; i >= 0; i--) { #else const int node = MyNodeId(); const int nodes = NumberOfNodes(); int i = node; if (node < nodes - 1) { stripes[i+1].Di = getStripes(node + 1, stripes[i].Di.size()); #endif for (int j = 0; j < stripes[i].Di.size(); j++) { if (stripes[i].Di[j] == stripes[i].height) { stripes[i].Di[j] += stripes[i+1].Di[j]; } } } #ifndef LOCAL if (node > 0) { sendStripes(node - 1, stripes[i].Di); } #endif #ifdef LOCAL for (int i=0; i<stripes.size()-1; i++) { #else if (node < nodes - 1) { #endif // LOCAL vector<int> opening = stripes[i].di; vector<int> closing = stripes[i+1].Di; for (int j=0; j<opening.size(); j++) { if (opening[j] == 0 || closing[j] == 0) { opening[j] = 0; closing[j] = 0; } } int64_t merged = mergeCnt(opening, closing); total += merged; } #ifdef LOCAL for (int i=0; i < stripes.size(); i++) { #endif // LOCAL total += stripes[i].total; #ifdef LOCAL } #endif // LOCAL if (node < nodes - 1) { PutLL(nodes - 1, total); Send(nodes - 1); } else { for (int i = 1; i < NumberOfNodes(); ++i) { int instancja = Receive(-1); int64_t subTotal = GetLL(instancja); total += subTotal; } std::cout << total << std::endl; } } int main() { //freopen("in/6.in", "r", stdin); const int height = GetFieldHeight(); if (height < 5000) { //single instance should handle 5k, need to check correctness if (MyNodeId() == 0) { Stripe full = delta(0, height); std::cout << full.total << std::endl; } return 0; } int nodes = NumberOfNodes(); vector<Stripe> stripes(nodes); //for (int node=0; node < nodes; node++) { //MyNodeId() const int node = MyNodeId(); int poczatek = (node * height) / nodes; int koniec = ((node + 1) * height) / nodes; stripes[node] = delta(poczatek, koniec); } merge(stripes); }
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 | #include <iostream> #include <vector> #include <stack> #include <cstdio> #include <algorithm> #include <map> #include "dzialka.h" #include "message.h" using std::vector; //http://www.deltami.edu.pl/temat/informatyka/algorytmy/2015/10/27/Zliczamy_puste_prostokaty/ //int GetFieldHeight() {return 50000;} //int GetFieldWidth() {return 50000;} //int IsUsableCell(int r, int c) {return (r == c) ? 0 : 1;} struct Rect { int w, h; }; struct Stripe { int64_t total; vector<int> di; vector<int> Di; int height; }; int64_t process(const vector<int>& di) { int64_t total = 0; std::stack< Rect, vector<Rect> > s; for (auto const &dij : di) { int w = 0; while (!s.empty() && s.top().h >= dij) { Rect sm = s.top(); s.pop(); int h = dij; if (!s.empty()) { if (h < s.top().h) h = s.top().h; } w += sm.w; int64_t part = w * int64_t(w + 1) / 2 * (sm.h - h); total += part; } if (dij > 0) { if (s.empty() || s.top().h < dij) { s.push({w + 1, dij}); } } } return total; } Stripe delta(int iBegin, int iEnd) { const int width = GetFieldWidth(); Stripe result; result.total = 0; result.di.resize(width + 2); result.Di.resize(width + 2); result.height = iEnd - iBegin; for (int i = iBegin; i < iEnd; i++) { for (int j = 0; j < width; j++) { bool usable = IsUsableCell(i, j) == 1; if (usable) { result.di[j + 1]++; } else { result.di[j + 1] = 0; } if (result.Di[j + 1] == (i - iBegin) && usable) { result.Di[j + 1]++; } } result.total += process(result.di); } return result; } int64_t process2(const std::map<int,int>& diComp) { int64_t total = 0; std::stack< Rect, vector<Rect> > s; auto i1 = diComp.begin(); auto i2 = diComp.begin(); int dij = 0; for (i2++; i1 != diComp.end(); ++i1, ++i2) { dij += i1->second; int cnt = 1; if (i2 != diComp.end()) cnt = i2->first - i1->first; //std::cout << "C: "<< val << " " << cnt << std::endl; int w = 0; while (!s.empty() && s.top().h >= dij) { Rect sm = s.top(); s.pop(); int h = dij; if (!s.empty()) { if (h < s.top().h) h = s.top().h; } w += sm.w; int64_t part = w * int64_t(w + 1) / 2 * (sm.h - h); total += part; } if (dij > 0) { //if (s.empty() || s.top().h < dij) { s.push({w + cnt, dij}); //} } } return total; } int64_t merge_util(const vector<int>& ups, const vector<int>& dns) { int64_t total = 0; std::map<int,int> diComp0; for (int i=0; i < ups.size(); i++) { int zerosEnd = -ups[i]; int incEnd = +dns[i]; int sumBeg = 0; auto it = diComp0.begin(); for (; it != diComp0.end() && it->first <= zerosEnd; ++it) { sumBeg += it->second; } diComp0.erase(diComp0.begin(), it); if (sumBeg + 1 != 0) diComp0[zerosEnd] = sumBeg + 1; auto newEnd = diComp0.lower_bound(incEnd); int sumEnd = 0; for (auto it = newEnd; it != diComp0.end(); ++it) { sumEnd += it->second; } diComp0.erase(newEnd, diComp0.end()); if (sumEnd - 1 != 0) diComp0[incEnd] = sumEnd - 1; if (!diComp0.empty()) { total += process2(diComp0); } } return total; } int64_t mergeCnt(const vector<int>& a, const vector<int>& b) { // shit... I'm too old & stupid to implement it correctly in O(n) - let's go bruteforce int64_t AB = merge_util(a, b); int s = a.size(); int64_t A = merge_util(a, vector<int>(s)); int64_t B = merge_util(b, vector<int>(s)); return AB - A - B; } const int every_n = 2000; void sendStripes(int node, const vector<int>& a) { for (int i = 0; i < a.size(); i++) { PutInt(node, a[i]); if (!((i + 1) % every_n) && i || i == a.size() - 1) { Send(node); } } } vector<int> getStripes(int node, int size) { vector<int> a(size); for (int i = 0; i < size; i++) { if (i % every_n == 0) { Receive(node); } a[i] = GetInt(node); } return a; } void merge(vector<Stripe>& stripes) { int64_t total = 0; //#define LOCAL //propagate closings #ifdef LOCAL for (int i = stripes.size() - 2; i >= 0; i--) { #else const int node = MyNodeId(); const int nodes = NumberOfNodes(); int i = node; if (node < nodes - 1) { stripes[i+1].Di = getStripes(node + 1, stripes[i].Di.size()); #endif for (int j = 0; j < stripes[i].Di.size(); j++) { if (stripes[i].Di[j] == stripes[i].height) { stripes[i].Di[j] += stripes[i+1].Di[j]; } } } #ifndef LOCAL if (node > 0) { sendStripes(node - 1, stripes[i].Di); } #endif #ifdef LOCAL for (int i=0; i<stripes.size()-1; i++) { #else if (node < nodes - 1) { #endif // LOCAL vector<int> opening = stripes[i].di; vector<int> closing = stripes[i+1].Di; for (int j=0; j<opening.size(); j++) { if (opening[j] == 0 || closing[j] == 0) { opening[j] = 0; closing[j] = 0; } } int64_t merged = mergeCnt(opening, closing); total += merged; } #ifdef LOCAL for (int i=0; i < stripes.size(); i++) { #endif // LOCAL total += stripes[i].total; #ifdef LOCAL } #endif // LOCAL if (node < nodes - 1) { PutLL(nodes - 1, total); Send(nodes - 1); } else { for (int i = 1; i < NumberOfNodes(); ++i) { int instancja = Receive(-1); int64_t subTotal = GetLL(instancja); total += subTotal; } std::cout << total << std::endl; } } int main() { //freopen("in/6.in", "r", stdin); const int height = GetFieldHeight(); if (height < 5000) { //single instance should handle 5k, need to check correctness if (MyNodeId() == 0) { Stripe full = delta(0, height); std::cout << full.total << std::endl; } return 0; } int nodes = NumberOfNodes(); vector<Stripe> stripes(nodes); //for (int node=0; node < nodes; node++) { //MyNodeId() const int node = MyNodeId(); int poczatek = (node * height) / nodes; int koniec = ((node + 1) * height) / nodes; stripes[node] = delta(poczatek, koniec); } merge(stripes); } |