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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Marcin Knapik

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;}
template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;}

template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;}


using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;

const int INF = 1e9 + 7;
const int N = 100 * 1000 + 4;
const int SQ = 500;

int ile[(N * 2) / SQ + 2][N];
int lazy[(N * 2) / SQ + 2];
int tab[N][2];

int n, k;

ii neighs[N][2][3];
ii corners[N][2][2];
// 0 graniczy z neighs 0 i 1, 1 graniczy z neighs 1 i 2
// UWAGA NA N == 2
ii where[N * 2];

int prev_cont[N * 2];
int val_pref[N * 2];
int ktory_kobek[N * 2];
int max_kubek;

int alive_neigh[3];
int alive_corner[2];

int contribution(int val){
    ii pos = where[val];
    int ret = 0;

    bool najm = true;
    FOR(i, 3){
        ii ne = neighs[pos.f][pos.s][i];
        if(tab[ne.f][ne.s] < val){
            najm = false;
        } 
    }

    ret += najm;

    FOR(i, 3){
        ii ne = neighs[pos.f][pos.s][i];
        alive_neigh[i] = tab[ne.f][ne.s] < val;
    }

    FOR(i, 2){
        ii ne = corners[pos.f][pos.s][i];
        alive_corner[i] = tab[ne.f][ne.s] < val;
    }

    if(n == 2){
        if(alive_neigh[0] and alive_neigh[1] and not alive_corner[0]){
            ret--;
        }
    } else {
        int spoj = alive_neigh[0] + alive_neigh[1] + alive_neigh[2];
        if(alive_neigh[0] and alive_neigh[1])
            spoj -= alive_corner[0];
        if(alive_neigh[1] and alive_neigh[2])
            spoj -= alive_corner[1];
        ret -= max(0, spoj - 1);
    }

    return ret;
}

int ask(int kub, int val){
    // cout << "ASK " << kub << ' ' << val <<  "   lazy: " << lazy[kub] << endl;
    val = val - lazy[kub];
    if(val < 0){
        // cout << "NEGATIVE " << endl;
        return 0;
    }
    // cout << "WYNIK TO  " << ile[kub][val] << endl;
    return ile[kub][val];
}

void relax_kubek(int kub){
    if(lazy[kub] == 0){
        return;
    }
    for(int i = kub * SQ; i < min((kub + 1) * SQ, n * 2); i++){
        if(val_pref[i] == INF)
            continue;

        assert(val_pref[i] >= 0);
        ile[kub][val_pref[i]]--;
        val_pref[i] += lazy[kub];
        assert(val_pref[i] >= 0);
        ile[kub][val_pref[i]]++;
    }

    lazy[kub] = 0;
}

void add(int poz, int val){
    int kub = ktory_kobek[poz];
    relax_kubek(kub);
    for(int i = poz; i < min((kub + 1) * SQ, n * 2); i++){
        assert(val_pref[i] >= 0);
        ile[kub][val_pref[i]]--;
        val_pref[i] += val;
        assert(val_pref[i] >= 0);
        ile[kub][val_pref[i]]++;
    }
    for(int i = kub + 1; i <= max_kubek; i++){
        lazy[i] += val;
    }
}

ll ans[11];

void wypisz_ans(){
    for(int i = 1; i <= k; i++){
        cout << ans[i] << ' ';
    }
    cout << '\n';
}

void solve () {
    cin >> n >> k;
    max_kubek = (n * 2 - 1) / SQ;

    FOR(j, 2){
        FOR(i, n){
            cin >> tab[i][j]; tab[i][j]--;
            where[tab[i][j]] = {i, j};

            neighs[i][j][0] = {(i + n - 1) % n, j};
            neighs[i][j][1] = {i, j ^ 1};
            neighs[i][j][2] = {(i + 1) % n, j};

            corners[i][j][0] = {(i + n - 1) % n, j ^ 1}; 
            corners[i][j][1] = {(i + 1) % n, j ^ 1};
        }
    }

    FOR(val, 2 * n){
        ktory_kobek[val] = val / SQ;
        prev_cont[val] = contribution(val);
        val_pref[val] = (val == 0 ? 0 : val_pref[val - 1]) + prev_cont[val];
        assert(val_pref[val] >= 0);
        ile[ktory_kobek[val]][val_pref[val]]++;
    }

    FOR(i, 2 * n){
        // relax_kubek(0);
        // relax_kubek(1);
        // relax_kubek(2);
        
        // dodaj wartosci tego goscia
        for(int kub = ktory_kobek[i]; kub <= max_kubek; kub++){
            for(int val = 1; val <= k; val++){
                ans[val] += ask(kub, val);
            }
            ans[1] += ask(kub, 0);
        }

        // cout << "STAN VAL_PREF " << endl;
        // FOR(j, i){
        //     cout << ' ' << ' ';
        // }
        // for(int j = i; j < 2 * n; j++){
        //     cout << val_pref[j] << ' ';
        // }
        // cout << endl;

        // cout << "STAN LZYYY " << endl;
        // FOR(j, i){
        //     cout << ' ' << ' ';
        // }
        // for(int j = i; j < 2 * n; j++){
        //     cout << lazy[ktory_kobek[j]] << ' ';
        // }
        // cout << endl;
        
        // cout << "PREF CONTRIBUTION " << endl;
        // FOR(j, i){
        //     cout << ' ' << ' ';
        // }
        // for(int j = i; j < 2 * n; j++){
        //     cout << prev_cont[j] << ' ';
        // }
        // cout << endl << endl;

        assert(val_pref[i] >= 0);
        ile[ktory_kobek[i]][val_pref[i]]--;
        val_pref[i] = INF;
        ii pos = where[i];
        tab[pos.f][pos.s] = INF;
        
        vii to_add;
        if(i < n * 2 - 1){
            to_add.pb({i + 1, -1});
            // add(i + 1, -1);

            auto check_contr = [&to_add](ii ne){
                int neigh_val = tab[ne.f][ne.s];
                if(neigh_val == INF)
                    return;

                int cont_teraz = contribution(neigh_val);
                if(cont_teraz != prev_cont[neigh_val]){
                    // add(neigh_val, cont_teraz - prev_cont[neigh_val]);
                    to_add.pb({neigh_val, cont_teraz - prev_cont[neigh_val]});
                    prev_cont[neigh_val] = cont_teraz;
                }
            };

            FOR (neigh, 3) {
                check_contr(neighs[pos.f][pos.s][neigh]);
            }

            FOR (corner, 2){
                check_contr(corners[pos.f][pos.s][corner]);
            }
        }
        sort(all(to_add), [](ii a, ii b){
            return a.s > b.s;
        });

        for(auto & [a, b] : to_add){
            add(a, b);
        }

        // wypisz_ans();
        // for(int j = 1; j <= k; j++){
        //     ans[j] = 0;
        // }
    }

    wypisz_ans();
}

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++) {
        solve();
    }

    return 0;
}