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
#include <iostream>
#include <functional>
#include <vector>
#include <set>
using namespace std;

struct UnionFind {
    vector<int>parent;
    vector<int>rank;

    UnionFind(int n) {
        parent.resize(n);
        rank.resize(n);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            rank[i] = 0;
        }
    }

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

    void merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (rank[x] < rank[y])
            parent[x] = y;
        else if (rank[x] > rank[y])
            parent[y] = x;
        else {
            parent[x] = y;
            rank[y]++;
        }
    }
};

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

    // input
    int n, k;
    cin >> n >> k;
    int NC = 2*n;

    vector<int>Ribbon[2] = {vector<int>(n), vector<int>(n)};
    for (int &x : Ribbon[0]) {
        cin >> x;
        x--;
    }
    for (int &x : Ribbon[1]) {
        cin >> x;
        x--;
    }
    vector<pair<int,int>> ColToPos(NC); // row, col
    for (int row = 0; row <= 1; row++)
        for (int col = 0; col < n; col++)
            ColToPos[Ribbon[row][col]] = {row, col};


    // faza 1 - symulacja [0, ...]
    vector <long long> Result(k+1);
    for (int start = 0; start < NC; start++) {
        vector<int>EndRangeVector(NC);
        UnionFind UF(NC);

        EndRangeVector[start] = 1;
        Result[1]++;
        for (int i = start + 1; i < NC; i++) {
            int r = ColToPos[i].first;
            int c = ColToPos[i].second;

            // sprawdź sąsiadów i
            set<int>neis;
            auto check_nei = [&](int rr, int cc) {
                int j = Ribbon[rr][cc];
                if (j < i && j >= start)
                    neis.insert(UF.find(j));
            };
            check_nei(1 - r, c);
            check_nei(r, (c + 1) % n);
            check_nei(r, (c + n - 1) % n);

            // oblicz wynik
            if (neis.size() == 0)
                EndRangeVector[i] = EndRangeVector[i-1] + 1;
            else if (neis.size() == 1)
                EndRangeVector[i] = EndRangeVector[i-1];
            else
                EndRangeVector[i] = EndRangeVector[i-1] - neis.size() + 1;

            if (EndRangeVector[i] <= k)
                Result[EndRangeVector[i]]++;

            // scal grupy
            auto merge_nei = [&](int rr, int cc) {
                int j = Ribbon[rr][cc];
                if (j < i && j >= start)
                    UF.merge(i, j);
            };
            merge_nei(1 - r, c);
            merge_nei(r, (c + 1) % n);
            merge_nei(r, (c + n - 1) % n);
        }
    }

    for (int i = 1; i <= k; i++)
        cout << Result[i] << " ";
    cout << "\n";

    return 0;
}