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
#include <iostream>
#include <stack>
#include <tuple>
#include "dzialka.h"
#include "message.h"
using namespace std;

#define LL long long
#define MAX 75005

int myNodeId;
int numberOfNodes;

int n;
int m;

int buf1[MAX];
int buf2[MAX];

LL answer = 0;

inline int fragmentStart(int size, int node) {
    return (size * node) / numberOfNodes;
}

inline int fragmentEnd(int size, int node) {
    return fragmentStart(size, node + 1);
}

void part1_send(int target, int start, int end) {
    if(target == myNodeId) {
        for(int i = start; i < end; ++i) {
            buf2[i] = buf1[i - start];
        }
    } else {
        for(int i = start; i < end; ++i) {
            PutInt(target, buf1[i - start]);
        }
        Send(target);
    }
}

void part1() {
    int start = fragmentStart(m, myNodeId);
    int end = fragmentEnd(m, myNodeId);
    for(int i = start; i < end; ++i) {
        buf1[i - start] = 0;
    }
    int id = 0;
    int idFragmentStart = fragmentStart(n, id);
    for(int row = 0; row < n; ++row) {
        for(int i = start; i < end; ++i) {
            if(IsUsableCell(row, i)) {
                ++buf1[i - start];
            } else {
                buf1[i - start] = 0;
            }
        }
        while(id < numberOfNodes && row == idFragmentStart) {
            part1_send(id, start, end);
            idFragmentStart = fragmentStart(n, ++id);
        }
        if(id == numberOfNodes) break;
    }
    while(id < numberOfNodes) {
        part1_send(id++, start, end);
    }
}

void part2_processRow() {
    stack<tuple<int, int, int>> S; // height, count, column
    LL sum = 0;
    for(int i = 0; i < m; ++i) {
        while(!S.empty() && buf2[i] <= get<0>(S.top())) {
            sum -= (LL)get<0>(S.top()) * get<1>(S.top());
            S.pop();
        }
        int count = (S.empty() ? i + 1 : i - get<2>(S.top()));
        S.push(make_tuple(buf2[i], count, i));
        sum += (LL)buf2[i] * count;
        answer += sum;
    }
}

void part2() {
    for(int node = 0; node < numberOfNodes; ++node) {
        if(myNodeId != node) {
            Receive(node);
            int start = fragmentStart(m, node);
            int end = fragmentEnd(m, node);
            for(int i = start; i < end; ++i) {
                buf2[i] = GetInt(node);
            }
        }
    }

    int start = fragmentStart(n, myNodeId);
    int end = fragmentEnd(n, myNodeId);
    if(start < end) part2_processRow();
    for(int i = start + 1; i < end; ++i) {
        for(int j = 0; j < m; ++j) {
            if(IsUsableCell(i, j)) {
                ++buf2[j];
            } else {
                buf2[j] = 0;
            }
        }
        part2_processRow();
    }
}


int main() {
    myNodeId = MyNodeId();
    numberOfNodes = NumberOfNodes();

    n = GetFieldHeight();
    m = GetFieldWidth();
    if(min(n, m) < numberOfNodes) {
        numberOfNodes = min(n, m);
        if(myNodeId >= numberOfNodes) {
            return 0;
        }
    }

    part1();
    part2();

    if(myNodeId != 0) {
        PutLL(0, answer);
        Send(0);
    } else {
        for(int node = 1; node < numberOfNodes; ++node) {
            Receive(node);
            answer += GetLL(node);
        }
        cout << answer;
    }

    return 0;
}