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
#include <bits/stdc++.h>
#include "message.h"
#include "dzialka.h"
using namespace std;
typedef long long ll;

ll id, NN;
ll a,b,n;
ll maxNodes = 1000;

void init(){
    id = MyNodeId();
    NN = NumberOfNodes();
    n = GetFieldHeight();
    if(id >= n || id > maxNodes)
        exit(0);
    NN = min(NN,n);
    NN = min(NN,maxNodes);
}

void setInterval(){
    a = (id*n/NN);
    b = ((id+1)*n/NN);
    b = min(b,n);
    b = max(b,0LL);
}

const int packSize=(1<<7)-1;

int main(){
    init();
    setInterval();
    int width = GetFieldWidth();
    vector<bool> allUsable(width,true);
    vector<int> glebokosc(width);
    for(int i=b-a-1;i>=0;i--){
        for(int j=0;j<width;j++){
            if(IsUsableCell(i+a,j)){
                if(i+1 <= b-a-1)
                    glebokosc[j] = glebokosc[j]+1;
                else
                    glebokosc[j] = 1;
            }
            else{
                allUsable[j] = false;
                glebokosc[j] = 0;
            }
        }
    }
    vector<int> xtra(width);

    bool waiting = false;
    if(id == NN-1 && NN >= 2){
        for(int i=0;i<width;i++){
            PutInt(id-1,glebokosc[i]);
            waiting = true;
            if((i&packSize) == packSize){
                Send(id-1);
                waiting = false;
            }
        }
        if(waiting){
            Send(id-1);
            waiting = false;
        }
    }
    if(id < NN-1){
        int prev = 0;
        while(prev < width){
            Receive(id+1);
            for(int i=prev;i<width && i-prev < packSize+1;i++){
                xtra[i] = GetInt(id+1);
                if(id > 0){
                    waiting = true;
                    if(allUsable[i])
                        PutInt(id-1,xtra[i]+glebokosc[i]);
                    else
                        PutInt(id-1,glebokosc[i]);
                    if((i&packSize) == packSize){
                        Send(id-1);
                        waiting = false;
                    }
                }
            }
            if(id > 0 && waiting){
                Send(id-1);
                waiting = false;
            }
            prev = min(prev+packSize+1,width);
        }
    }

    glebokosc = xtra;

    //Czesc 2
    ll res = 0;
    for(int i=b-a-1;i>=0;i--){
         for(int j=0;j<glebokosc.size();j++){
            if(IsUsableCell(i+a,j))
                glebokosc[j] ++;
            else
                glebokosc[j] = 0;
         }

		 glebokosc.push_back(0);
         vector<pair<int,int > > stos;
         for(int j=0;j<glebokosc.size();j++){
             int lastPoppedX = j;
             while(stos.size() > 0 && stos.back().first >= glebokosc[j]){
				 int prevH = glebokosc[j];
				 if(stos.size() >= 2)
					prevH = max(prevH,stos[stos.size()-2].first);
				
                res += (long long) (j-stos.back().second) * (j-stos.back().second+1) * (long long)(stos.back().first - prevH) / 2;
                lastPoppedX = stos.back().second;
                stos.pop_back();
             }
             if(glebokosc[j] > 0)
				stos.push_back(make_pair(glebokosc[j],lastPoppedX));
         }
         glebokosc.pop_back();

    }

    if(id != 0){
        PutLL(0,res);
        Send(0);
    }
    else{
        for(int i=1;i<NN;i++){
            Receive(i);
            res += GetLL(i);
        }
    }
    if(id == 0)
        cout<<res<<"\n";
}