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
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

#include "dzialka.h"
#include "message.h"

namespace {

void send_vector(int target, vector<int> const& v)
{
  constexpr int limit = 10000;
  int i = 0;
  int n = v.size();
  while (i < n) {
    int q = min(limit, n - i);
    PutInt(target, q);
    for (int j = i; j < i + q; ++j) {
      PutInt(target, v[j]);
    }
    Send(target);
    i += q;
  }
  PutInt(target, 0);
  Send(target);
}

pair<vector<int>, int> receive_vector(int target)
{
  vector<int> v;
  while (true) {
    target = Receive(target);
    int k = GetInt(target);
    if (k == 0) break;
    for (int i = 0; i < k; ++i) {
      v.emplace_back(GetInt(target));
    }
  }
  return {move(v), target};
}

}

constexpr int inf = 100111222;

int main()
{
  int h = GetFieldHeight();
  int w = GetFieldWidth();

  int k = min(NumberOfNodes(), h);
  int id = MyNodeId();
  if (id >= k) return 0;

  int top = (id + 0LL) * h / k;
  int bottom = (id + 1LL) * h / k;

  assert(bottom - top > 0);

  vector<int> last;
  if (id == 0) last.resize(w);
  else last.resize(w, inf);
  for (int i = top; i < bottom; ++i) {
    for (int j = 0; j < w; ++j) {
      if (IsUsableCell(i, j)) ++last[j];
      else last[j] = 0;
    }
  }

  vector<int> first;
  if (id == 0) {
    first.resize(w);
  } else {
    first = move(receive_vector(id - 1).first);
    for (int i = 0; i < w; ++i) {
      if (last[i] >= inf) {
        last[i] -= inf;
        last[i] += first[i];
      }
    }
  }

  if (id < k - 1) {
    send_vector(id + 1, last);
  }

  long long res = 0;

  last = first;
  for (int i = top; i < bottom; ++i) {
    vector<pair<int, int>> stack{{0, -1}};
    auto update = [&](int x, int j) {
      int p = j;
      while (stack.back().first > x) {
        assert(stack.size() > 1);
        auto const& q = stack.back();
        auto const& qq = stack[stack.size() - 2];
        auto rr = max(qq.first, x);
        p = q.second;
        long long area = static_cast<long long>(j - q.second) * (q.first - rr) * (j - q.second + 1) / 2;
        res += area;
        stack.pop_back();
      }
      if (stack.back().first < last[j]) {
        stack.emplace_back(last[j], p);
      }
    };
    for (int j = 0; j < w; ++j) {
      if (IsUsableCell(i, j)) ++last[j];
      else last[j] = 0;
      update(last[j], j);
    }
    update(0, w);
  }

  if (id > 0) {
    Receive(id - 1);
    res += GetLL(id - 1);
  }

  if (id < k - 1) {
    PutLL(id + 1, res);
    Send(id + 1);
  } else {
    cout << res << endl;
  }

  return 0;
}