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
#include <cstdio>
#include <vector>
#include "message.h"
#include "dzialka.h"
using namespace std;

struct Rect {
    int width, height;
};

int width, height;

int is_usable(int row, int col) {
    if(row == 0 || col == 0 || col > width) return 0;
    return IsUsableCell(row - 1, col - 1);
}

int main() {
    if (MyNodeId() > 0) return 0;
    width = GetFieldWidth(), height = GetFieldHeight();
    int dist[75002];
    for(int i = 0; i <= width + 1; ++i) dist[i] = 0;
    long long result = 0;
    for(int row = 1; row <= height; row++) {
        vector<Rect> stack;
        for(int col = 0; col <= width + 1; col++) {
            dist[col] = is_usable(row, col) ? dist[col] + 1 : 0;
            //printf("(%d, %d) - %d - dist=%d res=%lld\n", row, col, is_usable(row, col), dist[col], result);
            int w = 0, hei;
            for(auto it = stack.rbegin(); it != stack.rend() && (*it).height > 0; it++) {
                Rect cur = *it;
                hei = (*(it + 1)).height;
                w += cur.width;
                result += w * (cur.height - hei);
            }
            w = 0;
            hei = dist[col];
            while(stack.size() > 0 && stack.back().height >= dist[col]) {
                Rect top = stack.back();
                stack.pop_back();
                if(!stack.empty()) hei = max(hei, stack.back().height);
                w += top.width;
            }
            stack.push_back({w + 1, dist[col]});
        }
    }
    printf("%lld\n", result);
    return 0;
}