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
#include "dzialka.h"
#include "message.h"

#include <iostream>
#include <vector>
#include <utility>

#define ll long long
#define OUTPUT cerr << myId << ": "
using namespace std;

int NumRows;
int NumCols;
int nodes;
int myId;
ll myStart;
ll myEnd;

ll M[751][1200];
/*
//--------------------
int IsUsableCell(int row, int col) {
    return row!=col;
}

int GetFieldHeight(){
    return 75000;
}
int GetFieldWidth(){
    return 75000;
}
//-------------------- 
*/


void split(int N) {
    if(N<nodes){
        nodes = (int)N;
    }

    ll perNode = N/nodes;
    if(myId >= nodes){
        cerr << myId<<": no work\n";
        exit(0);
    }
    ll rest = N%nodes;
    int cur=0;
    for(int i=0;i<nodes;i++) {
        int start = cur;
        int end = cur+perNode;
        if(rest>0) {
            end++;
            rest--;
        }
        if(i==myId) {
            myStart = start;
            myEnd = end;
        }
        cur = end;
    }
    //OUTPUT << "["<<myStart<<", "<<myEnd<<")\n";
}

void setBit(int row, int col) {
    int elem = col/64;
    int bit = col & 63;
    M[row][elem] |= (1LL<<bit);
}

bool getBit(int row, int col) {
    int elem = col/64;
    int bit = col & 63;
    return (M[row][elem] & (1LL<<bit))!=0;
}

void readMap(){
    for(int row  = myStart;row<myEnd;row++)for(int col = 0;col<NumCols;col++){
       if(IsUsableCell(row,col)) setBit(row-myStart, col); 
    }
}

ll solveDistributed() {
    split(NumRows);
    readMap();
    int myRows = myEnd - myStart;
    vector<int>*D = new vector<int>(NumCols);
    vector<int>*PD = new vector<int>(NumCols);
    vector<int> D0;
    for(int row =0;row<myRows;row++) {
        for(int col=0;col<NumCols;col++){
            if(!getBit(row,col))(*D)[col]=0;
            else {
                (*D)[col]=1 + (*PD)[col];
            }
        }
        swap(D,PD);
    }
    if(myId >0) {
        for(int i=0;i<NumCols;i++){
            if(i%750==0) Receive(myId -1);
            D0.push_back((int)GetLL(myId-1));
        }
        for(int col=0;col<NumCols;col++){
           if((*PD)[col]==myRows)
               (*PD)[col] += D0[col];
        }
    }
    if(myId+1<nodes){
        for(int i=0;i<NumCols;i++){
            if(i>0 && i%750 ==0){ Send(myId+1);}
            PutLL(myId+1, (*PD)[i]);
        }
        Send(myId+1);
    }
    ll res =0;
    if(myId>0) {
        for(int i=0;i<NumCols;i++)(*PD)[i]=D0[i];
    }else {
        fill(PD->begin(), PD->end(),0);
    }
    vector<pair<int,int>>S;
    for(int row=0;row<myRows;row++) {
        S.clear();
        ll totMin=0;
        for(int col=0;col<NumCols;col++){
            if(!getBit(row,col))(*D)[col]=0;
            else {
                (*D)[col]=1 + (*PD)[col];
            }
            ll minH = (*D)[col];
            int removed=0;
            ll countRemoved=0;
            while(S.size()>0 && S.back().first>minH) {
                removed += S.back().second;
                countRemoved += 1LL*S.back().first*S.back().second;
                S.pop_back();
            }
            ll added = minH*(removed+1);
            S.push_back(make_pair(minH, removed+1));
            totMin = totMin - countRemoved +added;
            res+=totMin;
        }
        swap(D,PD);
    }
    if(myId>0){
        PutLL(0,res);
        Send(0);
    } else {
        for(int n=0;n<nodes-1;n++){
            int nd = Receive(-1);
            ll r = GetLL(nd);
            res+=r;
        }
        cout <<res<<endl;
    }
    return 0;
}

int main() {

    NumRows = GetFieldHeight();
    NumCols = GetFieldWidth();
    myId = MyNodeId();
    nodes = NumberOfNodes();



    solveDistributed();
}