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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <math.h>
#include <stdio.h>
#include <deque>
#include <unordered_map>
#include <cassert>
#include <numeric>
#include <queue>
#include <list>

using namespace std;


struct UnionFind {
    vector<int> parent;
    vector<int> size;
    int distinct_sets = 0;

    UnionFind(int n) {
        parent.resize(n, -1);
        size.resize(n, -1);
    }

    void make_set(int v) {
        assert (v >= 0 && v < parent.size());
        parent[v] = v;
        size[v] = 1;
        distinct_sets++;
    }

    void union_sets(int a, int b) {
        a = find_set(a);
        b = find_set(b);
        if (a != b) {
            if (size[a] < size[b])
                swap(a, b);
            parent[b] = a;
            size[a] += size[b];
            distinct_sets--;

            //printf("union_sets(%d,%d) distinct=%d\n", a+1, b+1, distinct_sets);
        }
    }

    bool contains(int v) {
        return parent[v] != -1;
    }

    int find_set(int v) {
        
        if (v == parent[v])
            return v;
        return parent[v] = find_set(parent[v]);
    }

    void clean() {
        for (int i=0; i < (int)parent.size(); i++) {
            make_set(i);
        }
    }
};


vector<int> getAdj(const vector<vector<int> > &rows, pair<int, int> pos) {
    const int n=rows[0].size();
    assert (pos.first >= 0 && pos.first < 2);
    assert (pos.second >=0 && pos.second < rows[0].size());
    int left = pos.second - 1;
    int right = pos.second + 1;
    if (left < 0) left += n;
    if (right >= n) right -= n;
    int vertical = pos.first + 1;
    if (vertical == 2) vertical = 0;

    int a = rows[pos.first][left];
    int b = rows[pos.first][right];
    int c = rows[vertical][pos.second];
    if (a == b) return {a,c};
    return {a,b,c};
}

bool within_interval(int elem, int left, int right) {
    return elem >= left && elem <= right;
}

void print_vec(const vector<int> &v, string msg) {
    printf("%s: ", msg.c_str());
    for (int i=0; i<v.size(); i++) {
        printf("%d ", v[i] + 1);
    }
    printf("\n");
}


vector<int> solveSlow(const vector<vector<int> > &rows, const int K) 
{
    const int n = rows[0].size();
    const int twoN = 2 * n;
    assert (rows.size() == 2);
    vector<pair<int, int> > whereIs(twoN, {-1,-1});
    for (int r=0; r < 2; r++) {
        for (int i=0; i < n; i++) {
            int elem = rows[r][i];
            whereIs[elem] = {r,i};
        }
    }
    vector<int> res(K, 0);
    for (int left=0; left < twoN; left++) {
        UnionFind uf(twoN);
        for (int right=left; right < twoN; right++) {
            int current_v = right;
            //printf("processing v: %d\n", current_v+1);
            uf.make_set(current_v);
            pair<int,int> pos = whereIs[current_v];

            vector<int> adj = getAdj(rows, pos);
            //print_vec(adj, "adj");
            for (auto x : adj) {
                if (uf.contains(x)) {
                    uf.union_sets(current_v, x);
                }
            }
            int ciekawosPrzedzialu = uf.distinct_sets;
            assert (ciekawosPrzedzialu >= 1);
            if (ciekawosPrzedzialu <= K) {
                res[ciekawosPrzedzialu-1]++;
                //printf("interval[%d,%d]->%d\n", left+1, right+1, ciekawosPrzedzialu);
            }
        }
    }
    return res;
}

int main() {

    vector<vector<int> > rows(2);
    int n, k;
    scanf("%d%d", &n, &k);

    for (int j=0; j < 2; j++)  {
        for (int i=0; i < n; i++)
        {
            int x;
            scanf("%d", &x);
            rows[j].push_back(x-1);
        }
    }
    vector<int> res = solveSlow(rows, k);
    for (int i=0; i < (int)res.size(); i++) {
        printf("%d ", res[i]);
    }
    printf("\n");
    return 0;
}