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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "dzialka.h"
#include "message.h"

#include<bits/stdc++.h>
using namespace std;

#define REP(i, M) for(int i = 0; i < M; i++)
#define R(p, k) for(int i = p; i <= k; i++)
#define RI(j, p, k) for(int j = p; j <= k; j++)
#define PB push_back
#define FI first
#define SE second
#define LL long long 


#define DEB 0

#if DEB
	#define CEN cerr << endl;
	#define CEL(a) cerr << #a << ": " << a << " "; 
	#define CES(s) cerr << s << endl;
#else
	#define CEN 
	#define CEL(a) 
	#define CES(s) 
#endif

//const LL INF = (LL) 1000 * 1000 * 1000 * 1000 * 1000 * 1000;
const int MN = 76 * 1000 + 5, K = 400; //<__________________________________________________
int deep[MN], pref_deep[MN];
LL result;
stack<pair<int, int> > st;
int m, id, NumRows, NumCols, b_row, e_row;
//vector<bool> data[MN];


void sync_result(){
   if(id > 0){
        CES("--------WYSYLAM");
        CEL(id);
        PutLL(0, result);
        Send(0);
    
    }
    
    else{
        for(int o = 1; o < m; o++){
            CES("-------ODBIERAM");
            CEL(o);
            Receive(o);
            result += GetLL(o);
        }
    }
}


void sync_deep(){
    for(int k = 0; k < K; k++){
        int b_col = k * NumCols / K + 1;
        int e_col = (k + 1) * NumCols / K + 1;
        CES("Paczuszka");
        CEL(k);
        CEL(b_col);
        CEL(e_col);
        CEN;
        if(id > 0){
            CES("-------ODBIERAM deep");
            CEL(id);
            Receive(id - 1);
            for(int i = b_col; i < e_col; i++){
                pref_deep[i] = GetLL(id - 1);
            }
        }
        
        for(int i = b_col; i < e_col; i++){
            swap(pref_deep[i], deep[i]);
            if(pref_deep[i] == e_row - b_row)
                pref_deep[i] += deep[i];
            CES("pref");
            CEL(i);
            CEL(b_row);
            CEL(e_row);
            CEL(deep[i]);
            CEL(pref_deep[i]);
            CEN;
        }
        
        if(id < m - 1){
            CES("--------WYSYLAM deep");
            CEL(id);
            for(int i = b_col; i < e_col; i++){
                PutLL(id + 1, pref_deep[i]);
            }
            Send(id + 1);
        }
    }
}

void just_compute_deep(int Row){
    for (int Col = 1; Col <= NumCols; ++Col){
        if (IsUsableCell(Row - 1, Col - 1)){
            deep[Col]++;
        }
        else{
            deep[Col] = 0;
        }
    }
}

/*void compute_deep(int Row){
    data[Row - b_row].reserve(NumCols + 1);
    data[Row - b_row][0] = (false);
    for (int Col = 1; Col <= NumCols; ++Col){
        if (IsUsableCell(Row - 1, Col - 1)){
            deep[Col]++;
            data[Row - b_row][Col] = (true);
        }
        else{
            deep[Col] = 0;
            data[Row - b_row][Col] = (false);
        }
    }
    //deep[NumCols + 1] = 0;
}


void re_compute_deep(int Row){
    for (int Col = 1; Col <= NumCols; ++Col){
        if (data[Row - b_row][Col]){
            deep[Col]++;
        }
        else{
            deep[Col] = 0;
        }
    }
    //deep[NumCols + 1] = 0;
}*/


void compute_part_res(int Row){
    
    while(!st.empty())
        st.pop();
    st.push({0, 0});
    
    for(int col = 1; col <= NumCols + 1; col++){
        CES("wsadzam:")
        CEL(deep[col]);
        CEN;
        while(deep[col] < st.top().first){
            CES("zdejmuje")
            CEL(st.top().first);
            CEN;
            
            
            LL up_h = st.top().first;
            LL dl = col - 1;
            st.pop();
            CEL(st.size());
            dl = dl - st.top().second;
            up_h = up_h - max(st.top().first, deep[col]);
            LL res = dl * (dl + 1) / 2 * up_h;
            CES("\tRES: ");
            CEL(res);
            CEN;
            result += res;
        }
        st.push({deep[col], col});
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    
    id = MyNodeId();
    //if(id != 0) return 0;
    NumRows = GetFieldHeight();
    NumCols = GetFieldWidth();
    m = NumberOfNodes();
    //---------------Na wszelki wypadek:
    if(NumRows * NumCols <= 3000 * 3000){
        if(id != 0) return 0;
        m = 1;
    }

    b_row = (id * NumRows / m) + 1;
    e_row = ((id + 1)* NumRows / m) + 1;
    //int b_row = 1, e_row = NumRows + 1;
    
    for (int Row = b_row; Row < e_row; ++Row)
        just_compute_deep(Row);
    sync_deep();
    
    /*for(int Col = 1; Col <= NumCols; Col++)
        deep[Col] = 0;
    for (int Row = 1; Row < b_row; ++Row)
        compute_deep(Row);*/
    
    for (int Row = b_row; Row < e_row; ++Row){
        CEL(Row);
        CEN;
        //building deep
        just_compute_deep(Row);
        
        //making stack and counting partial result
        //result += compute_part_res(Row);
        compute_part_res(Row);
        
    }
    
    sync_result();
    
    if (id == 0)
        cout << result << endl;
    return 0;
}