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
#include <bits/stdc++.h>
#define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define st first
#define nd second
using namespace std;

int n, k, grup, t[2][100005], rankk[200005], parent[200005], sasiedzi[2][100005][3], res[20];
pair<int, int> poz[200005];

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

void UFunion(int x, int y){
	int	parX = UFfind(x);
	int parY = UFfind(y);
	if(parX != parY) grup--;
	if(rankk[parX] < rankk[parY])
		parent[parX] = parY;
	else
		parent[parY] = parent[parX];
	if(rankk[parX] == rankk[parY])
		rankk[parX]++;
}

int main(){
	cin >> n >> k;
	for(int i = 0 ; i <= 1 ; i++){
		for(int j = 1 ; j <= n ; j++){
			cin >> t[i][j];
			poz[t[i][j]] = {i, j};
		}
	}
	for(int i = 0 ; i <= 1 ; i++){
		for(int j = 1 ; j <= n ; j++){
			sasiedzi[i][j][0] = t[1 - i][j];
			if(j == 1) {
				sasiedzi[i][j][1] = t[i][n];
				sasiedzi[i][j][2] = t[i][j + 1];
			}
			else if(j == n) {
				sasiedzi[i][j][1] = t[i][j - 1];
				sasiedzi[i][j][2] = t[i][1];
			}
			else{
				sasiedzi[i][j][1] = t[i][j - 1];
				sasiedzi[i][j][2] = t[i][j + 1];
			}
		}
	}
	for(int i = 1 ; i <= 2 * n ; i++){
		grup = 0;
		for(int j = 1 ; j <= 2* n ; j++){
			rankk[j] = 0;
			parent[j] = j;
		}
		for(int j = i ; j <= 2 * n ; j++){
			grup++;
			for(int l = 0 ; l < 3 ; l++){
				if(sasiedzi[poz[j].st][poz[j].nd][l] >= i && sasiedzi[poz[j].st][poz[j].nd][l] <= j){
					UFunion(j, sasiedzi[poz[j].st][poz[j].nd][l]);
				}
			}
			if(grup <= k)res[grup]++;
		}
	}
	for(int i = 1 ; i <= k ; i++){
		cout << res[i] << " ";
	}
	cout << endl;
}