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
#include <iostream>
#include <vector>
#include <utility>
#include <stack>

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

//#define DEBUG

using namespace std;



int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int numberOfNodes = NumberOfNodes();
    int myNodeId = MyNodeId();

    int parts_in_row = numberOfNodes;

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

    long long full_result = 0;
    long long part_result = 0;

    int this_instance_lower = height * myNodeId / numberOfNodes;
    int this_instance_upper = height * (myNodeId+1) / numberOfNodes;
    int this_instance_range = this_instance_upper - this_instance_lower;

    vector<vector<bool>> field_belt(this_instance_range, vector<bool>(width+1, false));    
    for(int i=0 ; i<this_instance_range ; i++) {
        for(int j=0 ; j<width ; j++) {
            field_belt[i][j] = (IsUsableCell(i+this_instance_lower, j)==1);
#ifdef DEBUG
            cout << (int)field_belt[i][j] << " ";
#endif
        }
#ifdef DEBUG
        cout << endl;
#endif
    }

    vector<int> upperG(width+1, 0);
    vector<int> prevG(width+1, 0);
    vector<int> G(width+1, 0);

    for(int i=0 ; i<this_instance_range ; i++) {
        swap(G, prevG);
        for(int j=0 ; j<width ; j++) {
            if(field_belt[i][j]==1) {
                G[j] = prevG[j] + 1;
            } else {
                G[j] = 0;
            }
        }
    }

#ifdef DEBUG
    cout << "From node " << myNodeId << " instance lower " << this_instance_lower << " instance upper " << this_instance_upper << endl;
#endif
    for(int part=0 ; part<parts_in_row ; part++) {
        int this_part_lower = width * part / parts_in_row;
        int this_part_upper = width * (part+1) / parts_in_row;
        if( myNodeId>0 && this_part_lower<this_part_upper ) {
            Receive(myNodeId-1);
            for(int j=this_part_lower ; j<this_part_upper ; j++) {
                upperG[j] = GetInt(myNodeId-1);
            }
            if( this_instance_lower==this_instance_upper ) {
                for(int j=this_part_lower ; j<this_part_upper ; j++) {
                    G[j] = upperG[j];
                }
            } else {
                for(int j=this_part_lower ; j<this_part_upper ; j++) {
                    if( G[j] == this_instance_range ) {
                        G[j] += upperG[j];
                    }
                }
            }
#ifdef DEBUG
            for(int j=this_part_lower ; j<this_part_upper ; j++)
                cout << upperG[j] << " ";
#endif
        }
        if( myNodeId<numberOfNodes-1 &&  this_part_lower<this_part_upper ) {
            for(int j=this_part_lower ; j<this_part_upper ; j++) {
                PutInt(myNodeId+1, G[j]);
            }
            Send(myNodeId+1);
        }
    }
#ifdef DEBUG
    cout << endl;
#endif

    prevG = upperG;
    stack<pair<int,int>> s;
    long long stack_area = 0;
    for(int i=0 ; i<this_instance_range ; i++) {
        stack_area = 0;
        for(int j=0 ; j<=width ; j++) {
            if( field_belt[i][j]==false ) {
                G[j] = 0;
            } else {
                G[j] = prevG[j] + 1;
            }
            
            if( G[j]==0 ) {
                while( !s.empty() ) {
                    s.pop();
                }
                stack_area = 0;
            }
            else if( s.empty() || get<0>(s.top()) < G[j] ) {
                s.push(make_pair(G[j], j));
                stack_area += G[j];
                part_result += stack_area;
            } else if( !s.empty() && get<0>(s.top()) == G[j] ) {
                stack_area += G[j];
                part_result += stack_area;
            } else { //!s.empty() && get<0>(s.top()) > G[j] && G[j] != 0
                int extent_on_j_axis;
                while( !s.empty() && get<0>(s.top()) > G[j] ) {
                    pair<int,int> point = s.top();
                    s.pop();
                    extent_on_j_axis = get<1>(point);
                    if( s.empty() ) {
                        stack_area -= static_cast<long long>(get<0>(point)-G[j]) * static_cast<long long>(j-extent_on_j_axis);
                    } else {
                        stack_area -= static_cast<long long>(get<0>(point)-G[j]) * static_cast<long long>(j-extent_on_j_axis);
                    }
                }
                s.push(make_pair(G[j], extent_on_j_axis));
                stack_area += G[j];
                part_result += stack_area;
            }
#ifdef DEBUG
                cout << "position " << i + this_instance_lower << " " << j << endl << "stack_area: " << stack_area << endl;
                stack<pair<int,int>> temp;
                while( !s.empty() ) {
                    cout << "j: " << get<1>(s.top()) << "\ti: " << get<0>(s.top()) << "\n";
                    temp.push(s.top());
                    s.pop();
                }
                while(!temp.empty()) {
                    s.push(temp.top());
                    temp.pop();
                }
#endif
        }
#ifdef DEBUG
        cout << "partial result from row " << i + this_instance_lower << ": " << part_result << endl;
#endif
        swap(prevG, G);
    }

    if(myNodeId>0) {
        PutLL(0, part_result);
        Send(0);
    } else {
        full_result = part_result;
        for(int i=1 ; i<numberOfNodes ; i++) {
            Receive(i);
            full_result += GetLL(i);
        }
        cout << full_result << endl;
    }


    return 0;
}