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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <cstdio>
#include <algorithm>

const int max_n = 524289;
const int czapa = 262144;
const int inf = 1000000000;

struct TreeNode {
  int delta;
  int min_value;
  long long counts[11];
};

TreeNode tree[max_n];

int n, k;
int in[max_n][2];
long long answer[11];

int findValue[max_n][2];

int pq[2 * max_n];

int findD(int p) {
  if (pq[p] == p) return p;
  else return pq[p] = findD(pq[p]);
}

bool uniteD(int p, int q) {
  p = findD(p);
  q = findD(q);
  if (p != q) {
    pq[q] = p;
    return true;
  }
  return false;
}


int verify(int L, int P) {
  for (int i = L; i <= P; ++i) pq[i] = i;
  int ret = 0;
  int cnt0 = 0;
  for (int W = P; W >= L; --W) {
    int i = findValue[W][0];
    int j = findValue[W][1];
        if (i > 0 && in[i - 1][j] >= W && in[i - 1][j] <= P) {
          ret += uniteD(in[i][j], in[i - 1][j]);
        }
        if (i == 0 && in[n - 1][j] >= W && in[n - 1][j] <= P) {
          ret += uniteD(in[i][j], in[n - 1][j]);
        }
        if (i + 1 < n && in[i + 1][j] >= W && in[i + 1][j] <= P) {
          ret += uniteD(in[i][j], in[i + 1][j]);
        }
        if (i + 1 == n && in[0][j] >= W && in[0][j] <= P) {
          ret += uniteD(in[i][j], in[0][j]);
        }
        if (in[i][1 - j] >= W && in[i][1 - j] <= P) {
          ret += uniteD(in[i][j], in[i][1 - j]);
        }
    if (P - W + 1 - ret == 1) cnt0++;
  }
  return cnt0;
}

void rangeAdd(int index, int nodeL, int nodeR, int left, int right, int delta) {
//  printf("RA %d %d %d %d %d %d\n", index, nodeL, nodeR, left, right, delta);
//  printf("RA %d %d\n", tree[index].min_value, tree[index].delta);
//  for(int j = 0; j < 11; ++j) {
//    printf("%lld ", tree[index].counts[j]);
//  }
//  printf("\n");
  if (left <= nodeL && nodeR <= right) {
    // All inclusive
    tree[index].delta += delta;
    return;
  }
  tree[index + index].delta += tree[index].delta;
  tree[index + index + 1].delta += tree[index].delta;
  tree[index].delta = 0;
  int nodeMiddle = (nodeL + nodeR) / 2;
  if (left <= nodeMiddle) {
    rangeAdd(index + index, nodeL, nodeMiddle, left, std::min(right, nodeMiddle), delta);
  }
  if (right > nodeMiddle) {
    rangeAdd(index + index + 1, nodeMiddle + 1, nodeR, std::max(left, nodeMiddle + 1), right, delta);
  }
  for (int j = 0; j < 11; ++j) tree[index].counts[j] = 0;
//  printf("%d vs %d\n", tree[index + index].min_value + tree[index + index].delta, tree[index + index + 1].min_value + tree[index + index + 1].delta);
  tree[index].min_value = std::min(tree[index + index].min_value + tree[index + index].delta, tree[index + index + 1].min_value + tree[index + index + 1].delta);
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index].min_value + tree[index + index].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index].counts[j];
  }
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index + 1].min_value + tree[index + index + 1].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index + 1].counts[j];
  }
//  printf("REA %d %d %d %d %d %d\n", index, nodeL, nodeR, left, right, delta);
//  printf("REA %d %d\n", tree[index].min_value, tree[index].delta);
//  for(int j = 0; j < 11; ++j) {
//    printf("%lld ", tree[index].counts[j]);
//  }
//  printf("\n");
}

int curr_min;
long long counts[12];

int cp_curr_min;
long long cp_counts[12];

