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

using namespace std;

int n, k;
int canvas[2][100100];
int mask1 = 0x7FFFFFFF;
int mask2 = 0x80000000;
int mask3 = 0xBFFFFFFF;
int mask4 = 0x40000000;

int locations[200200][2];
int res[11];

int dfs(int y, int x) {
    int r = 0;
    if((canvas[y][x] & mask2) && ((canvas[y][x] & mask4) == 0)) {
        canvas[y][x] |= mask4;
        r++;

        if(y == 0) {
            r += dfs(0, (x + 1)%n);
            if(x == 0) {
                r += dfs(0, n - 1);
            } else {
                r += dfs(0, x - 1);
            }
            r += dfs(1, x);
        } else {
            r += dfs(1, (x + 1)%n);
            if(x == 0) {
                r += dfs(1, n - 1);
            } else {
                r += dfs(1, x - 1);
            }
            r += dfs(0, x);
        }
    }
    return r;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k;

    for(int i = 0; i < n; i++) {
        cin >> canvas[0][i];
        locations[canvas[0][i]][0] = 0;
        locations[canvas[0][i]][1] = i;
    }
    for(int i = 0; i < n; i++) {
        cin >> canvas[1][i];
        locations[canvas[1][i]][0] = 1;
        locations[canvas[1][i]][1] = i;
    }

    for(int p = 1; p <= 2*n; p++) {
        for(int q = p; q <= 2*n; q++) {
            for(int i = p; i <= q; i++) {
                canvas[locations[i][0]][locations[i][1]] |= mask2;
            }

            for(int i = 0; i < n; i++) {
                canvas[0][i] &= mask3;
                canvas[1][i] &= mask3;
            }

            int c = 0;
            for(int i = p; i <= q; i++) {
                c += dfs(locations[i][0], locations[i][1]) > 0;
            }
            res[c]++;

            for(int i = p; i <= q; i++) {
                canvas[locations[i][0]][locations[i][1]] &= mask1;
            }
        }
    }

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

    return 0;
}