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
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;

#include "message.h"

//********************************************
#include "dzialka.h"

/*
static int FieldH = 75000, FieldW = 75000;
int GetFieldHeight() {
  return FieldH;
}

int GetFieldWidth() {
  return FieldW;
}

int IsUsableCell(int Row, int Col) {
  assert(0 <= Row && Row < FieldH && "Nieprawidlowy numer wiersza!");
  assert(0 <= Col && Col < FieldW && "Nieprawidlowy numer kolumny!");

  return 1;//rand() % 2;
}
*/
//******************************************



int NODES, ID, N, M;

const int MXM = 75003;
const int MXPN = 2 * MXM / 100 + 3;
int gora[MXM];
int dol[MXM];
bool memo[MXPN][MXM];


VPII V;
LL po2(LL n)
    {
    return n * (n-1) / 2;
    }

LL pole(LL x1, LL x2, LL y1, LL y2)
{
    LL wzo = (x2-x1) * po2(y2-y1+1+1); 
//    cerr<<"plu: "<<x1<<" "<<x2<<" "<<y1<<" "<<y2<<"   "<<wzo<<endl;
    return wzo;
}
LL go_dzialka1(int i)
    {
//    cerr<<"                      go: "<<i<<"     ";
//    FOR(j, 0, M-1)cerr<<gora[j]<<" ";
//    cerr<<endl;
    
    V.clear();
    V.PB(MP(i, -1));
    gora[M] = i;
    LL res = 0;
    
    FOR(j, 0, M) // jeden więcej
	    {
	    while(V.size() > 1 && V.back().f <= gora[j])
		    {
		    res += pole(V.back().f+1, min(gora[j], V[V.size()-2].f)+1, V[V.size()-2].s+1, j-1);
		    V.pop_back();
		    }
	    V.PB(MP(gora[j], j));
	    }
    return res;
    }


int main()
    {
    NODES=NumberOfNodes();
    ID=MyNodeId();
    
    N=GetFieldHeight();
    M=GetFieldWidth();


    int SIZ=N/NODES+(M%NODES>0);
    int poc=ID*SIZ;
    mini(poc,N);
    int kon=min(N-1,poc+SIZ-1);
//    cerr<<ID<<" "<<poc<<" "<<kon<<endl;

    REP(i, M)
        dol[i] = gora[i] = -1;

    FOR(i, poc, kon)
        {
        REP(j, M)
            {
            memo[i-poc][j] = IsUsableCell(i, j);
            
            if(!memo[i-poc][j])
                dol[j] = i;
            }
        }


    REP(i, 10)
        {
        if(ID + (1<<i) < NODES)
            {
            REP(j, M)
                PutInt(ID + (1<<i), dol[j]);
            Send(ID + (1<<i));
            }
        if(ID - (1<<i) >= 0)
            {
            Receive(ID - (1<<i));
            REP(j, M)
                {
                int x = GetInt(ID - (1<<i));
                maxi(dol[j], x);
                maxi(gora[j], x);
                }
            }
        }

    LL res = 0;
    FOR(i, poc, kon)
        {
        REP(j, M)
            {
            if(!memo[i-poc][j])
                gora[j] = i;
            }
        res += go_dzialka1(i);       
        }
    PutLL(0, res);
    Send(0);
    if(ID == 0)
        {
        unsigned long long sum = 0;
        REP(i, NODES)
            {
            Receive(i);
            sum += GetLL(i);
            }
        cout<<sum<<endl;
        }
    return 0;
    }