#include "dzialka.h" #include "message.h" #include <iostream> #include <stack> #include <queue> //#include <cassert> using namespace std; int NumRows; int NumCols; #define MAX_COLS 75002 int g[MAX_COLS]; struct t_rect { int i, j; }; class t_rect_cmp { public: bool operator()(const t_rect *l, const t_rect *r) { return l->j > r->j; } }; stack<int> st; priority_queue< t_rect*, vector<t_rect*>, t_rect_cmp > q; t_rect rect_tab[MAX_COLS]; void calcG(int i) { int j; if (i == 0) { for(j = 0; j < NumCols; ++j) { g[j] = IsUsableCell(0, j); } g[NumCols] = 0; return; } for(j = 0; j < NumCols; ++j) { if (IsUsableCell(i, j)) { g[j]++; } else { g[j] = 0; } } } inline long long calcRect(int j1, int i1, int j2, int i2) { return (1LL+j2-j1)*(2LL+j2-j1)/2LL*(1LL+i2-i1); } long long calcRow(int i) { long long res=0; int j; bool popped; int first_free_rect; // assert(st.empty()); for(j = 0; j <= NumCols; ++j) { int col; int h=0; popped = false; first_free_rect = 0; while (!st.empty() && st.top() > g[j]) { // rect [col, i - h+1] x [j-1, i] auto rect = &rect_tab[first_free_rect]; first_free_rect++; h = st.top(); st.pop(); col = st.top(); st.pop(); rect->j = col; rect->i = i - h + 1; //cout << "pop from stack: [" << col << "," << h << "]\n"; //cout << "(" << i << ", " << j << ") - " << "push [" << rect->j << "," << rect->i << "]x[" << j-1 << "," << i << "] = " << endl; q.push(rect); popped = true; if (!st.empty()) { h = st.top(); } } if ((st.empty() && g[j] > 0) || (h < g[j])) { if (popped) { st.push(col); st.push(g[j]); //cout << "push on stack: [" << col << "," << g[j] << "]" << endl; } else { st.push(j); st.push(g[j]); //cout << "push on stack: [" << j << "," << g[j] << "]" << endl; } } int bottom_i = i - g[j]; while(!q.empty()) { auto rect = q.top(); q.pop(); long long r = calcRect( rect->j, rect->i, j-1, bottom_i); //cout << "bottom i=" << bottom_i << endl; //cout << "(" << i << ", " << j << ") - " << "rect [" << rect->j << "," << rect->i << "]x[" << j-1 << "," << bottom_i << "] = " << r << endl; bottom_i = rect->i-1; res += r; //delete rect; } } return res; } int main() { int i; long long result = 0; NumRows = GetFieldHeight(); NumCols = GetFieldWidth(); /* if (NumRows < NumberOfNodes()) { if (MyNodeId() > 0) { return 0; } // mynode == 0 for(i = 0; i < NumRows; ++i) { calcG(i); result += calcRow(i); } cout << result << endl; return 0; } */ // int line_from, line_to; // line_from = MyNodeId()*NumRows/NumberOfNodes(); // line_to = (MyNodeId()+1)*NumRows/NumberOfNodes()-1; // if (MyNodeId() == NumberOfNodes()-1) { // line_to = NumRows-1; // } //cout << "Robie linie od " << line_from << " do " << line_to << endl; for(i = 0; i < NumRows; ++i) { calcG(i); // if (i >= line_from && i <= line_to) { // result += calcRow(i); // } if (i % NumberOfNodes() == MyNodeId()) { result += calcRow(i); } } if (MyNodeId() > 0) { PutLL(0, result); Send(0); } else { for(i = 1; i < NumberOfNodes(); ++i) { Receive(i); result += GetLL(i); } cout << result << endl; } //cin >> NumRows; //cin >> NumCols; /* int ncols = NumCols+1; field = new int*[NumRows]; for(i = 0; i < NumRows; ++i) { field[i] = new int[ncols]; } for(i = 0; i < NumRows; ++i) { for(j = 0; j < NumCols; ++j) { char c; cin >> c; field[i][j] = c - '0'; // cout << "(" << i << "," << j << ") = " << field[i][j] << endl; } } */ // calcG(); // printField(); // printG(); 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 | #include "dzialka.h" #include "message.h" #include <iostream> #include <stack> #include <queue> //#include <cassert> using namespace std; int NumRows; int NumCols; #define MAX_COLS 75002 int g[MAX_COLS]; struct t_rect { int i, j; }; class t_rect_cmp { public: bool operator()(const t_rect *l, const t_rect *r) { return l->j > r->j; } }; stack<int> st; priority_queue< t_rect*, vector<t_rect*>, t_rect_cmp > q; t_rect rect_tab[MAX_COLS]; void calcG(int i) { int j; if (i == 0) { for(j = 0; j < NumCols; ++j) { g[j] = IsUsableCell(0, j); } g[NumCols] = 0; return; } for(j = 0; j < NumCols; ++j) { if (IsUsableCell(i, j)) { g[j]++; } else { g[j] = 0; } } } inline long long calcRect(int j1, int i1, int j2, int i2) { return (1LL+j2-j1)*(2LL+j2-j1)/2LL*(1LL+i2-i1); } long long calcRow(int i) { long long res=0; int j; bool popped; int first_free_rect; // assert(st.empty()); for(j = 0; j <= NumCols; ++j) { int col; int h=0; popped = false; first_free_rect = 0; while (!st.empty() && st.top() > g[j]) { // rect [col, i - h+1] x [j-1, i] auto rect = &rect_tab[first_free_rect]; first_free_rect++; h = st.top(); st.pop(); col = st.top(); st.pop(); rect->j = col; rect->i = i - h + 1; //cout << "pop from stack: [" << col << "," << h << "]\n"; //cout << "(" << i << ", " << j << ") - " << "push [" << rect->j << "," << rect->i << "]x[" << j-1 << "," << i << "] = " << endl; q.push(rect); popped = true; if (!st.empty()) { h = st.top(); } } if ((st.empty() && g[j] > 0) || (h < g[j])) { if (popped) { st.push(col); st.push(g[j]); //cout << "push on stack: [" << col << "," << g[j] << "]" << endl; } else { st.push(j); st.push(g[j]); //cout << "push on stack: [" << j << "," << g[j] << "]" << endl; } } int bottom_i = i - g[j]; while(!q.empty()) { auto rect = q.top(); q.pop(); long long r = calcRect( rect->j, rect->i, j-1, bottom_i); //cout << "bottom i=" << bottom_i << endl; //cout << "(" << i << ", " << j << ") - " << "rect [" << rect->j << "," << rect->i << "]x[" << j-1 << "," << bottom_i << "] = " << r << endl; bottom_i = rect->i-1; res += r; //delete rect; } } return res; } int main() { int i; long long result = 0; NumRows = GetFieldHeight(); NumCols = GetFieldWidth(); /* if (NumRows < NumberOfNodes()) { if (MyNodeId() > 0) { return 0; } // mynode == 0 for(i = 0; i < NumRows; ++i) { calcG(i); result += calcRow(i); } cout << result << endl; return 0; } */ // int line_from, line_to; // line_from = MyNodeId()*NumRows/NumberOfNodes(); // line_to = (MyNodeId()+1)*NumRows/NumberOfNodes()-1; // if (MyNodeId() == NumberOfNodes()-1) { // line_to = NumRows-1; // } //cout << "Robie linie od " << line_from << " do " << line_to << endl; for(i = 0; i < NumRows; ++i) { calcG(i); // if (i >= line_from && i <= line_to) { // result += calcRow(i); // } if (i % NumberOfNodes() == MyNodeId()) { result += calcRow(i); } } if (MyNodeId() > 0) { PutLL(0, result); Send(0); } else { for(i = 1; i < NumberOfNodes(); ++i) { Receive(i); result += GetLL(i); } cout << result << endl; } //cin >> NumRows; //cin >> NumCols; /* int ncols = NumCols+1; field = new int*[NumRows]; for(i = 0; i < NumRows; ++i) { field[i] = new int[ncols]; } for(i = 0; i < NumRows; ++i) { for(j = 0; j < NumCols; ++j) { char c; cin >> c; field[i][j] = c - '0'; // cout << "(" << i << "," << j << ") = " << field[i][j] << endl; } } */ // calcG(); // printField(); // printG(); return 0; } |