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
// Błędne rozwiązanie do zadania Działka 2.
// Na pojedynczym węźle wypisuje liczbę dostępnych kwadracików.

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

#include <iostream>
#include <vector>

bool can_extend(int i, int j, int size, std::vector<std::vector<int>> &prefix_sums) {
    return prefix_sums[i + size][j] - prefix_sums[i][j] == size;
}

int main() {
    const int field_height = GetFieldHeight();
    const int field_width = GetFieldWidth();
    const int segment_size = field_height < 150 ? 150 : field_height/NumberOfNodes() + 1;
    std::vector<std::vector<int>> prefix_sums(field_height + 1, std::vector<int>(field_width));
    long long result = 0;

    for (int i = 0; i < field_width; i++) {
        prefix_sums[0][i] = 0;
        for (int j = 1; j <= field_height; j++) {
            prefix_sums[j][i] = prefix_sums[j - 1][i] + IsUsableCell(j-1, i);
        }
    }

    // printf("%d: start: %lld end: %lld field_height: %lld \n", MyNodeId(), segment_size * MyNodeId() + 1, segment_size*MyNodeId() + segment_size, field_height);

    // for (int i = 0; i <= field_height; i++) {
    //     puts("");
    //     for (int j = 0; j <= field_width; j++) {
    //         printf("%d ", prefix_sums[i][j]);
    //     }
    // }

    for (int size = segment_size * MyNodeId() + 1; size <= field_height && size <= segment_size*MyNodeId() + segment_size; size++) {
        for (int i = 0; i < field_height - size + 1; i++) {
            long long length = 0;
            for (int j = 0; j < field_width; j++) {
                if (can_extend(i, j, size, prefix_sums)) {
                    length++;
                } else {
                    result += (length*(length + 1))/2;
                    length = 0;
                }
            }
            result += (length*(length + 1))/2;
        }
    }

    if (MyNodeId() != 0) {
        PutLL(0, result);
        Send(0);
    } else {
        long long results_sum = 0;
        for (int i = 1; i < NumberOfNodes(); ++i) {
            Receive(i);
            long long node_result = GetLL(i);
            results_sum += node_result;
            // printf("%d res: %lld \n", i, node_result);
        }
        printf("%lld\n", results_sum + result);
    }
}