#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include "dzialka.h" #include "message.h" using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ll = long long; using ull = long long; const int INF = 1e9; const int MASTER = 0; const int N = 75000 + 10; const int M = N; const int MAXL = 75000/100 + 10; //const int MAXL = 100; bool a[MAXL][M]; int sum[M]; int sumGood[M]; int up[M]; int upGood[M]; int tmp[M]; const int CHUNK = 10000; ull countSubgrids(ull x, ull y) { return x*(x+1)/2 * y; } struct Entry { ull x; ull height; ull deltaY; ull accX; Entry() {} Entry(ull x_, ull height_, ull deltaY_, ull accX_) : x(x_), height(height_), deltaY(deltaY_), accX(accX_) { } }; int main() { ios_base::sync_with_stdio(0); const int id = MyNodeId(); const int n = GetFieldHeight(); const int m = GetFieldWidth(); const int k = min(n, NumberOfNodes()); //don't need such instances if (id >= k) return 0; const int blockLen = ceil(n / (double)k); const ll A = id*blockLen; const ll B = min(n-1, (id+1)*blockLen-1); const ll L = B-A+1; /* reading values in my range */ for (int i = A; i <= B; ++i) { for (int j = 0; j < m; ++j) { a[i-A][j] = IsUsableCell(i, j) == 1; } } /* initialize sum[m] and sumGood[m] arrays */ for (int j = 0; j < m; ++j) { for (int i = L-1; i >= 0 and a[i][j]; --i) { sum[j]++; } sumGood[j] = sum[j] == L; } // initializing upGood[m] array for (int j = 0; j < m; ++j) { upGood[j] = true; } // precomputing prefix sums in parallel int step = 1; int wantedPrefSz = blockLen; while (id+step < k or id-step >= 0) { if (id+step < k) { int recipient = id+step; for (int j = 0; j < m; ++j) { if (j > 0 and j % CHUNK == 0) { Send(recipient); } PutInt(recipient, sum[j]); } Send(recipient); } if (id-step >= 0) { int sender = id-step; int cnt = 0; for (int j = 0; j < m; ++j) { if (j % CHUNK == 0) { Receive(sender); } tmp[j] = GetInt(sender); } // merge tmp array into up array, and up array into sum array for (int j = 0; j < m; ++j) { if (upGood[j]) { up[j] += tmp[j]; if (up[j] < wantedPrefSz) { upGood[j] = false; } if (sumGood[j]) { sum[j] += tmp[j]; } } } } step *= 2; wantedPrefSz += step*blockLen; } // computing local result ull result = 0; for (int i = 0; i < L; ++i) { ull rowResult = 0; for (int j = 0; j < m; ++j) { if (a[i][j]) { if (i == 0) { tmp[j] = up[j] + 1; } else { tmp[j]++; } } else { tmp[j] = 0; } } stack<Entry> q; for (int j = 0; j < m; ++j) { int h = tmp[j]; ull accX = 0; while (!q.empty() and q.top().height > h) { ull a = j - q.top().x; ull b = q.top().deltaY; accX = max(accX, a+q.top().accX); rowResult += countSubgrids(a, b); rowResult += (q.top().accX * a) * b; q.pop(); } ull deltaY = q.empty() ? h : h-q.top().height; if (deltaY > 0) { q.push(Entry(j, h, deltaY, accX)); } } while (!q.empty()) { ull a = m - q.top().x; ull b = q.top().deltaY; rowResult += countSubgrids(a, b); rowResult += (q.top().accX * a) * b; q.pop(); } result += rowResult; } if (id != MASTER) { PutLL(MASTER, result); Send(MASTER); } else { for (int sender = 0; sender < k; ++sender) { if (sender == MASTER) continue; Receive(sender); ll subresult = GetLL(sender); result += subresult; } cout << result << endl; } 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 <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include "dzialka.h" #include "message.h" using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ll = long long; using ull = long long; const int INF = 1e9; const int MASTER = 0; const int N = 75000 + 10; const int M = N; const int MAXL = 75000/100 + 10; //const int MAXL = 100; bool a[MAXL][M]; int sum[M]; int sumGood[M]; int up[M]; int upGood[M]; int tmp[M]; const int CHUNK = 10000; ull countSubgrids(ull x, ull y) { return x*(x+1)/2 * y; } struct Entry { ull x; ull height; ull deltaY; ull accX; Entry() {} Entry(ull x_, ull height_, ull deltaY_, ull accX_) : x(x_), height(height_), deltaY(deltaY_), accX(accX_) { } }; int main() { ios_base::sync_with_stdio(0); const int id = MyNodeId(); const int n = GetFieldHeight(); const int m = GetFieldWidth(); const int k = min(n, NumberOfNodes()); //don't need such instances if (id >= k) return 0; const int blockLen = ceil(n / (double)k); const ll A = id*blockLen; const ll B = min(n-1, (id+1)*blockLen-1); const ll L = B-A+1; /* reading values in my range */ for (int i = A; i <= B; ++i) { for (int j = 0; j < m; ++j) { a[i-A][j] = IsUsableCell(i, j) == 1; } } /* initialize sum[m] and sumGood[m] arrays */ for (int j = 0; j < m; ++j) { for (int i = L-1; i >= 0 and a[i][j]; --i) { sum[j]++; } sumGood[j] = sum[j] == L; } // initializing upGood[m] array for (int j = 0; j < m; ++j) { upGood[j] = true; } // precomputing prefix sums in parallel int step = 1; int wantedPrefSz = blockLen; while (id+step < k or id-step >= 0) { if (id+step < k) { int recipient = id+step; for (int j = 0; j < m; ++j) { if (j > 0 and j % CHUNK == 0) { Send(recipient); } PutInt(recipient, sum[j]); } Send(recipient); } if (id-step >= 0) { int sender = id-step; int cnt = 0; for (int j = 0; j < m; ++j) { if (j % CHUNK == 0) { Receive(sender); } tmp[j] = GetInt(sender); } // merge tmp array into up array, and up array into sum array for (int j = 0; j < m; ++j) { if (upGood[j]) { up[j] += tmp[j]; if (up[j] < wantedPrefSz) { upGood[j] = false; } if (sumGood[j]) { sum[j] += tmp[j]; } } } } step *= 2; wantedPrefSz += step*blockLen; } // computing local result ull result = 0; for (int i = 0; i < L; ++i) { ull rowResult = 0; for (int j = 0; j < m; ++j) { if (a[i][j]) { if (i == 0) { tmp[j] = up[j] + 1; } else { tmp[j]++; } } else { tmp[j] = 0; } } stack<Entry> q; for (int j = 0; j < m; ++j) { int h = tmp[j]; ull accX = 0; while (!q.empty() and q.top().height > h) { ull a = j - q.top().x; ull b = q.top().deltaY; accX = max(accX, a+q.top().accX); rowResult += countSubgrids(a, b); rowResult += (q.top().accX * a) * b; q.pop(); } ull deltaY = q.empty() ? h : h-q.top().height; if (deltaY > 0) { q.push(Entry(j, h, deltaY, accX)); } } while (!q.empty()) { ull a = m - q.top().x; ull b = q.top().deltaY; rowResult += countSubgrids(a, b); rowResult += (q.top().accX * a) * b; q.pop(); } result += rowResult; } if (id != MASTER) { PutLL(MASTER, result); Send(MASTER); } else { for (int sender = 0; sender < k; ++sender) { if (sender == MASTER) continue; Receive(sender); ll subresult = GetLL(sender); result += subresult; } cout << result << endl; } return 0; } |