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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#ifndef MW_HEADER
#define MW_HEADER
#include <bits/stdc++.h>
#include "message.h"
using namespace std;

#define FOR(i,a,b) for (LL i = (a); i < (b); ++i)
#define FORD(i,b,a) for (LL i = (LL)(b)-1; i >= (a); --i)
#define REP(i,N) FOR(i,0,N)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define st first
#define nd second
#define pb push_back

typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
#endif

LL NODES = NumberOfNodes(), ME = MyNodeId();
LL __N, START, END, NEXT, PREV;
LL get_start(int node) { return __N/NODES * node + min(__N%NODES, (LL)node); }
LL get_end(int node) { return get_start(node+1); }
/** Every node has at least factor items to process */
void reduce_nodes(LL N, int factor = 1) {
  NODES = min(NODES, max(N/factor, 1LL)); if (ME >= NODES) exit(0);
  NEXT = (ME + 1)%NODES, PREV=(ME+NODES-1)%NODES;
  __N = N;  START = get_start(ME), END = get_end(ME);
}

template <typename Container> struct is_container : false_type { };
template <typename... Ts> struct is_container<list<Ts...> > : true_type { };
template <typename... Ts> struct is_container<vector<Ts...> > : true_type { };
// template <typename... Ts> struct is_container<set<Ts...> > : true_type { };
// template <typename... Ts> struct is_container<map<Ts...> > : true_type { };
template <typename Container> struct is_pair : false_type { };
template <typename... Ts> struct is_pair<pair<Ts...>> : true_type { };
template <typename Container> struct is_triple : false_type { };
template <typename T1, typename T2, typename T3> struct is_triple<tuple<T1,T2,T3>> : true_type { };

template<typename T> void Put(int target, typename enable_if<!is_class<T>::value, const T&>::type value) { T::not_implemented; }
template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec);
template<> void Put<bool>(int target, const bool& value) { PutChar(target, value); }
template<> void Put<char>(int target, const char& value) { PutChar(target, value); }
template<> void Put<int>(int target, const int& value) { PutInt(target, value); }
template<> void Put<unsigned int>(int target, const unsigned int& value) { PutInt(target, value); }
template<> void Put<long long>(int target, const long long& value) { PutLL(target, value); }
template<> void Put<unsigned long long>(int target, const unsigned long long& value) { PutLL(target, value); }
// template<typename T> void Put(int target, typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, const T&>::type value){
//   char data[sizeof(T)]; memcpy(data, &value, sizeof(T));
//   REP(i, (int)sizeof(T)) PutChar(target, data[i]);
// }
template<typename T> void Put(int target, typename enable_if<is_pair<T>::value, const T&>::type pair) {
  Put<typename T::first_type>(target, pair.first);
  Put<typename T::second_type>(target, pair.second);
}
template<typename T> void Put(int target, typename enable_if<is_triple<T>::value, const T&>::type triple) {
  Put<typename tuple_element<0, T>::type>(target, get<0>(triple));
  Put<typename tuple_element<1, T>::type>(target, get<1>(triple));
  Put<typename tuple_element<2, T>::type>(target, get<2>(triple));
}
const int MAX_MESSAGE_SIZE = 62000;
const int PARTS = -1;
template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec) {
  int data_size = vec.size() * sizeof(typename T::value_type);

  int parts = 1 + data_size / MAX_MESSAGE_SIZE;
  int part_size = (vec.size() + parts-1) / parts;

  Put<int>(target, parts);
  for (int p = 0; p < parts; ++p) {
    int start = p*part_size;
    int end = min((int)vec.size(), (p+1)*part_size);
    Put<int>(target, end-start);
    FOR(i, start, end) {
      Put<typename T::value_type>(target, vec[i]);
    }
    if (p < parts-1) {
      Send(target);
    }
  }
}

