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
//2022-12-16
//author: Marcin Rolbiecki
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e5+10;

int n, k;
int P[2][maxN];
int x[2*maxN], y[2*maxN];
int ID [2*maxN], IDsize [2*maxN];
int ciek[12];

int FIND (int x) {
	
	if (x != ID[x])
		ID[x] = FIND(ID[x]);
	return ID[x];
}

void UNION (int a, int b) {
	
	a = FIND(a);
	b = FIND(b);
	
	if (IDsize[a] < IDsize[b]) swap(a, b);
	
	IDsize[a] += IDsize[b];
	ID[b] = ID[a];
}

bool isin (int c, int a, int b) {return c >= a && c <= b;}


int main () {
		
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> k;
	
	for (int i = 1; i <= n; i++) {
		cin >> P[0][i];
		y[ P[0][i] ] = 0;
		x[ P[0][i] ] = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> P[1][i];
		y[ P[1][i] ] = 1;
		x[ P[1][i] ] = i;
	}
	
	P[0][0] = P[0][n];
	P[1][0] = P[1][n];
	P[0][n+1] = P[0][1];
	P[1][n+1] = P[1][1];
	
	for (int a = 1; a <= 2*n; a++) {
		
		for (int i = 0; i <= 2*n; i++)
				ID[i] = i, IDsize[i] = 1;
				
		int c = 0;
		
		for (int b = a; b <= 2*n; b++) {
			
			set <int> u;
			
			if (isin(P[y[b]][x[b]+1], a, b)) u.insert(FIND(P[y[b]][x[b]+1]));
			if (isin(P[y[b]][x[b]-1], a, b)) u.insert(FIND(P[y[b]][x[b]-1]));
			if (isin(P[!y[b]][x[b]], a, b)) u.insert(FIND(P[!y[b]][x[b]]));
			
			c += 1 - u.size();
			
			if (c <= k)
				ciek[c]++;
			
			for (int it : u)
				UNION(b, it);
		}
	}
	
	for (int i = 1; i <= k; i++)
		cout << ciek[i] << ' ';
	
	
	return 0;
}