void resetElem(int index, int nodeL, int nodeR, int ps) {
  if (ps <= nodeL && nodeR <= ps) {
    // All inclusive
    tree[index].min_value = 0;
    tree[index].delta = 0;
    for (int j = 0; j < 11; ++j) {
      tree[index].counts[j] = 0;
    }
    tree[index].counts[0] = 1;
    return;
  }
  tree[index + index].delta += tree[index].delta;
  tree[index + index + 1].delta += tree[index].delta;
  tree[index].delta = 0;

  int nodeMiddle = (nodeL + nodeR) / 2;
  if (ps <= nodeMiddle) {
    resetElem(index + index, nodeL, nodeMiddle, ps);
  }
  if (ps > nodeMiddle) {
    resetElem(index + index + 1, nodeMiddle + 1, nodeR, ps);
  }
  // TODO: merge back
  for (int j = 0; j < 11; ++j) tree[index].counts[j] = 0;
  tree[index].min_value = std::min(tree[index + index].min_value + tree[index + index].delta, tree[index + index + 1].min_value + tree[index + index + 1].delta);
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index].min_value + tree[index + index].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index].counts[j];
  }
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index + 1].min_value + tree[index + index + 1].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index + 1].counts[j];
  }
//  printf("WWW %d %d %d %d\n", index, nodeL, nodeR, ps);
//  printf("WWW %d %d\n", tree[index].min_value, tree[index].delta);
//  for(int j = 0; j < 11; ++j) {
//    printf("%lld ", tree[index].counts[j]);
//  }
//  printf("\n");
}

void rangeQuery(int index, int nodeL, int nodeR, int left, int right) {
//  printf("RQ %d %d %d %d %d\n", index, nodeL, nodeR, left, right);
//  printf("RQ %d %d\n", tree[index].min_value, tree[index].delta);
//  for(int j = 0; j < 11; ++j) {
//    printf("%lld ", tree[index].counts[j]);
//  }
//  printf("\n");
  if (left <= nodeL && nodeR <= right) {
    // All inclusive
    cp_curr_min = curr_min;
    for (int j = 0; j < 11; ++j) {
      cp_counts[j] = counts[j];
      counts[j] = 0;
    }
    curr_min = std::min(cp_curr_min, tree[index].min_value + tree[index].delta);
    for (int j = 0; j < 11; ++j) {
      int cv = cp_curr_min + j - curr_min;
      if (cv <  11) counts[cv] += cp_counts[j];
    }
    for (int j = 0; j < 11; ++j) {
      int cv = tree[index].min_value + tree[index].delta + j - curr_min;
      if (cv <  11) counts[cv] += tree[index].counts[j];
    }
    return;
  }
  tree[index + index].delta += tree[index].delta;
  tree[index + index + 1].delta += tree[index].delta;
  tree[index].delta = 0;

  int nodeMiddle = (nodeL + nodeR) / 2;
  if (left <= nodeMiddle) {
    rangeQuery(index + index, nodeL, nodeMiddle, left, std::min(right, nodeMiddle));
  }
  if (right > nodeMiddle) {
    rangeQuery(index + index + 1, nodeMiddle + 1, nodeR, std::max(left, nodeMiddle + 1), right);
  }
  // TODO: merge back
  for (int j = 0; j < 11; ++j) tree[index].counts[j] = 0;
  tree[index].min_value = std::min(tree[index + index].min_value + tree[index + index].delta, tree[index + index + 1].min_value + tree[index + index + 1].delta);
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index].min_value + tree[index + index].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index].counts[j];
  }
  for (int j = 0; j < 11; ++j) {
    int cv = tree[index + index + 1].min_value + tree[index + index + 1].delta + j - tree[index].min_value;
    if (cv < 11) tree[index].counts[cv] += tree[index + index + 1].counts[j];
  }
}

int V0 = 0;

void rangeQuery(int left, int right) {
  curr_min = inf;
  for (int j = 0; j < 11; ++j) counts[j] = 0;
  rangeQuery(1, 1, 2 * n, left, right);

//  V0 = 0;
//  if (right > 12000) {
//    V0 = verify(1, right);
 // }
  long long delta = answer[1];
  for (int j = 0; j < 11; ++j) {
    if (curr_min + j < 11) {
//      printf("%d | %d %d\n", answer[curr_min + j], curr_min + j, counts[j]);
      answer[curr_min + j] += counts[j];
    }
  }
  //printf("U: %d %d\n", answer[1] - delta, V0);
  //if (/*right > 12000 &&*/ answer[1] - delta != V0) {
  //  exit(0);
 // }
}


