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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(auto v=(p);v<=(k);++v)
#define FORD(v,p,k) for(auto v=(p);v>=(k);--v)
#define REP(i,n) for(auto i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

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

int main() {
  int fieldHeight = GetFieldHeight();
  int fieldWidth = GetFieldWidth();
  int nodesNumber = NumberOfNodes();
  int nodeID = MyNodeId();

  int firstHeight = fieldHeight * nodeID / nodesNumber;
  int lastHeight = fieldHeight * (nodeID + 1) / nodesNumber - 1;
  int mySegmentHeight = lastHeight - firstHeight + 1;

  // wstaw sztuczna prawa krawedz, ale nie wysyłaj
  // TODO moze wywalmy ta tablice (w sumie tylko 2 razy z niej korzystamy)
  vector<vector<bool>> tab(mySegmentHeight, vector<bool>(fieldWidth+1));

  REP(r, mySegmentHeight) {
    REP(c, fieldWidth) {
      tab[r][c] = IsUsableCell(r+firstHeight,c);
    }
  }
  VI G(fieldWidth+1);
  if (nodeID < nodesNumber-1) {
    REP(r, mySegmentHeight) {
      REP(c, fieldWidth) {
        G[c] = (tab[r][c]) ? G[c]+1 : 0;
      }
    }
  }
  //TODO czy na pewno te in_single_message dziala???
  VI upperG(fieldWidth+1);
  if (nodeID > 0) {
    int in_single_message=0;
    int receivedNodeID = Receive(nodeID-1);
    REP(c, fieldWidth) {
      if (in_single_message == 60*1024) {
        receivedNodeID = Receive(nodeID-1);
        in_single_message=0;
      }
      upperG[c] = GetInt(receivedNodeID);
      in_single_message+=4;
    }
  }
  if (nodeID < nodesNumber-1) {
    int in_single_message = 0;
    REP(c, fieldWidth) {
      if (G[c] == mySegmentHeight) G[c] += upperG[c];
      PutInt(nodeID+1, G[c]);
      in_single_message += 4;
      if (in_single_message == 60*1024) {
        Send(nodeID+1);
        in_single_message=0;
      }
    }
    if (in_single_message > 0) {
      Send(nodeID+1);
    }
  }

  G = upperG;
  LL res = 0;

  //opieram sie na opisie i kodzie zadania Dzialka z
  //http://www.oi.edu.pl/static/attachment/20110811/oi9.pdf
  //http://www.oi.edu.pl/static/attachment/20110811/oi9-dysk.zip
  vector<PII> S;
  REP(r, mySegmentHeight) {
    REP(c, fieldWidth) {
      G[c] = (tab[r][c]) ? G[c]+1 : 0;
    }

    S.clear();
    S.PB(MP(0,0));
    REP(j, fieldWidth+1) {
      int start=j;
      while (G[j] < S.back().ND) {
        int k = S.back().ST;//kolumna startowa
        int g = S.back().ND;//wysokosc prostokata
        S.pop_back();
        //TODO przejrzyj to miejsce kilka razy
        res += ((LL) (g - max(S.back().ND, G[j]))) * (j - k) * (j - k + 1) / 2;
        start=k;
      }
      if (S.back().ND < G[j]) {
        S.PB(MP(start, G[j]));
      }
    }
  }


  if (nodeID > 0) {
    PutLL(0, res);
    Send(0);
  } else {
    REP(i, nodesNumber-1) {
      int receivedNodeID = Receive(-1);
      res += GetLL(receivedNodeID);
    }
    printf("%lld\n", res);
  }
  return 0;
}