template<typename T> typename enable_if<!is_class<T>::value, T>::type Get(int source) { T::not_implemented; }
template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source);
template<> bool Get<bool>(int source) { return GetChar(source); }
template<> char Get<char>(int source) { return GetChar(source); }
template<> int Get<int>(int source) { return GetInt(source); }
template<> unsigned int Get<unsigned int>(int source) { return GetInt(source); }
template<> long long Get<long long>(int source) { return GetLL(source); }
template<> unsigned long long Get<unsigned long long>(int source) { return GetLL(source); }
// template<typename T> typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, T>::type Get(int source) {
//   char data[sizeof(T)]; REP(i, (int)sizeof(T)) data[i] = GetChar(source);
//   T value; memcpy(&value, data, sizeof(T));
//   return value;
// }
template<typename T> typename enable_if<is_pair<T>::value, T>::type Get(int source) {
  auto f = Get<typename T::first_type>(source);
  auto s = Get<typename T::second_type>(source);
  return T(f, s);
}
template<typename T> typename enable_if<is_triple<T>::value, T>::type Get(int source) {
  auto f = Get<typename tuple_element<0, T>::type>(source);
  auto s = Get<typename tuple_element<1, T>::type>(source);
  auto t = Get<typename tuple_element<2, T>::type>(source);
  return T(f, s, t);
}
template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source) {
  int parts = GetInt(source);

  vector<typename T::value_type> result;
  REP(p, parts) {
    if (p > 0) Receive(source);

    int size = GetInt(source);
    REP(i,size) result.push_back(Get<typename T::value_type>(source));
  }

  return T(result.begin(), result.end());
}

template<typename T> void Broadcast(int source, T& value) {
  if (ME == source) REP(i,NODES) { Put<T>(i, value); Send(i); }
  Receive(source); value = Get<T>(source);
}

template<typename T> void BroadcastTree(int source, T& value) {
  int relative = (ME - source + NODES) % NODES;
  if (relative) {
    int from = (source + (relative-1) / 2) % NODES;
    Receive(from); value = Get<T>(from);
  }
  FOR(i,1,3) if (2*relative + i < NODES) {
    int to = ((source + 2*relative + i) % NODES);
    Put<T>(to, value); Send(to);
  }
}

template<typename T, typename Fn> void Accumulate(int target, const T& value, Fn fn) {
  Put<T>(target, value); Send(target);
  if (ME == target) REP(i, NODES) { Receive(i); fn(Get<T>(i)); }
}

/** ~5ms for adding ints */
template<typename T, typename Fn> T AccumulateTree(int target, T value, Fn fn) {
  int relative = (ME - target + NODES) % NODES;
  for (int b = 1; b < NODES; b <<= 1) {
    if (relative&b) {
      int to = (target + relative - b + NODES) % NODES;
      Put<T>(to, value); Send(to); break;
    } else if (relative + b < NODES) {
      int from = (target + relative + b) % NODES;
      Receive(from); value = fn(value, Get<T>(from));
    }
  }

  return value;
}

template<typename T, typename Compute, typename Acc> T AccumulateValues(int target, Compute fn, Acc acc) {
  T result = fn(START);
  FOR(i,START+1,END) result = acc(result, fn(i));
  return AccumulateTree(target, result, acc);
}

template<typename T, typename Compute> vector<T> AccumulateVector(int target, Compute fn) {
  vector<T> local; FOR(i,START, END) local.pb(fn(i));
  vector<T> acc;
  Accumulate(target, local, [&](const vector<T>& vec) {acc.insert(acc.end(), vec.begin(), vec.end());});
  return acc;
}

template<typename T> vector<T> Collect(int target, const T& value) {
  vector<T> acc;
  Accumulate(target, value, [&](const T& val) { acc.pb(val); });
  return acc;
}
// ^^^ CUT HERE ^^^

/**
 * Intervals are left_bound-closed and right_bound-open: [L, R)
 */
template<typename Config>
class bit {
  template<typename TConfig = Config>
  struct range_updates_config {
    template<class U> static char (&test(typename U::TRangeUpdate const*))[1];
    template<class U> static char (&test(...))[2];

    template<class U = TConfig> constexpr static typename U::TRangeUpdate _neutral(typename U::TRangeUpdate const*) {
      return U::neutral_range_update();
    }
    template<class U = TConfig> constexpr static void* _neutral(...) { return 0; }

    static const bool enabled = (sizeof(test<TConfig>(0)) == 1);
    typedef decltype(_neutral<TConfig>(0)) Type;
    constexpr static Type neutral() {
      return _neutral<TConfig>(0);
    }
  };

  typedef typename Config::TData TData;
  typedef typename range_updates_config<Config>::Type TRangeUpdate;
  typedef const function<bool(const TData&)>& Predicate;

  int size;
  /** Interval represented by the node */
  vector<pair<int, int>> bounds;
  vector<TData> data;
  vector<TRangeUpdate> range_updates;

