#include <bits/stdc++.h> #include "dzialka.h" #include "message.h" using namespace std; long long licz(int *h, int n) { long long res = 0; vector <pair <int, int>> S; S.push_back({-1, 0}); h[n] = 0; for(int i = 0; i <= n; ++i) { int x = i; while(!S.empty() && S.back().second >= h[i]) { auto u = S.back(); S.pop_back(); res += (long long) (i - u.first) * (i - u.first + 1) * (u.second - max(h[i], S.back().second)); x = u.first; } S.push_back({x, h[i]}); } return res / 2; } int N; int pcz[107]; int kon[107]; int n, m; int id; vector <bool> jed[107]; const int L = 75000; int h[L + 7]; int ho[L + 7]; int main() { id = MyNodeId(); N = NumberOfNodes(); n = GetFieldHeight(); m = GetFieldWidth(); int il = (n + N - 1) / N; pcz[0] = 0; kon[0] = il - 1; for(int i = 1; i < N; ++i) { pcz[i] = kon[i - 1] + 1; kon[i] = pcz[i] + il - 1; } int nN = 0; for(int i = 0; i < N; ++i) { kon[i] = min(kon[i], n - 1); if(pcz[i] <= kon[i]) nN = i + 1; } N = nN; if(id >= N) return 0; vector <char> tt; char tobuf; for(int j = 0; j < m; ++j) { bool jedynki = 1; for(int i = pcz[id]; i <= kon[id] && jedynki; ++i) { if(!IsUsableCell(i, j)) jedynki = 0; } if(j != 0 && (j % 8) == 0) { tt.push_back(tobuf); tobuf = 0; } if(jedynki) tobuf |= (1 << (j % 8)); } tt.push_back(tobuf); for(int i = id + 1; i < N; ++i) { for(auto u : tt) PutChar(i, u); Send(i); } for(int i = 0; i < id; ++i) { jed[i].resize(m); Receive(i); int dlu = (m + 7) / 8; for(int jt = 0; jt < dlu; ++jt) { char cc = GetChar(i); for(int j = 0; j < 8; ++j) if(jt * 8 + j < m) jed[i][jt * 8 + j] = bool (cc & (1 << j)); } } for(int j = 0; j < m; ++j) { ho[j] = 0; int it = id - 1; while(it >= 0) { if(jed[it][j]) { ho[j] += kon[it] - pcz[it] + 1; it--; } else break; } while(pcz[id] - ho[j] - 1 >= 0 && IsUsableCell(pcz[id] - ho[j] - 1, j)) ho[j]++; } unsigned long long wynik = 0; for(int i = pcz[id]; i <= kon[id]; ++i) { for(int j = 0; j < m; ++j) { if(IsUsableCell(i, j)) h[j] = ho[j] + 1; else h[j] = 0; } wynik += licz(h, m); for(int j = 0; j < m; ++j) ho[j] = h[j]; } if(id == 0) { for(int i = 1; i < N; ++i) { Receive(i); wynik += GetLL(i); } cout << wynik << endl; } else { PutLL(0, wynik); 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 | #include <bits/stdc++.h> #include "dzialka.h" #include "message.h" using namespace std; long long licz(int *h, int n) { long long res = 0; vector <pair <int, int>> S; S.push_back({-1, 0}); h[n] = 0; for(int i = 0; i <= n; ++i) { int x = i; while(!S.empty() && S.back().second >= h[i]) { auto u = S.back(); S.pop_back(); res += (long long) (i - u.first) * (i - u.first + 1) * (u.second - max(h[i], S.back().second)); x = u.first; } S.push_back({x, h[i]}); } return res / 2; } int N; int pcz[107]; int kon[107]; int n, m; int id; vector <bool> jed[107]; const int L = 75000; int h[L + 7]; int ho[L + 7]; int main() { id = MyNodeId(); N = NumberOfNodes(); n = GetFieldHeight(); m = GetFieldWidth(); int il = (n + N - 1) / N; pcz[0] = 0; kon[0] = il - 1; for(int i = 1; i < N; ++i) { pcz[i] = kon[i - 1] + 1; kon[i] = pcz[i] + il - 1; } int nN = 0; for(int i = 0; i < N; ++i) { kon[i] = min(kon[i], n - 1); if(pcz[i] <= kon[i]) nN = i + 1; } N = nN; if(id >= N) return 0; vector <char> tt; char tobuf; for(int j = 0; j < m; ++j) { bool jedynki = 1; for(int i = pcz[id]; i <= kon[id] && jedynki; ++i) { if(!IsUsableCell(i, j)) jedynki = 0; } if(j != 0 && (j % 8) == 0) { tt.push_back(tobuf); tobuf = 0; } if(jedynki) tobuf |= (1 << (j % 8)); } tt.push_back(tobuf); for(int i = id + 1; i < N; ++i) { for(auto u : tt) PutChar(i, u); Send(i); } for(int i = 0; i < id; ++i) { jed[i].resize(m); Receive(i); int dlu = (m + 7) / 8; for(int jt = 0; jt < dlu; ++jt) { char cc = GetChar(i); for(int j = 0; j < 8; ++j) if(jt * 8 + j < m) jed[i][jt * 8 + j] = bool (cc & (1 << j)); } } for(int j = 0; j < m; ++j) { ho[j] = 0; int it = id - 1; while(it >= 0) { if(jed[it][j]) { ho[j] += kon[it] - pcz[it] + 1; it--; } else break; } while(pcz[id] - ho[j] - 1 >= 0 && IsUsableCell(pcz[id] - ho[j] - 1, j)) ho[j]++; } unsigned long long wynik = 0; for(int i = pcz[id]; i <= kon[id]; ++i) { for(int j = 0; j < m; ++j) { if(IsUsableCell(i, j)) h[j] = ho[j] + 1; else h[j] = 0; } wynik += licz(h, m); for(int j = 0; j < m; ++j) ho[j] = h[j]; } if(id == 0) { for(int i = 1; i < N; ++i) { Receive(i); wynik += GetLL(i); } cout << wynik << endl; } else { PutLL(0, wynik); Send(0); } return 0; } |