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
#include <vector>
#include <cstdio>

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


int W, H; // width, height
int ww; // widht for this instance
int I; // number of usable instances
int X0;
int MYSELF;

long long A[75000]; // TODO: change into int

void setup() {
    W = GetFieldWidth();
    H = GetFieldHeight();
    I = NumberOfNodes();
    if (I > W) I = W;
    ww = (W+I-1)/I;
    X0 = MyNodeId()*ww;
    MYSELF = MyNodeId();
}

void fillInitialA() {
    for (int y=0; y<H; y++) {
        A[y] = 0;
        for (int x=X0+ww+ww-1; x>=X0+ww; x--)
            if (x>=W || !IsUsableCell(y, x))
                A[y] = 0;
            else
                A[y]++;
    }
}

void sendA(int target) {
    for (int y=0; y<H; y++) {
        if (y % 5000 == 4999) Send(target); // workaround for SIO limit of message size
        PutLL(target, A[y]);
    }
    Send(target);
}

void receiveA(int source, long long dd) {
    Receive(source);
    for (int y=0; y<H; y++) {
        if (y % 5000 == 4999) Receive(source); // workaround for SIO limit of message size
        if (A[y] == dd)
            A[y] += GetLL(source);
        else
            GetLL(source);
    }
}
    

void propagateA() {
    int d = 1;
    while (d<I) {
        if (MYSELF-d >= 0)
            sendA(MYSELF-d);
        if (MYSELF+d < I)
            receiveA(MYSELF+d, ww*d);
        d*=2;
    }
}

struct Info {
    int y;
    int a;
    long long area;
    Info(int y, int a, long long area) : y(y), a(a), area(area) {}
};

long long result = 0;
void calc() {
    std::vector<Info> infos;
    for (int x=X0+ww-1; x>=X0; x--) {
        if (x>=W) continue;
        infos.clear();
        infos.push_back(Info(-1, 0, 0));
        for (int y=0; y<H; y++) {
            
            if (IsUsableCell(y, x)) {
                A[y]++;
                while (infos.back().a >= A[y])
                    infos.pop_back();
                infos.push_back(Info(y, A[y],
                    infos.back().area + (long long)(A[y])*(y-infos.back().y)));
                result += infos.back().area;
            }
            
            else {
                A[y] = 0;
                infos.clear();
                infos.push_back(Info(y, 0, 0));
            }
        }
    }
    
    if (MyNodeId() > 0) {
        PutLL(0, result);
        Send(0);
    }
}



int main() {
    
    setup();
    if (MyNodeId() >= I)
        return 0;
    
    fillInitialA();
    propagateA();
    calc();
    
    if (MyNodeId() > 0)
        return 0;
    
    for (int i=1; i<I; i++) {
        Receive(i);
        result += GetLL(i);
    }
    
    printf("%lld\n", result);
    
    return 0;
}