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
# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll inf = 1e9 + 7;
ll n , k , q;
ll a[2][200005];
ll root[200005] , Rank[200005];
ll get_r(ll x) {
    if (root[x] == x)return x;
    else return get_r(root[x]);
}
void Union(ll x , ll y) {
    ll r1 = get_r(x) , r2 = get_r(y);
    if (r1 == r2)return;
    if (Rank[r1] < Rank[r2])swap(r1 , r2);
    root[r2] = r1;
    Rank[r1] += Rank[r2];
}
bool used[2][200005];
pair<ll , ll> pos[200005];
vector<ll> check(ll i , ll j) {
      vector<ll>arr;
      if (i != 1 && used[i + 1][j])arr.push_back(a[i + 1][j]);
      if (i != 0 && used[i - 1][j])arr.push_back(a[i - 1][j]);
      if (j + 1 < n && used[i][j + 1])arr.push_back(a[i][j + 1]);
      else if (j + 1 == n && used[i][0])arr.push_back(a[i][0]);
      if (j - 1 >= 0 && used[i][j - 1])arr.push_back(a[i][j - 1]);
      else if (j - 1 == -1 && used[i][n])arr.push_back(a[i][n]);
      return arr;
}
void solve2() {
      vector<ll>res(k + 5);
      for (int i = 1; i <= 2 * n; i++) {
              root[i] = i;
              Rank[i] = 1;
              used[pos[i].first][pos[i].second] = 0;
      }
      for (int l = 1; l <= 2 * n; l++) {
     //   cout << "l = " << l << endl;
        ll kol = 0;
        for (int r = l; r <= 2 * n; r++) {
              vector<ll>new_pos = check(pos[r].first , pos[r].second);
         /*     cout << "new_pos = ";
              for (auto val : new_pos)cout << val << ' ';
              cout << endl; */
              if (new_pos.size() == 0)kol++;
              else {
                 for (int i = 0; i < new_pos.size(); i++)new_pos[i] = get_r(new_pos[i]);
                 sort(new_pos.begin() , new_pos.end());
                 ll prev = r , dif = 0;
                 for (int i = 0; i < new_pos.size(); i++) {
                       if (new_pos[i] != prev) {
                            Union(prev , new_pos[i]);
                            dif++;
                       }
                       prev = new_pos[i];
                 }
                 kol -= (dif - 1);
              }
              res[kol]++;
              used[pos[r].first][pos[r].second] = 1;
          //    cout << "r = " << r << " kol = " << kol << " pos.sz = " << new_pos.size() << endl;
          }
          for (int r = l; r <= 2 * n; r++) {
              root[r] = r;
              Rank[r] = 1;
              used[pos[r].first][pos[r].second] = 0;
        }
      }
      for (int i = 1; i <= k; i++) {
            cout << res[i] << ' ';
      }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < n; j++) {
             cin >> a[i][j];
             pos[a[i][j]] = {i , j};
        }
    }
   /* if (k == 1)solve1();
    else */ solve2();
}
/*
4
2827 1074 2431 1152
2900 1968 2994 1633



*/