int ufUp[2 * max_n];
int ufDown[2 * max_n];

int findUp(int p) {
  if (ufUp[p] == p) return p;
  else return ufUp[p] = findUp(ufUp[p]);
}

void uniteUp(int p, int q) {
  p = findUp(p);
  q = findUp(q);
  if (p != q) ufUp[q] = p;
}

int findDown(int p) {
  if (ufDown[p] == p) return p;
  else return ufDown[p] = findDown(ufDown[p]);
}

void uniteDown(int p, int q) {
  p = findDown(p);
  q = findDown(q);
  if (p != q) ufDown[q] = p;
}

int events[12];

char brut[5][2];
char cpBrut[5][2];

void localBrutDFS(int x, int y) {
  cpBrut[x][y] = '.';
  if (cpBrut[x][1 - y] == 'X') localBrutDFS(x, 1 - y);
  int nx = x + 1 == 5 ? 0 : x + 1;
  int px = x == 0 ? 4 : x - 1;
  if (cpBrut[px][y] == 'X') localBrutDFS(px, y);
  if (cpBrut[nx][y] == 'X') localBrutDFS(nx, y);
}

int localBrut() {
  for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 2; ++j) {
      cpBrut[i][j] = brut[i][j];
    }
  }
  int ret = 0;
  for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 2; ++j) {
      if (cpBrut[i][j] == 'X') {
        ++ret;
        localBrutDFS(i, j);
      }
    }
  }
  return ret;
}

bool connectedUp;
bool connectedDown;
int lowerBoundUp;
int lowerBoundDown;

