#include<bits/stdc++.h> #define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++) #define all(v) (v).begin(), (v).end() #define SZ(v) (int)((v).size()) #define pb push_back #define ft first #define sd second typedef long long ll; typedef unsigned long long ull; typedef long double ld; const long long INF = 1e18L + 1; const int IINF = 1e9 + 1; #include "dzialka.h" #include "message.h" using namespace std; const int bufSize = 1000; int N, myId, H, W, myH; vector<vector<short int>> myGrid; vector<int> topRow; void init() { N = NumberOfNodes(); myId = MyNodeId(); H = GetFieldHeight(); W = GetFieldWidth(); int myOffset = H / N * myId; myH = H / N; if (myId == N - 1) { myH += H - H / N * N; } myGrid = vector<vector<short int>>(myH, vector<short int>(W)); rep (i, 0, myH) { rep (j, 0, W) { myGrid[i][j] = IsUsableCell(myOffset + i, j); } } topRow.resize(W); } void dp() { rep (i, 1, myH) { rep (j, 0, W) { if (myGrid[i][j]) { myGrid[i][j] = myGrid[i - 1][j] + 1; } } } } vector<int> readVector(int Id) { vector<int> V(W); rep (i, 0, W) { if (i % bufSize == 0) { Receive(Id); } V[i] = GetInt(Id); } return V; } void writeVector(int Id, vector<int> V) { rep (i, 0, W) { PutInt(Id, V[i]); if (i % bufSize == bufSize - 1 or i == W - 1) { Send(Id); } } } void talk() { if (myId > 0) { topRow = readVector(myId - 1); } vector<int> botRow(W); if (myH == 0) { botRow = topRow; } else { rep (j, 0, W) { if (myGrid[myH - 1][j] == myH) { botRow[j] = topRow[j] + myH; } else { botRow[j] = myGrid[myH - 1][j]; } } } if (myId != N - 1) { writeVector(myId + 1, botRow); } } struct st { ll h, ind, sum; }; ll calc() { ll res = 0; rep (i, 0, myH) { //cout << i << " ----------------------------------\n\n"; stack<st> S; S.push({0, -1, 0}); rep (j, 0, W) { if (myGrid[i][j] == 0) { topRow[j] = 0; } int g = topRow[j] + myGrid[i][j]; int k = j; while (g < S.top().h) { k = S.top().ind; S.pop(); } //cout << j << " " << k << " " << g << "\n"; if (g > S.top().h) { S.push({g, k, S.top().sum + (ll)S.top().h * (k - S.top().ind)}); } //cout << SZ(S) << " " << S.top().h << " " << S.top().ind << " " << S.top().sum << endl; //cout << S.top().sum << " " << (ll)g * (j - S.top().ind + 1) << endl; res += S.top().sum + (ll)g * (j - S.top().ind + 1); } } return res; } void report(ll res) { if (myId > 0) { PutLL(0, res); Send(0); } else { rep (i, 1, N) { Receive(i); res += GetLL(i); } cout << res; } } int main() { init(); dp(); talk(); ll res = calc(); report(res); }
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 | #include<bits/stdc++.h> #define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++) #define all(v) (v).begin(), (v).end() #define SZ(v) (int)((v).size()) #define pb push_back #define ft first #define sd second typedef long long ll; typedef unsigned long long ull; typedef long double ld; const long long INF = 1e18L + 1; const int IINF = 1e9 + 1; #include "dzialka.h" #include "message.h" using namespace std; const int bufSize = 1000; int N, myId, H, W, myH; vector<vector<short int>> myGrid; vector<int> topRow; void init() { N = NumberOfNodes(); myId = MyNodeId(); H = GetFieldHeight(); W = GetFieldWidth(); int myOffset = H / N * myId; myH = H / N; if (myId == N - 1) { myH += H - H / N * N; } myGrid = vector<vector<short int>>(myH, vector<short int>(W)); rep (i, 0, myH) { rep (j, 0, W) { myGrid[i][j] = IsUsableCell(myOffset + i, j); } } topRow.resize(W); } void dp() { rep (i, 1, myH) { rep (j, 0, W) { if (myGrid[i][j]) { myGrid[i][j] = myGrid[i - 1][j] + 1; } } } } vector<int> readVector(int Id) { vector<int> V(W); rep (i, 0, W) { if (i % bufSize == 0) { Receive(Id); } V[i] = GetInt(Id); } return V; } void writeVector(int Id, vector<int> V) { rep (i, 0, W) { PutInt(Id, V[i]); if (i % bufSize == bufSize - 1 or i == W - 1) { Send(Id); } } } void talk() { if (myId > 0) { topRow = readVector(myId - 1); } vector<int> botRow(W); if (myH == 0) { botRow = topRow; } else { rep (j, 0, W) { if (myGrid[myH - 1][j] == myH) { botRow[j] = topRow[j] + myH; } else { botRow[j] = myGrid[myH - 1][j]; } } } if (myId != N - 1) { writeVector(myId + 1, botRow); } } struct st { ll h, ind, sum; }; ll calc() { ll res = 0; rep (i, 0, myH) { //cout << i << " ----------------------------------\n\n"; stack<st> S; S.push({0, -1, 0}); rep (j, 0, W) { if (myGrid[i][j] == 0) { topRow[j] = 0; } int g = topRow[j] + myGrid[i][j]; int k = j; while (g < S.top().h) { k = S.top().ind; S.pop(); } //cout << j << " " << k << " " << g << "\n"; if (g > S.top().h) { S.push({g, k, S.top().sum + (ll)S.top().h * (k - S.top().ind)}); } //cout << SZ(S) << " " << S.top().h << " " << S.top().ind << " " << S.top().sum << endl; //cout << S.top().sum << " " << (ll)g * (j - S.top().ind + 1) << endl; res += S.top().sum + (ll)g * (j - S.top().ind + 1); } } return res; } void report(ll res) { if (myId > 0) { PutLL(0, res); Send(0); } else { rep (i, 1, N) { Receive(i); res += GetLL(i); } cout << res; } } int main() { init(); dp(); talk(); ll res = calc(); report(res); } |