  bool __intersects(int L, int R, int idx) {
    return bounds[idx].first < R && L < bounds[idx].second;
  }

  bool __covers(int L, int R, int idx) {
    return L <= bounds[idx].first && bounds[idx].second <= R;
  }

  void __update_range_single(int idx, const TRangeUpdate& op) {
    Config::apply(op, data[idx], bounds[idx].first, bounds[idx].second);
    Config::compose_range_updates(op, range_updates[idx]);
  }

  template<class T = Config, typename enable_if<!range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {}

  template<class T = Config, typename enable_if<range_updates_config<T>::enabled, int>::type = 0>
  void __push_range_update(int idx) {
    if (idx < size) {
      __update_range_single(2*idx, range_updates[idx]);
      __update_range_single(2*idx + 1, range_updates[idx]);
      range_updates[idx] = Config::neutral_range_update();
    }
  }

  void __update_range(int L, int R, const TRangeUpdate& op, int idx) {
    if (!__intersects(L, R, idx)) {
      return;
    }
    if (__covers(L, R, idx)) {
      __update_range_single(idx, op);
      return;
    }

    __push_range_update(idx);
    __update_range(L, R, op, 2*idx);
    __update_range(L, R, op, 2*idx+1);

    data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
  }

  TData __query_range(int L, int R, int idx) {
    if (!__intersects(L, R, idx)) {
      return Config::neutral();
    }
    if (__covers(L, R, idx)) {
      return data[idx];
    }

    __push_range_update(idx);
    return Config::merge(
      __query_range(L, R, 2*idx),
      __query_range(L, R, 2*idx+1)
    );
  }

  /**
  * If last=1, the last matching is returned. If last=0, the first one.
  */
  int __find(int L, int R, Predicate fn, int idx, int last) {
    if (!__intersects(L, R, idx) || !fn(data[idx])) {
      return -1;
    }

    if (idx >= size) {
      return idx - size;
    }

    __push_range_update(idx);
    int preferred = __find(L, R, fn, 2*idx+last, last);
    if (preferred != -1) {
      return preferred;
    }
    return __find(L, R, fn, 2*idx+(last^1), last);
  }

public:
  bit(int _size = 0) {
    size = 1;
    while (size < _size) {
      size <<= 1;
    }
    data = vector<TData>(2*size, Config::neutral());
    range_updates = vector<TRangeUpdate>(2*size, range_updates_config<Config>::neutral());
    bounds = vector<pair<int, int>>(2*size);
    for (int i = 0; i < size; ++i) {
      bounds[i+size] = {i, i+1};
    }
    for (int i = size - 1; i >= 0; --i) {
      bounds[i] = {bounds[2*i].first, bounds[2*i+1].second};
    }
  }

  void update_range(int L, int R, const TRangeUpdate& op) {
    __update_range(L, R, op, 1);
  }

  void update_single(int pos, const TRangeUpdate& op) {
    update_range(pos, pos+1, op);
  }

  TData query_range(int L, int R) {
    return __query_range(L, R, 1);
  }

  TData query_single(int pos) {
    return query_range(pos, pos+1);
  }

  void set(int pos, TData value) {
    int idx = size + pos;
    if (range_updates_config<Config>::enabled) {
      idx = 1;
      while (idx < size) {
        __push_range_update(idx);
        idx = 2*idx + (pos >= bounds[2*idx+1].first);
      }
    }
    // Push pending operations

    data[idx] = value;
    idx >>= 1;
    while (idx > 0) {
      data[idx] = Config::merge(data[2*idx], data[2*idx+1]);
      idx >>= 1;
    }
  }

  /** @returns -1 if no element found */
  int first_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 0);
  }

  int first_which(Predicate contain_check) {
    return first_which(0, size, contain_check);
  }

  /** @returns -1 if no element found */
  int last_which(int L, int R, Predicate contain_check) {
    return __find(L, R, contain_check, 1, 1);
  }

  int last_which(Predicate contain_check) {
    return last_which(0, size, contain_check);
  }
};

struct bit_config {
  typedef struct {
    LL sum_a;
    LL sum_b;
    int min_a;
    int min_b;
    LL sum_a_mul_b;
  } TData;

