#include "message.h" #include "dzialka.h" #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) #define PER(i,n) for(int i=(n)-1; i>=0; --i) #define FOR(i,a,b) for(int i=(a); i<int(b); ++i) #define ROF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define ALL(c) (c).begin(), (c).end() ll randll() { ll r = 0; REP(i,5) { r ^= rand(); r <<= 15; } return abs(r); } ll rand(ll a, ll b) { return a + randll()%(b-a); } #define DBG false const int MAX_H = 750 * 1000 + 10; int last[MAX_H]; struct Point { int y; int x; }; Point points[MAX_H+10]; int num_points = 0; int NUM_NODES = NumberOfNodes(); const int ME = MyNodeId(); const int H = GetFieldHeight(); const int W = GetFieldWidth(); inline int get_x0(int node) { return node * W / NUM_NODES; } inline int get_x1(int node) { int x1 = (node+1) * W / NUM_NODES; if(node == NUM_NODES-1) x1 = W; return x1; } inline int get_y0(int node) { return node * H / NUM_NODES; } inline int get_y1(int node) { int y1 = (node+1) * H / NUM_NODES; if(node == NUM_NODES-1) y1 = H; return y1; } int main() { if(NUM_NODES > W) { NUM_NODES = W; } if(ME >= NUM_NODES) return 0; int x0 = get_x0(ME); int x1 = get_x1(ME); // compute last element in each row int y0 = get_y0(ME); int y1 = get_y1(ME); FOR(y,y0,y1) { int curr_last = -1; REP(cnode, NUM_NODES-1) { int xx0 = get_x0(cnode); int xx1 = get_x1(cnode); FOR(x, xx0, xx1) { if(!IsUsableCell(y,x)) curr_last = x; } PutInt(cnode+1, curr_last); } } if(y0 < y1) REP(cnode, NUM_NODES-1) { Send(cnode+1); } if(ME == 0) { REP(y,H) last[y] = -1; } else { REP(cnode, NUM_NODES) { Receive(cnode); int yy0 = get_y0(cnode); int yy1 = get_y1(cnode); FOR(y, yy0, yy1) { last[y] = GetInt(cnode); } } } if(DBG) { fprintf(stderr, "INPUT:\n"); REP(y,H) { REP(x,W) { fprintf(stderr, "%d", IsUsableCell(y,x)); } fprintf(stderr, "\n"); } } // now `last` has valid values ll total = 0; vector<ll> dbg; // dp FOR(x, x0, x1) { ll val = 0; num_points = 1; points[0] = {-1,x}; REP(y,H) { if(IsUsableCell(y,x)) { while(points[num_points-1].x <= last[y]) { auto diff = 1LL * (last[y] - points[num_points-1].x) * (points[num_points-1].y - points[num_points-2].y); val -= diff; --num_points; } val += x - last[y]; points[num_points++] = {y, last[y]}; } else { val = 0; last[y] = x; num_points = 1; points[0] = {y, x}; } //fprintf(stderr, "result[%d][%d] == %lld\n", y, x, val); //fprintf(stderr, "%lld\t", val); if(DBG) dbg.push_back(val); total += val; } //fprintf(stderr, "\n"); } if(ME == 0) { FOR(i, 1, NUM_NODES) { Receive(i); total += GetLL(i); } printf("%lld", total); if(DBG) { REP(y,H) { REP(x,W) { fprintf(stderr, "%lld\t", dbg[x*H + y]); } fprintf(stderr, "\n"); } } } else { PutLL(0, total); Send(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 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | #include "message.h" #include "dzialka.h" #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) #define PER(i,n) for(int i=(n)-1; i>=0; --i) #define FOR(i,a,b) for(int i=(a); i<int(b); ++i) #define ROF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define ALL(c) (c).begin(), (c).end() ll randll() { ll r = 0; REP(i,5) { r ^= rand(); r <<= 15; } return abs(r); } ll rand(ll a, ll b) { return a + randll()%(b-a); } #define DBG false const int MAX_H = 750 * 1000 + 10; int last[MAX_H]; struct Point { int y; int x; }; Point points[MAX_H+10]; int num_points = 0; int NUM_NODES = NumberOfNodes(); const int ME = MyNodeId(); const int H = GetFieldHeight(); const int W = GetFieldWidth(); inline int get_x0(int node) { return node * W / NUM_NODES; } inline int get_x1(int node) { int x1 = (node+1) * W / NUM_NODES; if(node == NUM_NODES-1) x1 = W; return x1; } inline int get_y0(int node) { return node * H / NUM_NODES; } inline int get_y1(int node) { int y1 = (node+1) * H / NUM_NODES; if(node == NUM_NODES-1) y1 = H; return y1; } int main() { if(NUM_NODES > W) { NUM_NODES = W; } if(ME >= NUM_NODES) return 0; int x0 = get_x0(ME); int x1 = get_x1(ME); // compute last element in each row int y0 = get_y0(ME); int y1 = get_y1(ME); FOR(y,y0,y1) { int curr_last = -1; REP(cnode, NUM_NODES-1) { int xx0 = get_x0(cnode); int xx1 = get_x1(cnode); FOR(x, xx0, xx1) { if(!IsUsableCell(y,x)) curr_last = x; } PutInt(cnode+1, curr_last); } } if(y0 < y1) REP(cnode, NUM_NODES-1) { Send(cnode+1); } if(ME == 0) { REP(y,H) last[y] = -1; } else { REP(cnode, NUM_NODES) { Receive(cnode); int yy0 = get_y0(cnode); int yy1 = get_y1(cnode); FOR(y, yy0, yy1) { last[y] = GetInt(cnode); } } } if(DBG) { fprintf(stderr, "INPUT:\n"); REP(y,H) { REP(x,W) { fprintf(stderr, "%d", IsUsableCell(y,x)); } fprintf(stderr, "\n"); } } // now `last` has valid values ll total = 0; vector<ll> dbg; // dp FOR(x, x0, x1) { ll val = 0; num_points = 1; points[0] = {-1,x}; REP(y,H) { if(IsUsableCell(y,x)) { while(points[num_points-1].x <= last[y]) { auto diff = 1LL * (last[y] - points[num_points-1].x) * (points[num_points-1].y - points[num_points-2].y); val -= diff; --num_points; } val += x - last[y]; points[num_points++] = {y, last[y]}; } else { val = 0; last[y] = x; num_points = 1; points[0] = {y, x}; } //fprintf(stderr, "result[%d][%d] == %lld\n", y, x, val); //fprintf(stderr, "%lld\t", val); if(DBG) dbg.push_back(val); total += val; } //fprintf(stderr, "\n"); } if(ME == 0) { FOR(i, 1, NUM_NODES) { Receive(i); total += GetLL(i); } printf("%lld", total); if(DBG) { REP(y,H) { REP(x,W) { fprintf(stderr, "%lld\t", dbg[x*H + y]); } fprintf(stderr, "\n"); } } } else { PutLL(0, total); Send(0); } } |