#include <bits/stdc++.h> #include "dzialka.h" #include "message.h" using namespace std; template <class T, class U> ostream& operator<<(ostream& os, pair<T, U> p) { return os << "(" << p.first << ", " << p.second << ")"; } template<template<class, class...> class C, class... A> typename enable_if<!is_same<C<A...>, string>::value, ostream&>::type operator<<(ostream& os, C<A...> c) { auto i = c.begin(); while (i != c.end()) { os << " {"[i == c.begin()] << *i; os << ",}"[++i == c.end()]; } return os; } //#define debug(x) { cerr << #x << " = " << x << endl; } #define debug(...) {} const int N = 75 * 1000 + 5; vector<int> wys; int N_THREAD; int MY_ID; int n, m, p, k; void readFields() { N_THREAD = NumberOfNodes(); MY_ID = MyNodeId(); n = GetFieldHeight(); m = GetFieldWidth(); int ile = n / N_THREAD; p = ile * MY_ID; k = ile * (MY_ID + 1); // printf("przedzial %d %d -- %d\n", p, k, MY_ID); if (k > n || MY_ID == N_THREAD -1) k = n; wys = vector<int>(m, -1); for (int i = p; i < k; i ++) { for (int j = 0; j < m; j ++) { if(not IsUsableCell(i, j)) wys[j] = i; } } debug(wys); } namespace message { const size_t batchSize = 16 * 1000; void sendVector(int where, const vector<int>& v) { for (size_t B = 0; B * batchSize < v.size(); B ++) { for (int i = B * batchSize; i < min(v.size(), (B + 1) * batchSize); i ++) { PutInt(where, v[i]); } Send(where); } } vector<int> receiveVector(int from, size_t n) { vector<int> res; for (size_t B = 0; B * batchSize < n; B ++) { Receive(from); for (int i = B * batchSize; i < min(n, (B + 1) * batchSize); i ++) { res.push_back(GetInt(from)); } } return res; } void sendLL(int where, long long x) { PutLL(where, x); Send(where); } long long receiveLL(int from) { Receive(from); return GetLL(from); } } vector<int> drzewo(int podst, vector<int> lewy) { if (MY_ID % podst != 0) { message::sendVector(MY_ID - podst / 2, lewy); return message::receiveVector(MY_ID - podst / 2, m); } else { vector<int> prawy = vector<int>(m, -1); if ((MY_ID + podst / 2) < N_THREAD) { prawy = message::receiveVector(MY_ID + podst / 2, m); } for (int i = 0; i < lewy.size(); i ++) { prawy[i] = max(lewy[i], prawy[i]); } vector<int> prev = vector<int>(m, -1); if (podst < N_THREAD) { prev = drzewo(2 * podst, prawy); } for (int i = 0; i < lewy.size(); i ++) { lewy[i] = max(lewy[i], prev[i]); } if (MY_ID + podst / 2 < N_THREAD) { message::sendVector(MY_ID + podst / 2, lewy); } return prev; } } void sendFields() { wys = drzewo(2, wys); } void computeMatrix() { debug(MY_ID); debug("COMPUTE MATRIX"); for (int i = 0; i < wys.size(); i ++) { wys[i] = p - 1 - wys[i]; } } long long wynik = 0; void computeResult() { debug(wys); for (int i = p; i < k; i ++) { for (int j = 0; j < m; j ++) { if (IsUsableCell(i, j)) { wys[j] ++; } else { wys[j] = 0; } } stack<pair<int, int>> stos; stos.push({0, -1}); long long sum = 0; for (int j = 0; j < wys.size(); j ++) { while (stos.top().second != -1 && stos.top().first >= wys[j]) { auto pom = stos.top(); stos.pop(); sum -= (pom.second - stos.top().second) * (long long) pom.first; } sum += (j - stos.top().second) * (long long) wys[j]; // debug(i); // debug(j); // debug(sum); stos.push(make_pair(wys[j], j)); wynik += sum; } } } void sumResult(int podst, long long r) { if (MY_ID % podst != 0) { message::sendLL(MY_ID - podst / 2, r); } else { long long sum = r; if ((MY_ID + podst / 2) < N_THREAD) { sum += message::receiveLL(MY_ID + podst / 2); } if (podst < N_THREAD) { sumResult(2 * podst, sum); } else { printf("%lld\n", sum); } } } int main() { readFields(); sendFields(); computeMatrix(); computeResult(); sumResult(2, wynik); }
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 | #include <bits/stdc++.h> #include "dzialka.h" #include "message.h" using namespace std; template <class T, class U> ostream& operator<<(ostream& os, pair<T, U> p) { return os << "(" << p.first << ", " << p.second << ")"; } template<template<class, class...> class C, class... A> typename enable_if<!is_same<C<A...>, string>::value, ostream&>::type operator<<(ostream& os, C<A...> c) { auto i = c.begin(); while (i != c.end()) { os << " {"[i == c.begin()] << *i; os << ",}"[++i == c.end()]; } return os; } //#define debug(x) { cerr << #x << " = " << x << endl; } #define debug(...) {} const int N = 75 * 1000 + 5; vector<int> wys; int N_THREAD; int MY_ID; int n, m, p, k; void readFields() { N_THREAD = NumberOfNodes(); MY_ID = MyNodeId(); n = GetFieldHeight(); m = GetFieldWidth(); int ile = n / N_THREAD; p = ile * MY_ID; k = ile * (MY_ID + 1); // printf("przedzial %d %d -- %d\n", p, k, MY_ID); if (k > n || MY_ID == N_THREAD -1) k = n; wys = vector<int>(m, -1); for (int i = p; i < k; i ++) { for (int j = 0; j < m; j ++) { if(not IsUsableCell(i, j)) wys[j] = i; } } debug(wys); } namespace message { const size_t batchSize = 16 * 1000; void sendVector(int where, const vector<int>& v) { for (size_t B = 0; B * batchSize < v.size(); B ++) { for (int i = B * batchSize; i < min(v.size(), (B + 1) * batchSize); i ++) { PutInt(where, v[i]); } Send(where); } } vector<int> receiveVector(int from, size_t n) { vector<int> res; for (size_t B = 0; B * batchSize < n; B ++) { Receive(from); for (int i = B * batchSize; i < min(n, (B + 1) * batchSize); i ++) { res.push_back(GetInt(from)); } } return res; } void sendLL(int where, long long x) { PutLL(where, x); Send(where); } long long receiveLL(int from) { Receive(from); return GetLL(from); } } vector<int> drzewo(int podst, vector<int> lewy) { if (MY_ID % podst != 0) { message::sendVector(MY_ID - podst / 2, lewy); return message::receiveVector(MY_ID - podst / 2, m); } else { vector<int> prawy = vector<int>(m, -1); if ((MY_ID + podst / 2) < N_THREAD) { prawy = message::receiveVector(MY_ID + podst / 2, m); } for (int i = 0; i < lewy.size(); i ++) { prawy[i] = max(lewy[i], prawy[i]); } vector<int> prev = vector<int>(m, -1); if (podst < N_THREAD) { prev = drzewo(2 * podst, prawy); } for (int i = 0; i < lewy.size(); i ++) { lewy[i] = max(lewy[i], prev[i]); } if (MY_ID + podst / 2 < N_THREAD) { message::sendVector(MY_ID + podst / 2, lewy); } return prev; } } void sendFields() { wys = drzewo(2, wys); } void computeMatrix() { debug(MY_ID); debug("COMPUTE MATRIX"); for (int i = 0; i < wys.size(); i ++) { wys[i] = p - 1 - wys[i]; } } long long wynik = 0; void computeResult() { debug(wys); for (int i = p; i < k; i ++) { for (int j = 0; j < m; j ++) { if (IsUsableCell(i, j)) { wys[j] ++; } else { wys[j] = 0; } } stack<pair<int, int>> stos; stos.push({0, -1}); long long sum = 0; for (int j = 0; j < wys.size(); j ++) { while (stos.top().second != -1 && stos.top().first >= wys[j]) { auto pom = stos.top(); stos.pop(); sum -= (pom.second - stos.top().second) * (long long) pom.first; } sum += (j - stos.top().second) * (long long) wys[j]; // debug(i); // debug(j); // debug(sum); stos.push(make_pair(wys[j], j)); wynik += sum; } } } void sumResult(int podst, long long r) { if (MY_ID % podst != 0) { message::sendLL(MY_ID - podst / 2, r); } else { long long sum = r; if ((MY_ID + podst / 2) < N_THREAD) { sum += message::receiveLL(MY_ID + podst / 2); } if (podst < N_THREAD) { sumResult(2 * podst, sum); } else { printf("%lld\n", sum); } } } int main() { readFields(); sendFields(); computeMatrix(); computeResult(); sumResult(2, wynik); } |