  static TData neutral() {
    return { (LL)0, (LL)0, (int)1e5, (int)1e5, (LL)0 };
  }
  static TData merge(const TData& left, const TData& right) {
    return {
      left.sum_a + right.sum_a,
      left.sum_b + right.sum_b,
      min(left.min_a, right.min_a),
      min(left.min_b, right.min_b),
      left.sum_a_mul_b + right.sum_a_mul_b,
    };
  }

  typedef struct {
    int a;
    int b;
  } TRangeUpdate;

  static void apply(const TRangeUpdate& op, TData& value, int A, int B) {
    value.sum_a = min(value.sum_a, op.a * (LL)(B-A));
    value.sum_b = min(value.sum_b, op.b * (LL)(B-A));
    value.min_a = min(value.min_a, op.a);
    value.min_b = min(value.min_b, op.b);
    value.sum_a_mul_b = min(
      min(value.sum_a_mul_b, (B-A) * (LL)op.a * (LL)op.b),
      min(value.sum_a * op.b, value.sum_b * op.a)
    );
  }
  static void compose_range_updates(const TRangeUpdate& outer, TRangeUpdate& inner) {
    inner.a = min(inner.a, outer.a);
    inner.b = min(inner.b, outer.b);
  }
  static TRangeUpdate neutral_range_update() {
    return { (LL)1e5, (LL)1e5 };
  }
};


#include "dzialka.h"

bool data[800][75000];
int taken_in_column[75000];

typedef pair<LL, vector<tuple<int, int, bool>>> PartialResult;

int main() {
  // ios_base::sync_with_stdio(0);
  int H = GetFieldHeight(), W = GetFieldWidth();
  reduce_nodes(H);
  FOR(i,START,END) {
    REP(j,W) data[i-START][j] = IsUsableCell(i, j);
  }

  int N = END-START;
  REP(j,W) taken_in_column[j] = 0;

  LL result = 0;
  REP(i,N) {
    vector<PII> taken = {{-1, N}};
    int sum_taken = 0;
    REP(j,W) {
      if (!data[i][j]) taken_in_column[j] = i+1;

      while (taken.back().nd < taken_in_column[j]) {
        sum_taken -= (taken.back().st - taken[taken.size()-2].st) * taken.back().nd;
        taken.pop_back();
      }
      sum_taken += (j - taken.back().st) * taken_in_column[j];
      taken.pb({j, taken_in_column[j]});

      result += (i+1) * (j + 1) - sum_taken;
    }
  }

  PartialResult res;
  res.first = result;
  REP(j,W) {
    int top = 0, bottom = 0;
    REP(i,N) {
      if (data[i][j]) {
        ++bottom;
        if (top == i) ++top;
      } else {
        bottom = 0;
      }
    }
    res.second.pb(make_tuple(top, bottom, top == N));
  }

  //printf("%d %d\n", START, END);

  res = AccumulateTree(0, res, [&](PartialResult& top, const PartialResult& bottom) {
    LL result = top.st + bottom.st;
    bit<bit_config> BIT(W);
    REP(j,W) {
      int a = get<1>(top.nd[j]);
      int b = get<0>(bottom.nd[j]);

      BIT.set(j, {a, b, a, b, a*(LL)b});
      int k = BIT.last_which(0, j, [&](const bit_config::TData& dt) {
        return dt.min_a <= a;
      });
      BIT.update_range(k+1, j+1, {a, (int)1e5});
      // printf("a=%d [%d, %d]\n",a, k+1, j);

      k = BIT.last_which(0, j, [&](const bit_config::TData& dt) {
        return dt.min_b <= b;
      });
      BIT.update_range(k+1, j+1, {(int)1e5, b});
      // printf("b=%d [%d, %d]\n",b, k+1, j);

      // REP(k,j+1) printf("%d %lld %lld\n", k, BIT.query_single(k).sum_a, BIT.query_single(k).sum_b);

      result += BIT.query_range(0, j+1).sum_a_mul_b;
    }

    top.st = result;
    REP(j,W) {
      get<0>(top.nd[j]) = get<2>(top.nd[j]) ? get<0>(top.nd[j]) + get<0>(bottom.nd[j]) : get<0>(top.nd[j]);
      get<1>(top.nd[j]) = get<2>(bottom.nd[j]) ? get<1>(bottom.nd[j]) + get<1>(top.nd[j]) : get<1>(bottom.nd[j]);

      get<2>(top.nd[j]) &= get<2>(bottom.nd[j]);
    }



    return top;
  });

  if (ME == 0) {
    printf("%lld\n", res.st);
  }
}