#include "dzialka.h" #include "message.h" // #define DEBUG #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (decltype(n) i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,(n)-1) #define FORD(i,a,n) for (decltype(a) i = (a), i##__ = (n); i >= i##__; --i) #define REPD(i,n) FORD(i,(n)-1,0) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define SZ(x) ((int)x.size()) #define RS resize #define V vector constexpr char nl = '\n'; using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VVI = V<VI>; using VPII = V<PII>; using VVPII = V<VPII>; using VB = V<bool>; using VVB = V<VB>; template<class T, class B> void mini(T &&a, B &&b) { if (b < a) a = b; } template<class T, class B> void maxi(T &&a, B &&b) { if (b > a) a = b; } int pow2(int x) { return x < 2 ? 0 : sizeof(x) * 8 - __builtin_clz(x - 1); } template<class T> struct Off { T a, b; }; template<class T, class X = typename enable_if<!is_same<string, T>::value, T>::type> auto O(T &x) -> Off<decltype(x.begin())> { return {x.begin(), x.end()}; } template<class T> auto O(T &x) -> decltype(cerr << x, x) { return x; } template<class T> auto O(T &x) -> decltype(x.first, x) { return x; } struct Debug { Debug() { cerr << '[' << MyNodeId() << "] "; } ~Debug() { cerr << nl; } template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << O(x); return *this; } template<class T> auto operator<<(T x) -> decltype(x.begin(), typename enable_if<!is_same<string, T>::value, T>::type(), *this) { cerr << "{\n"; for (auto a = x.begin(); a != x.end(); ++a) *this << " " << distance(x.begin(), a) << ": " << O(*a) << '\n'; return *this << "}"; } template<class T, class B> Debug& operator<<(pair<T, B> p) { return *this << "(" << O(p.first) << ", " << O(p.second) << ")"; } template<class T> Debug& operator<<(Off<T> d) { cerr << "{"; for (auto a = d.a, b = d.b; a != b; ++a) *this << O(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } }; struct DebugOff { template<class T> DebugOff& operator<<(T) { return *this; } }; #ifdef DEBUG # define D Debug() #else # define D DebugOff() #endif #define I(x...) #x ": " << x << " " #define TD(X, ...) ostream& operator<<(ostream &os, X &x) { __VA_ARGS__; return os; } //end of templates template<class T> struct Tree { int sz; VI t; T &merge; Tree(int n, int default_val, T &lambda) : sz(1 << pow2(n)), t(sz << 1, default_val), merge(lambda) {} void set(int v, int val) { v += sz; while(v) { t[v] = merge(t[v], val); v >>= 1; } } int get(int l, int r) { if((l += sz) > (r += sz)) return -1; int answ = merge(t[l], t[r]); while(l >> 1 != r >> 1) { if(~l & 1) answ = merge(answ, t[l^1]); if(r & 1) answ = merge(t[r^1], answ); l >>= 1; r >>= 1; } return answ; } void clear(int x) { for(int &i : t) i = x; } }; constexpr int max_mess = 15500; VI receiveVI(int n, int inst) { VI v(n); int i = 0; while(i < n) { Receive(inst); for(int j = 0; j < max_mess && j + i < n; ++j) v[i + j] = GetInt(inst); i += max_mess; } return v; } void sendVI(VI &v, int n, int inst) { int i = 0; while(i < n) { for(int j = 0; j < max_mess && j + i < n; ++j) PutInt(inst, v[i + j]); Send(inst); i += max_mess; } } int main() { // input int non = NumberOfNodes(); int id = MyNodeId(); D << "WTF1"; int m = GetFieldHeight(); int n = GetFieldWidth(); D << "WTF2"; mini(non, n); if(id >= non) return D << "died", 0; int mod = m % non; int h1 = (m / non) * id, h2 = (m / non) * (id + 1) - 1; if(mod > id) { h1 += id; h2 += id + 1; } else { h1 += mod; h2 += mod; } D << I(h1) << I(h2); m = h2 - h1 + 1; // VVI in(n, VI(m)); // REP(y, m) // REP(x, n) // in[x][y] = IsUsableCell(y + h1, x); // D << I(n) << I(m) << I(in); // building time table VI time(n, -1); REP(t, m) { REP(x, n) if(!IsUsableCell(t + h1, x)) time[x] = t + h1; } D << I(time); // sending tables VI prev; if(id) { prev = receiveVI(n, id - 1); REP(i, n) maxi(time[i], prev[i]); } else prev = VI(n, -1); D << I(prev); if(id != non-1) sendVI(time, n, id + 1); time = prev; D << I(time); // preparing trees auto lambda_l = [&](int l, int r) { return min(l, r); }; auto lambda_r = [&](int l, int r) { return max(l, r); }; Tree<decltype(lambda_l)> t_l(n, n, lambda_l); Tree<decltype(lambda_r)> t_r(n, -1, lambda_r); VPII time_sort(n); // calculating sum LL sum = 0; REP(t, m) { REP(x, n) if(!IsUsableCell(t + h1, x)) time[x] = t + h1; REP(x, n) time_sort[x] = {time[x], x}; sort(time_sort.rbegin(), time_sort.rend()); D << I(time_sort); t_l.clear(n); t_r.clear(-1); for(auto &p : time_sort) { int x = p.ND; int l = (x == n-1 ? n : t_l.get(x+1, n-1)), r = (x ? t_r.get(0, x-1) : -1); LL answ = LL(r - x) * LL(x - l) * LL(t + h1- time[p.ND]); sum += answ; t_l.set(x, x); t_r.set(x, x); D << I(t+h1) << I(x) << I(l) << I(r) << I(answ); // D << I(t_l.t) << I(t_r.t); } } // sending and receiving sum if(id) { PutLL(0, sum); Send(0); } else { FOR(i, 1, non-1) { Receive(i); sum += GetLL(i); } cout << sum << nl; } D << "died"; }
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | #include "dzialka.h" #include "message.h" // #define DEBUG #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (decltype(n) i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,(n)-1) #define FORD(i,a,n) for (decltype(a) i = (a), i##__ = (n); i >= i##__; --i) #define REPD(i,n) FORD(i,(n)-1,0) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define SZ(x) ((int)x.size()) #define RS resize #define V vector constexpr char nl = '\n'; using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VVI = V<VI>; using VPII = V<PII>; using VVPII = V<VPII>; using VB = V<bool>; using VVB = V<VB>; template<class T, class B> void mini(T &&a, B &&b) { if (b < a) a = b; } template<class T, class B> void maxi(T &&a, B &&b) { if (b > a) a = b; } int pow2(int x) { return x < 2 ? 0 : sizeof(x) * 8 - __builtin_clz(x - 1); } template<class T> struct Off { T a, b; }; template<class T, class X = typename enable_if<!is_same<string, T>::value, T>::type> auto O(T &x) -> Off<decltype(x.begin())> { return {x.begin(), x.end()}; } template<class T> auto O(T &x) -> decltype(cerr << x, x) { return x; } template<class T> auto O(T &x) -> decltype(x.first, x) { return x; } struct Debug { Debug() { cerr << '[' << MyNodeId() << "] "; } ~Debug() { cerr << nl; } template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << O(x); return *this; } template<class T> auto operator<<(T x) -> decltype(x.begin(), typename enable_if<!is_same<string, T>::value, T>::type(), *this) { cerr << "{\n"; for (auto a = x.begin(); a != x.end(); ++a) *this << " " << distance(x.begin(), a) << ": " << O(*a) << '\n'; return *this << "}"; } template<class T, class B> Debug& operator<<(pair<T, B> p) { return *this << "(" << O(p.first) << ", " << O(p.second) << ")"; } template<class T> Debug& operator<<(Off<T> d) { cerr << "{"; for (auto a = d.a, b = d.b; a != b; ++a) *this << O(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } }; struct DebugOff { template<class T> DebugOff& operator<<(T) { return *this; } }; #ifdef DEBUG # define D Debug() #else # define D DebugOff() #endif #define I(x...) #x ": " << x << " " #define TD(X, ...) ostream& operator<<(ostream &os, X &x) { __VA_ARGS__; return os; } //end of templates template<class T> struct Tree { int sz; VI t; T &merge; Tree(int n, int default_val, T &lambda) : sz(1 << pow2(n)), t(sz << 1, default_val), merge(lambda) {} void set(int v, int val) { v += sz; while(v) { t[v] = merge(t[v], val); v >>= 1; } } int get(int l, int r) { if((l += sz) > (r += sz)) return -1; int answ = merge(t[l], t[r]); while(l >> 1 != r >> 1) { if(~l & 1) answ = merge(answ, t[l^1]); if(r & 1) answ = merge(t[r^1], answ); l >>= 1; r >>= 1; } return answ; } void clear(int x) { for(int &i : t) i = x; } }; constexpr int max_mess = 15500; VI receiveVI(int n, int inst) { VI v(n); int i = 0; while(i < n) { Receive(inst); for(int j = 0; j < max_mess && j + i < n; ++j) v[i + j] = GetInt(inst); i += max_mess; } return v; } void sendVI(VI &v, int n, int inst) { int i = 0; while(i < n) { for(int j = 0; j < max_mess && j + i < n; ++j) PutInt(inst, v[i + j]); Send(inst); i += max_mess; } } int main() { // input int non = NumberOfNodes(); int id = MyNodeId(); D << "WTF1"; int m = GetFieldHeight(); int n = GetFieldWidth(); D << "WTF2"; mini(non, n); if(id >= non) return D << "died", 0; int mod = m % non; int h1 = (m / non) * id, h2 = (m / non) * (id + 1) - 1; if(mod > id) { h1 += id; h2 += id + 1; } else { h1 += mod; h2 += mod; } D << I(h1) << I(h2); m = h2 - h1 + 1; // VVI in(n, VI(m)); // REP(y, m) // REP(x, n) // in[x][y] = IsUsableCell(y + h1, x); // D << I(n) << I(m) << I(in); // building time table VI time(n, -1); REP(t, m) { REP(x, n) if(!IsUsableCell(t + h1, x)) time[x] = t + h1; } D << I(time); // sending tables VI prev; if(id) { prev = receiveVI(n, id - 1); REP(i, n) maxi(time[i], prev[i]); } else prev = VI(n, -1); D << I(prev); if(id != non-1) sendVI(time, n, id + 1); time = prev; D << I(time); // preparing trees auto lambda_l = [&](int l, int r) { return min(l, r); }; auto lambda_r = [&](int l, int r) { return max(l, r); }; Tree<decltype(lambda_l)> t_l(n, n, lambda_l); Tree<decltype(lambda_r)> t_r(n, -1, lambda_r); VPII time_sort(n); // calculating sum LL sum = 0; REP(t, m) { REP(x, n) if(!IsUsableCell(t + h1, x)) time[x] = t + h1; REP(x, n) time_sort[x] = {time[x], x}; sort(time_sort.rbegin(), time_sort.rend()); D << I(time_sort); t_l.clear(n); t_r.clear(-1); for(auto &p : time_sort) { int x = p.ND; int l = (x == n-1 ? n : t_l.get(x+1, n-1)), r = (x ? t_r.get(0, x-1) : -1); LL answ = LL(r - x) * LL(x - l) * LL(t + h1- time[p.ND]); sum += answ; t_l.set(x, x); t_r.set(x, x); D << I(t+h1) << I(x) << I(l) << I(r) << I(answ); // D << I(t_l.t) << I(t_r.t); } } // sending and receiving sum if(id) { PutLL(0, sum); Send(0); } else { FOR(i, 1, non-1) { Receive(i); sum += GetLL(i); } cout << sum << nl; } D << "died"; } |