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
#include "dzialka.h"
#include "message.h"
#include<bits/stdc++.h>
using namespace std;
# ifdef DEB
# define debug(...) fprintf(stderr, __VA_ARGS__)
# else
# define debug(...)
#endif
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LD long double
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define pb pop_back
#define VI vector<int>
#define VPI vector<PII>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define ALL(x) (x).begin(), (x).end()
#define siz(V) (V.size())
#define LL unsigned long long
const int M = 1024 * 75;
const int Nodes = 100;

int n, m, up[Nodes], down[Nodes], id;
int T[M], TU[M];
LL res;

void init()
{
  id = MyNodeId();
  n = GetFieldHeight();
  m = GetFieldWidth();
  int h = (n + Nodes - 1) / Nodes;
  int tmp = 0;
  for (int i = 0; i < Nodes; i++)
  {
    up[i] = tmp;
    down[i] = min(n - 1, tmp + h - 1);
    tmp += h;
  }
  for (int i = 0; i < m; i++)
  {
    T[i] = TU[i] = -1;
  }
}

void readMyPart()
{
  for (int i = 0; i < m; i++)
  {
    for (int j = down[id]; j >= up[id]; j--)
    {
      if (!IsUsableCell(j, i))
      {
        T[i] = j;
        break;
      }
    }
  }
}

void sendAndReceive()
{
  int dis = 1;
  while (id - dis >= 0 || id + dis < Nodes)
  {
    if (id + dis < Nodes)
    {
      for (int i = 0; i < m; i++)
      {
        PutInt(id + dis, max(TU[i], T[i]));
      }
      Send(id + dis);
    }
    if (id - dis >= 0)
    {
      Receive(id - dis);
      for (int i = 0; i < m; i++)
      {
        TU[i] = max(TU[i], GetInt(id - dis));
      }
    }
    dis *= 2;
  }
}

void solve()
{
  vector<PII> V;
  for (int i = up[id]; i <= down[id]; i++)
  {
    V.clear();
    V.PB(MP(i, -1));
    LL tmp = 0;
    for (int j = 0; j < m; j++)
    {
      int h = 0;
      if (!IsUsableCell(i, j))
      {
        TU[j] = max(TU[j], i);
      }
      tmp += i;
      while (siz(V) > 0 && V[siz(V) - 1].F <= TU[j])
      {
        tmp -= (LL)(V[siz(V) - 1].F - h) * (j - V[siz(V) - 1].S);
        h = V[siz(V) - 1].F;
        V.pb();
      }
      if (siz(V) > 0)
      {
        tmp -= (LL)(TU[j] - h) * (j - V[siz(V) - 1].S);
      }
      V.PB(MP(TU[j], j));
      res += tmp;
    }
  }
}

int main()
{
  init();
  readMyPart();
  sendAndReceive();
  solve();
  if (id == 0)
  {
    for (int i = 1; i < Nodes; i++)
    {
      Receive(i);
      res += GetLL(i);
    }
    printf("%llu\n", res);
  }
  else
  {
    PutLL(0, res);
    Send(0);
  }
  return 0;
}