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
/*
    Zadanie: Płótno
    Autor: Tomasz Kwiatkowski
*/

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;
typedef long long ll;

const int MAXN = 1e5 + 7;
const int INF = 1e9 + 7;

int arr[MAXN][2];
vector<int> G[2*MAXN];

struct FAU {
    vector<int> R;
    vector<int> rank;
    int _find(int v) { return (R[v] != v ? R[v] = _find(R[v]) : R[v]); }
    bool connect(int u, int v)
    {
        int a = _find(u);
        int b = _find(v);
        if (a == b) return false;
        if (rank[a] < rank[b])
            swap(a, b);
        rank[a] += rank[b];
        R[b] = a;
        return true;
    }
    FAU(int n)
    {
        R.resize(n + 1);
        for (int i = 1; i <= n; ++i) R[i] = i;
        rank.resize(n + 1, 1);
    }
};

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

    int n, k;
    cin >> n >> k;
    for (int r = 0; r < 2; ++r) {
        for (int i = 0; i < n; ++i)
            cin >> arr[i][r];
    }
    for (int r = 0; r < 2; ++r) {
        for (int i = 0; i < n; ++i)
            G[arr[i][r]] = {arr[(i + n - 1) % n][r], arr[(i + 1) % n][r], arr[i][(r + 1) % 2]};
    }
    vector<int> ans(k);
    for (int l = 1; l <= 2*n; ++l) {
        FAU F(2*n);
        int c = 0;
        for (int r = l; r <= 2*n; ++r) {
            ++c;
            for (auto u : G[r]) {
                if (l <= u && u <= r)
                    c -= F.connect(r, u);
            }
            if (0 < c && c <= k)
                ++ans[c - 1];
        }
    }
    for (auto c : ans)
        cout << c << ' ';
    cout << '\n';
    return 0;
}