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
#include "message.h"
#include "dzialka.h"
#include <cassert>
#include <vector>
#include <iostream>
#include <stack>

#define repeat(count) for(int __iterator = 0; __iterator < count; ++__iterator)

namespace Communication
{

    struct Node
    {
        const int id;
    public:

        Node(int id) : id(id) {}

        operator bool() const
        {
            return id >= 0 and id < NumberOfNodes();
        }

        Node next() const { return {id+1}; }
        Node prev() const { return {id-1}; }

        operator int() const { return id; }
    };

    struct CurrentNode : public Node
    {
        CurrentNode() : Node(MyNodeId()) {}
    };

    struct Nodes : public std::vector<Node> 
    {
        const int count;
        Nodes() : count(NumberOfNodes())
        {
            reserve(count);
            for (int i = 0; i < count; ++i)
                emplace_back(i);
        }

        Node receive() const { return {Receive(-1)}; }
    };
}

using namespace Communication;
using namespace std;

template<typename T = int>
vector<T> split(T count, int parts)
{
    vector<T> ret;
    ret.reserve(parts + 1);

    for (int i = 0; i <= parts; ++i)
    {
        ret.push_back(count * i / parts);
    }

    return ret;
}

struct Point { int x, y; };

int main() try
{
    CurrentNode node;
    Nodes nodes;

    Node master = nodes.back();    

    if (node.id == master.id)
    {
        unsigned long long total_answer = 0;

        repeat (nodes.count-1)
        {
            Node source = nodes.receive();
            unsigned long long subanswer = GetLL(source);
            total_answer += subanswer;
        }

        cout << total_answer << endl;
        return 0;
    }

    int width = GetFieldWidth();
    int height = GetFieldHeight();

    const auto X = split(width, nodes.count-1);
    const auto Y = split(height, nodes.count-1);

    {
        int offset = X.at(node.id);
        int length = X.at(node.id + 1) - offset;
        
        vector<int> data(length, 0);
        auto next = 0;

        for (int y = 0; y < height; ++y)
        {
            while (y == Y.at(next))
            {
                PutChar(next, 0);
                for (int x = 0; x < length; ++x)
                {
                    PutInt(next, data.at(x));
                }
                //cerr << "Sending " << length << " ints + char to #" << next << endl;
                Send(next);
                next++;
            }

            for (int x = 0; x < length; ++x)
            {
                bool isValid = IsUsableCell(y, x+offset) == 1;
                data.at(x) = isValid ? data.at(x) + 1 : 0;
            }
        }
    }


    long long answer = 0;

    {
        vector<int> data(width+1, 0);

        repeat (nodes.count-1)
        {
            Node source = nodes.receive();
            //cerr << "Reading " << X.at(source.id+1)-X.at(source.id) << " ints + char from #" << source.id << endl;
            GetChar(source);
            for (int x = X.at(source.id); x < X.at(source.id+1); ++x)
            {
                data.at(x) = GetInt(source);
            }
        }

        for (int y = Y.at(node.id); y < Y.at(node.id + 1); ++y)
        {
            //cout << "y = " << y << endl << "  ";

            for (int x = 0; x < width; ++x)
            {
                bool isValid = IsUsableCell(y, x) == 1;
                data.at(x) = isValid ? data.at(x) + 1 : 0;
                //cout << data.at(x) << " ";
            }
            //cout << endl;
            stack<Point> stack;
            stack.push({0, 0});

            for (int x = 0; x <= width; ++x)
            {
                int left = x;
                int y = data.at(x);
                while (stack.top().y > y)
                {
                    Point p = stack.top();
                    left = p.x;
                    //long long size = 1LL * (x - p.x) * p.y;
                    stack.pop();
                    int y_low = std::max(y, stack.top().y);

                    //cout << "  .at(" << p.x << ".." << x - 1 << ")x.at(0.." << p.y-1 << ") = " << size << "  (with y_low = " << y_low << ")" << endl;
                    //cout << "    Creating " << 1LL * (x-left) * (p.y-y_low) << " answers" << endl;

                    int w = x-left;
                    answer += 1LL * w * (w+1) / 2 * (p.y-y_low);


                    //best_solution = std::max(size, best_solution);
                }

                if (stack.top().y < y)
                    stack.push({ left, y });
            }
        }
    }

    PutLL(master, answer);
    Send(master);

    //cout << "answer += " << answer << endl;
}
catch (std::exception &ex)
{
    cerr << ex.what() << endl;
}