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
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;

const int N = 100000;

class DisjointSet {
public:
    vi parent;
    vi set_size;
    int size;

    DisjointSet(int n) : parent(n, -1), set_size(n, 0), size(0) {}

    void merge(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) {
            return;
        }

        if (set_size[a] < set_size[b]) {
            swap(a, b);
        }
        parent[b] = a;
        set_size[a] += set_size[b];
        --size;
    }

    int find(int a) {
        if (parent[a] == a) {
            return a;
        }
        parent[a] = find(parent[a]);
        return parent[a];
    }

    void create(int a) {
        parent[a] = a;
        set_size[a] = 0;
        ++size;
    }

    void clear() {
        for (size_t i = 0; i < parent.size(); ++i) {
            parent[i] = -1;
            set_size[i] = 0;
        }
        size = 0;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int canvas[N][2];
    pii neighbors[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

    int n, k;
    cin >> n >> k;

    vector<pii> color_indeces(2 * n);
    for (int i = 0; i < n; ++i) {
        cin >> canvas[i][0];
        --canvas[i][0];
        color_indeces[canvas[i][0]] = make_pair(i, 0);
    }
    for (int i = 0; i < n; ++i) {
        cin >> canvas[i][1];
        --canvas[i][1];
        color_indeces[canvas[i][1]] = make_pair(i, 1);
    }

    vector<int> result(k + 1);
    DisjointSet set = DisjointSet(2 * n);
    for (int i = 0; i < 2 * n; ++i) {
        set.clear();
        for (int j = i; j < 2 * n; ++j) {
            set.create(j);

            // check neighbors and merge
            pii coords = color_indeces[j];
            for (int neigh_idx = 0; neigh_idx < 4; ++neigh_idx) {
                pii diff = neighbors[neigh_idx];
                int new_x = (coords.first + diff.first);
                int new_y = (coords.second + diff.second);
                if (new_y < 0 || new_y >= 2) {
                    continue;
                }
                if (new_x >= n) {
                    new_x = 0;
                }
                if (new_x < 0) {
                    new_x = n - 1;
                }

                int neigh = canvas[new_x][new_y];
                if (neigh >= i && neigh <= j) {
                    set.merge(j, neigh);
                }
            }

            if (set.size <= k) {
                result[set.size] += 1;
            }
        }
    }

    for (int i = 1; i <= k; ++i) {
        cout << result[i] << ' ';
    }
    cout << '\n';

    return 0;
}