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
131
#include <iostream>
#include <utility>
#include <set>

constexpr int maxN = 1e5;
constexpr int maxK = 10;

int n, k;
int canvas[2][maxN + 1];

std::pair<bool, int> where[maxN + 1];
bool used[maxN + 1];
int set[maxN + 1], size[maxN + 1];
int cnt;

int ans[maxK + 1];

void clear() {
    for(int i=1; i<=2*n; i++) {
        used[i] = false;
        set[i] = i, size[i] = 1;
    }

    cnt = 0;
}

int findSet(int v) {
    if(set[v] == v) {
        return v;
    }

    return set[v] = findSet(set[v]);
}

void unionSets(int a, int b) {
    a = findSet(a);
    b = findSet(b);

    if(a != b) {
        if(size[a] < size[b]) {
            std::swap(a, b);
        }

        set[b] = a;
        size[a] += size[b];
    }
}

void add(int v) {
    int v1, v2, v3;

    if(where[v].second == 1) {
        v1 = canvas[where[v].first][n];
    }
    else {
        v1 = canvas[where[v].first][where[v].second - 1]; 
    }

    v2 = canvas[!where[v].first][where[v].second];

    if(where[v].second == n) {
        v3 = canvas[where[v].first][1];
    }
    else {
        v3 = canvas[where[v].first][where[v].second + 1];
    }

    std::set<int> S;

    if(used[v1]) {
        S.insert(findSet(v1));
    }

    if(used[v2]) {
        S.insert(findSet(v2));
    }

    if(used[v3]) {
        S.insert(findSet(v3));
    }

    if(S.empty()) {
        cnt++;
    }
    else if(S.size() == 2) {
        cnt--;
    }
    else if(S.size() == 3) {
        cnt -= 2;
    }

    used[v] = true;
    
    for(auto u : S) {
        unionSets(v, u);
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    std::cin >> n >> k;

    for(int i=1; i<=n; i++) {
        std::cin >> canvas[0][i];
        where[canvas[0][i]] = {0, i};
    }

    for(int i=1; i<=n; i++) {
        std::cin >> canvas[1][i];
        where[canvas[1][i]] = {1, i};
    }

    for(int i=1; i<=2*n; i++) {
        clear();

        for(int j=i; j<=2*n; j++) {
            add(j);

            if(cnt <= k) {
                ans[cnt]++;
            }
        }
    }

    for(int i=1; i<=k; i++) {
        std::cout << ans[i] << ' ';
    }
}