void update(int R) {
  int X = findValue[R][0];  // 0..n-1
  int Y = findValue[R][1];  // 0..1

  ufUp[R] = R;
  ufDown[R] = R;
  bool bcUp = connectedUp;
  bool bcDown = connectedDown;
  int bcBUp = lowerBoundUp;
  int bcBDown = lowerBoundDown;
  if (!connectedUp) {
    if (X > 0 && in[X - 1][Y] <= R) uniteUp(R, in[X - 1][Y]);
    if (X + 1 < n && in[X + 1][Y] <= R) uniteUp(R, in[X + 1][Y]);
    if (in[X][1 - Y] <= R) uniteUp(R, in[X][1 - Y]);

    if (in[0][0] <= R && in[n - 1][0] <= R && findUp(in[0][0]) == findUp(in[n - 1][0])) {
      connectedUp = true;
    }
  }
  if (!connectedDown) {
    if (X > 0 && in[X - 1][Y] <= R) uniteDown(R, in[X - 1][Y]);
    if (X + 1 < n && in[X + 1][Y] <= R) uniteDown(R, in[X + 1][Y]);
    if (in[X][1 - Y] <= R) uniteDown(R, in[X][1 - Y]);

    if (in[0][1] <= R && in[n - 1][1] <= R && findDown(in[0][1]) == findDown(in[n - 1][1])) {
      connectedDown = true;
    }
  }
  if (connectedUp) {
    while (lowerBoundUp <= R) {
      int RX = findValue[lowerBoundUp][0];
      int RY = findValue[lowerBoundUp][1];
      if (RX > 0 && RX < n - 1) {
        bool lExists = lowerBoundUp < in[RX - 1][1 - RY] && in[RX - 1][1 - RY] <= R;
        bool uExists = lowerBoundUp < in[RX][1 - RY] && in[RX][1 - RY] <= R;
        bool rExists = lowerBoundUp < in[RX + 1][1 - RY] && in[RX + 1][1 - RY] <= R;
        if (lExists && uExists && rExists) {
          ++lowerBoundUp;
          continue;
        }
      } else if (RX == 0 && RY == 1) {
        bool uExists = lowerBoundUp < in[RX][1 - RY] && in[RX][1 - RY] <= R;
        bool rExists = lowerBoundUp < in[RX + 1][1 - RY] && in[RX + 1][1 - RY] <= R;
        if (uExists && rExists) {
          ++lowerBoundUp;
          continue;
        }       
      } else if (RX == n - 1 && RY == 1) {
        bool lExists = lowerBoundUp < in[RX - 1][1 - RY] && in[RX - 1][1 - RY] <= R;
        bool uExists = lowerBoundUp < in[RX][1 - RY] && in[RX][1 - RY] <= R;
//        printf("WW=%d %d\n", lExists, uExists);
        if (lExists && uExists) {
          ++lowerBoundUp;
          continue;
        }
      }
      break;
    }
  }
  if (connectedDown) {
    while (lowerBoundDown <= R) {
      int RX = findValue[lowerBoundDown][0];
      int RY = findValue[lowerBoundDown][1];
      if (RX > 0 && RX < n - 1) {
        bool lExists = lowerBoundDown < in[RX - 1][1 - RY] && in[RX - 1][1 - RY] <= R;
        bool uExists = lowerBoundDown < in[RX][1 - RY] && in[RX][1 - RY] <= R;
        bool rExists = lowerBoundDown < in[RX + 1][1 - RY] && in[RX + 1][1 - RY] <= R;
        if (lExists && uExists && rExists) {
          ++lowerBoundDown;
          continue;
        }
      } else if (RX == 0 && RY == 0) {
        bool uExists = lowerBoundDown < in[RX][1 - RY] && in[RX][1 - RY] <= R;
        bool rExists = lowerBoundDown < in[RX + 1][1 - RY] && in[RX + 1][1 - RY] <= R;
        if (uExists && rExists) {
          ++lowerBoundDown;
          continue;
        }       
      } else if (RX == n - 1 && RY == 0) {
        bool lExists = lowerBoundDown < in[RX - 1][1 - RY] && in[RX - 1][1 - RY] <= R;
        bool uExists = lowerBoundDown < in[RX][1 - RY] && in[RX][1 - RY] <= R;
//        printf("WW=%d %d\n", lExists, uExists);
        if (lExists && uExists) {
          ++lowerBoundDown;
          continue;
        }
      }
      break;
    }
  }

//  printf("%d %d\n", connectedUp, lowerBoundUp);
//  printf("%d %d\n", connectedDown, lowerBoundDown);
  int A = in[X == 0 ? n - 1 : X - 1][Y];
  int E = in[X == 0 ? n - 1 : X - 1][1 - Y];
  int B = in[X == n - 1 ? 0 : X + 1][Y];
  int F = in[X == n - 1 ? 0 : X + 1][1 - Y];
  int C = in[X][1 - Y];
  int G = connectedUp == false ? inf : lowerBoundUp;
  if (connectedDown && (G == inf || G < lowerBoundDown)) G = lowerBoundDown; 
//  if (R == 12012) G = 44;

//  printf("A=%d\n", A);
//  printf("B=%d\n", B);
//  printf("C=%d\n", C);
//  printf("E=%d\n", E);
 // printf("F=%d\n", F);
 // printf("G=%d\n", G);

  events[0] = 0; // sentinel
  events[1] = R;
  events[2] = A;
  events[3] = B;
  events[4] = C;
  events[5] = E;
  events[6] = F;
  events[7] = G;
//  events[8] = H;
  std::sort(events, events + 8);
  int posR = 0;
  while (events[posR] != R) ++posR;

//  printf("PP\n");
  resetElem(1, 1, 2 * n, R);
//    printf("MIN: %d\n", tree[1].min_value);
//    printf("CNT: %d\n", tree[1].counts[0]);
//  printf("QQ\n");

  for (int i = 1; i <= posR; ++i) {
    // Assess situation for range (events[i - 1], events[i]]
    int rangeL = events[i - 1] + 1;
    int rangeR = events[i];
    if (rangeL > rangeR) continue;
    for (int j = 0; j < 5; ++j) brut[j][0] = brut[j][1] = '.';
    // It is enough to concentrate on rangeR. Rest will follow.
    brut[2][0] = '.'; // R doesn't exists
    brut[1][0] = rangeR <= A && A <= R ? 'X' : '.'; // A exists
    brut[3][0] = rangeR <= B && B <= R ? 'X' : '.'; // B exists
    brut[2][1] = rangeR <= C && C <= R ? 'X' : '.'; // C exists
    brut[1][1] = rangeR <= E && E <= R ? 'X' : '.'; // E exists
    brut[3][1] = rangeR <= F && F <= R ? 'X' : '.'; // F exists
    brut[0][0] = rangeR <= G && G <= R ? 'X' : '.'; // G exists
    brut[4][0] = rangeR <= G && G <= R ? 'X' : '.'; // G exists
    brut[0][1] = rangeR <= G && G <= R ? 'X' : '.'; // G exists
    brut[4][1] = rangeR <= G && G <= R ? 'X' : '.'; // G exists
  //  printf("B: %c%c%c%c%c\n", brut[0][0], brut[1][0], brut[2][0], brut[3][0], brut[4][0]);
  //  printf("B: %c%c%c%c%c\n", brut[0][1], brut[1][1], brut[2][1], brut[3][1], brut[4][1]);
    int deltaBefore = localBrut();
    brut[2][0] = 'X'; // R doesn't exists
    int deltaAfter= localBrut();
//    printf("A: %c%c%c%c%c\n", brut[0][0], brut[1][0], brut[2][0], brut[3][0], brut[4][0]);
//    printf("A: %c%c%c%c%c\n", brut[0][1], brut[1][1], brut[2][1], brut[3][1], brut[4][1]);
//    printf("[%d, %d] | %d %d\n", rangeL, rangeR, deltaBefore, deltaAfter);
    int delta = deltaAfter - deltaBefore;
    rangeAdd(1, 1, 2 * n, rangeL, rangeR, delta);
  }
}

