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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include<bits/stdc++.h>
using namespace std;
#ifdef LOC
template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; }
template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) {
    out << "{";
    bool fi = 1;
    for(auto b : a) {
        if(fi) {out<<b; fi=0;}
        else out<<", "<<b;
    }
    return out << "}";
}

template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; }
void dump(){
   cerr << "\e[39m"<<"\n";
}
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define fwd(i, a, b) for(int i=(a);i<(int)(b);i++)
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;

pii swapIfReversed(pii a) {
    if(a.st > a.nd)
        return {a.st, a.nd};
    return {a.nd, a.st};
}

int getLargestPossibleMinValueForEndsConnectingPath(const vector<vi>& tab, int n) {
    int start = max(tab[0][0], tab[0][1]);
    int posI = 0, posJ = start == tab[0][0] ? 0 : 1;
    int res = start;
    while(posI < n) {
        if(tab[posI+1][posJ] > min(tab[posI][posJ^1], tab[posI+1][posJ^1]))
            res = min(res, tab[posI+1][posJ]);
        else {
            res = min(res, min(tab[posI][posJ^1], tab[posI+1][posJ^1]));
            posJ^= 1;
        }
        posI++;
    }
    return res;
}

const int ARR_SIZE = 11;
typedef array<int, ARR_SIZE> AR;
const AR EMPTY_AR = {{}};

struct PlusMinTree {
    int siz;
    vector<AR> countOfMinPlusK;
    vector<ll> mini;
    vector<ll> lazy;

    PlusMinTree(int size): siz(size) {
        assert(__builtin_popcount(size) == 1);
        countOfMinPlusK.resize(2*siz,EMPTY_AR);
        for(auto&a : countOfMinPlusK) a.fill(0);
        mini.resize(2*siz,0);
        lazy.resize(2*siz,0);
        build(1,0,siz-1);
    }

    void build(int node, int l, int r) {
        countOfMinPlusK[node][0] = r-l+1;
        if(l == r)
            return;
        int m = (l+r)/2;
        build(2*node, l, m);
        build(2*node+1, m+1, r);
    }

    AR combineQuery(AR v1, const AR& v2) {
        rep(i,ARR_SIZE)
            v1[i] += v2[i];
        return v1;
    }

    void tag(int val, int node, int l, int r) {
        lazy[node] += val;
        mini[node] += val;
    }

    void push(int node, int l, int r) {
        if(lazy[node] != 0 && l != r) {
            lazy[2*node] += lazy[node];
            lazy[2*node+1] += lazy[node];
            mini[2*node] += lazy[node];
            mini[2*node+1] += lazy[node];
            lazy[node] = 0;
        }
    }

    void _update(int a, int b, int val, int node, int l, int r) {
        if(r < a || l > b)
            return;
        if(a <= l && b >= r) {
            tag(val,node,l,r);
            return;
        }
        push(node,l,r);
        int m = (l+r)/2;
        _update(a,b,val,2*node,l,m);
        _update(a,b,val,2*node+1,m+1,r);

        mini[node] = min(mini[2*node], mini[2*node+1]);
        countOfMinPlusK[node] = EMPTY_AR;

        int idx = 0;
        fwd(i,mini[2*node]-mini[node],ARR_SIZE)
            countOfMinPlusK[node][i] += countOfMinPlusK[2*node][idx++];
        idx = 0;
        fwd(i,mini[2*node+1]-mini[node],ARR_SIZE)
            countOfMinPlusK[node][i] += countOfMinPlusK[2*node+1][idx++];
    }
    AR _query(int a, int b, int node, int l, int r) {
        if(r < a || l > b)
            return EMPTY_AR;
        if(a <= l && b >= r) {
            AR toReturn = EMPTY_AR;
            int idx=0;
            fwd(i,mini[node], ARR_SIZE)
                toReturn[i] += countOfMinPlusK[node][idx++];
            return toReturn;
        }
        push(node,l,r);
        int m = (l+r)/2;
        return combineQuery(_query(a,b,2*node,l,m),_query(a,b,2*node+1,m+1,r));
    }
    
    void update(int a, int b, int val) {if(a <= b && a >= 0) _update(a,b,val,1,0,siz-1);}
    AR query(int a, int b) {return _query(a,b,1,0,siz-1);}
};

struct Sweep {
    Sweep(int n) : tab(n,0){}
    vi tab;
    void add(int l, int r, int v) {
        fwd(i,l,r+1)
            tab[i] += v;
    }
    int count(int v, int R) {
        int cnt=0;
        rep(i, R+1)
            if(tab[i] == v)
                cnt++;
        return cnt;
    } 
};

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin >> n >> k;

    vector<vi> tab(n, vi(2));
    vector<pii> where(2*n);

    rep(i,n) {cin >> tab[i][0]; tab[i][0]--; where[tab[i][0]] = {i,0};}
    rep(i,n) {cin >> tab[i][1]; tab[i][1]--; where[tab[i][1]] = {i,1};}
    tab.push_back(tab[0]);

    vector<pair<pii, int> > events;
    vi dp(2*n,-1);
    dp[2*n-1] = getLargestPossibleMinValueForEndsConnectingPath(tab, n);
    for(int i=2*n-1;i>0;i--) {
        int pI = where[i].st;
        int pJ = where[i].nd;
        if(tab[pI][pJ^1] > tab[pI][pJ] || tab[pI+1][pJ^1] > tab[pI][pJ] || tab[pI == 0 ? n-1 : pI-1][pJ^1] > tab[pI][pJ])
            break;
        dp[i-1] = min({dp[i], tab[pI][pJ^1], tab[pI+1][pJ^1], tab[pI == 0 ? n-1 : pI-1][pJ^1]});
    }
    fwd(i,1,2*n) {
        if(dp[i-1] != -1)
            events.push_back({{i,dp[i-1]}, 1});
        if(dp[i] != -1)
            events.push_back({{i,dp[i]}, -1});
    }
    rep(i,n) {
        int m = min({tab[i][0], tab[i][1], tab[i+1][0], tab[i+1][1]});
        int M = max({tab[i][0], tab[i][1], tab[i+1][0], tab[i+1][1]});
        
        events.push_back({swapIfReversed({tab[i][0], tab[i+1][0]}),1});
        events.push_back({swapIfReversed({tab[i][1], tab[i+1][1]}),1});
        events.push_back({swapIfReversed({tab[i][0], tab[i][1]}),1});

        events.push_back({swapIfReversed({m,M}),-1});
        events.push_back({{tab[i][0], tab[i][0]},-1});
        events.push_back({{tab[i][1], tab[i][1]},-1});
    }
   
    sort(all(events));
    int epos = 0;
    vector<ll> ans(ARR_SIZE);
    PlusMinTree sweep((1<<18));
    rep(i,2*n) {
        while(epos < sz(events) && events[epos].st.st == i) {
            sweep.update(0, events[epos].st.nd, -events[epos].nd);
            epos++;
        }
        auto qr = sweep.query(0,i);
        fwd(j, 1, ARR_SIZE)
            ans[j] += qr[j];
    }
    fwd(i,1,k+1)
        cout<<ans[i]<<" ";
}