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
#include <bits/stdc++.h>
#define ll long long
#define debug if(0)

const int MAX_N = 1e5;
const int base = 1<<18;
const int inf = 1e9;

int a[2][MAX_N+3];
std::pair<int, int> pos[2*MAX_N+3];
int n, k;

struct Node {
    std::vector<std::pair<int, int> > v;
    int lazy;

    Node(){ v.clear(); lazy = 0; }
};

Node t[2*base+5];

void calc_vals(int low, int high, int v) {
    int i, j;
    i = j = 0;
    int lsize = t[v*2].v.size();
    int rsize = t[v*2+1].v.size();
    t[v].v.clear();
    while (t[v].v.size() < k+1 && (i < lsize || j < rsize)) {
        if (i < lsize && j < rsize && t[v*2].v[i].first == t[v*2+1].v[j].first) {
            t[v].v.push_back({t[v*2].v[i].first, t[v*2].v[i].second + t[v*2+1].v[j].second});
            i++; j++;
        } else if (i >= lsize || (j < rsize && t[v*2+1].v[j].first < t[v*2].v[i].first)) 
            t[v].v.push_back(t[v*2+1].v[j++]);
        else if (i < lsize) 
            t[v].v.push_back(t[v*2].v[i++]);
        else 
            break;
    }

    debug {
        std::cout << "vals on [" << low << ", " << high << "]: ";
        for (auto p : t[v].v) std::cout << "{" << p.first << ", " << p.second << "} ";
        std::cout << "\n";
    }
}

void push_down(int low, int high, int v) {
    for (auto &p : t[v].v) p.first += t[v].lazy;

    debug {
        std::cout << "vals on [" << low << ", " << high << "]: ";
        for (auto p : t[v].v) std::cout << "{" << p.first << ", " << p.second << "} ";
        std::cout << "\n";
    }

    if (low != high) {
        t[v*2].lazy += t[v].lazy;
        t[v*2+1].lazy += t[v].lazy;
    }
    t[v].lazy = 0;
}

void add(int low, int high, int L, int R, int v, int x) {
    if (low > high || L > R) return;

    if (t[v].lazy != 0) push_down(low, high, v);

    if (L > high || R < low) return;
    
    if (high <= R && low >= L) {
        t[v].lazy += x;
        push_down(low, high, v);
        return;
    }
    int mid = (low+high)/2;
    add(low, mid, L, R, v*2, x);
    add(mid+1, high, L, R, v*2+1, x);
    calc_vals(low, high, v);
}

void init(int low, int high, int v) {
    if (low > high) return;
    t[v].v.push_back({0, high-low+1});
    if (low == high) return;
    int mid = (low+high)/2;
    init(low, mid, v*2);
    init(mid+1, high, v*2+1);
}

ll get_cnt(int x) {
    int res = 0;
    for (auto p : t[1].v) if (p.first == x) res = p.second;
    debug std::cout << "get_cnt(" << x << ") = " << res << "\n";
    return res;
}

void calc_and_add(int i, int left, int X) {
    // calculate good range for i-th gap if left is fixed and add X on this range
    int x = inf;
    if (a[0][i] >= left) x = a[0][i];
    if (a[1][i] >= left) x = std::min(x, a[1][i]);

    int y = 2*n+1;
    if (a[0][i] >= left && a[0][(i+n-1)%n] >= left)
        y = std::min(y, std::max(a[0][i], a[0][(i+n-1)%n]));
    if (a[1][i] >= left && a[1][(i+n-1)%n] >= left) {
        y = std::min(y, std::max(a[1][i], a[1][(i+n-1)%n]));
    }

    debug std::cout << "calc_and_add(" << i << ", " << left << ", " << X << "): [" << x << ", " << y-1 << "]\n";

    if (left <= x && x < y) {
        add(1, 2*n, x, y-1, 1, X);
    }
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);

    std::cin >> n >> k;
    init(1, 2*n, 1);

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < n; j++) { std::cin >> a[i][j]; pos[a[i][j]] = {i, j}; }

    std::vector<ll> res(11, 0);

    int l = 1;

    for (int i = 0; i < n; i++) {
        fflush(stdout);
        calc_and_add(i, 1, 1);
    }

    while (l <= 2*n) {
        res[1] += get_cnt(0) + get_cnt(1);
        for (int i = 2; i <= k; i++) res[i] += get_cnt(i);
        int pos_y = pos[l].second;
        calc_and_add(pos_y, l, -1);
        calc_and_add((pos_y+1)%n, l, -1);
        calc_and_add(pos_y, l+1, 1);
        calc_and_add((pos_y+1)%n, l+1, 1);
        l++;
        add(1, 2*n, l-1, l-1, 1, inf);
    }

    for (int i = 1; i <= k; i++) std::cout << res[i] << " ";
    std::cout << "\n";
}