void hack(int x, int y) {
  brut[x][y] = '.';
  if (brut[x][1 - y] == 'X') {
    hack(x, 1 - y);
  }
  if (brut[1 - x][y] == 'X') {
    hack(1 - x, y);
  }
}

int main() {
  for (int i = 1; i <= 2 * czapa; ++i) {
    tree[i].delta = 0;
    tree[i].min_value = inf;
    for (int j = 0; j < 11; ++j) tree[i].counts[j] = 0;
  }
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &in[i][0]);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &in[i][1]);
  }
  for (int i = 0; i < n; ++i) {
    findValue[in[i][0]][0] = i;
    findValue[in[i][0]][1] = 0;
    findValue[in[i][1]][0] = i;
    findValue[in[i][1]][1] = 1;
    ufUp[i + 1] = inf;
    ufUp[2 * n + i + 1] = inf;
    ufDown[i + 1] = inf;
    ufDown[2 * n + i + 1] = inf;
  }
  for (int i = 1; i <= k; ++i) {
    answer[i] = 0;
  }
  if (n < 3) {
    for (int i = 1; i <= 4; ++i) {
      for (int j = i; j <= 4; ++j) {
        brut[0][0] = i <= in[0][0] && in[0][0] <= j ? 'X' : '.';
        brut[0][1] = i <= in[0][1] && in[0][1] <= j ? 'X' : '.';
        brut[1][0] = i <= in[1][0] && in[1][0] <= j ? 'X' : '.';
        brut[1][1] = i <= in[1][1] && in[1][1] <= j ? 'X' : '.';
        int cnt = 0;
        for (int A = 0; A < 2; ++A) {
          for (int B = 0; B < 2; ++B) {
            if (brut[A][B] == 'X') {
              ++cnt;
              hack(A, B);
            }
          }
        }
//        printf("%d %d %d\n", i, j, cnt);
        answer[cnt]++;
      }
    }
    for (int i = 1; i <= k; ++i) {
      printf("%lld ", answer[i]);
    }
    printf("\n");
    return 0;
  }

  connectedUp = false;
  connectedDown = false;
  lowerBoundUp = 1;
  lowerBoundDown = 1;
  for (int i = 1; i <= 2 * n; ++i) {
//    printf("UPDATE %d\n", i);
    update(i);
//    printf("MIN: %d\n", tree[1].min_value);
//    printf("CNT: %d\n", tree[1].counts[0]);
    rangeQuery(1, i);
//    for (int i = 1; i <= k; ++i) {
//      printf("[%d]=%lld\n", i, answer[i]);
//    }
  }

  for (int i = 1; i <= k; ++i) {
    printf("%lld ", answer[i]);
  }
  printf("\n");
  return 0;
}