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
#include <bits/stdc++.h>


using namespace std;


struct rect {
    int lx, rx, ly, ry;

    bool isValid() const { return lx != -1; }
};

const rect RECT_INVALID = {-1, -1, -1, -1};

struct event {
    int l, r, val;
};


const int MAXL = 800'008;

typedef pair<int, vector <int>> val_t;

val_t vals[MAXL];
int mods[MAXL];


struct LazyTree {
    int p = 1, k;

    void recompute(int i) {
        int iL = 2 * i + 1, iR = 2 * i + 2;
        vals[i].first = min(vals[iL].first, vals[iR].first);

        int diffL = vals[i].first - vals[iL].first;
        int diffR = vals[i].first - vals[iR].first;

        for (int x = 0; x < k; x++) {
            int v = 0;

            int xL = x + diffL;
            if (xL >= 0) {
                v += vals[iL].second[xL];
            }

            int xR = x + diffR;
            if (xR >= 0) {
                v += vals[iR].second[xR];
            }

            vals[i].second[x] = v;
        }
    }

    val_t getValInit() {
        vector <int> cntsInit(k, 0);
        cntsInit[0] = 1;

        return {0, cntsInit};
    }

    LazyTree(int n, int k): k(k) {
        while (p < n) p *= 2;

        int m = 2 * p - 1;
        for (int i = 0; i < m; i++) {
            vals[i] = getValInit();
            mods[i] = 0;
        }

        for (int i = p - 2; i >= 0; i--) {
            recompute(i);
        }
    }

    void modify(int x, int y, int m) { _modify(0, 0, p - 1, x, y, m); }

    void _push(int u) {
        vals[u].first += mods[u];

        if (u < p - 1) {
            mods[2 * u + 1] += mods[u];
            mods[2 * u + 2] += mods[u];
        }

        mods[u] = 0;
    }

    void _modify(int u, int a, int b, int x, int y, int m) {
        _push(u);

        if (x <= a && b <= y) {
            mods[u] += m;
            _push(u);
            return ;
        }

        int mid = (a + b) / 2;

        if (x <= mid) _modify(2 * u + 1, a, mid, x, y, m);
        else _push(2 * u + 1);

        if (mid < y) _modify(2 * u + 2, mid + 1, b, x, y, m);
        else _push(2 * u + 2);

        recompute(u);
    }
};

rect makeRect(int m, const vector <int> &include, const vector <int> &exclude) {
    int rx = *min_element(include.begin(), include.end());
    int ly = *max_element(include.begin(), include.end());

    int lx = 0, ry = m - 1;
    for (int e : exclude) {
        if (e >= rx && e <= ly) {
            return RECT_INVALID;
        } else if (e < rx) {
            lx = max(lx, e + 1);
        } else {
            ry = min(ry, e - 1);
        }
    }

    return {lx, rx, ly, ry};
}

vector <long long> solve(int n, int k, vector <vector<int>> &tab) {
    int m = 2 * n;

    vector <rect> rects;
    for (int i = 0; i < n; i++) {
        int j = i < n - 1 ? i + 1 : 0;
        int ap = tab[0][i], bp = tab[1][i], a = tab[0][j], b = tab[1][j];

        auto toAdd = {
            makeRect(m, {a}, {b, ap}),
            makeRect(m, {b}, {a, bp}),
            makeRect(m, {a, b}, {ap, bp})
        };

        for (auto &r : toAdd) if (r.isValid()) {
            rects.push_back(r);
        }
    }

    vector <vector<event>> events(m);
    for (auto &[lx, rx, ly, ry] : rects) {
        events[lx].push_back({ly, ry, 1});
        if (rx < m - 1) {
            events[rx + 1].push_back({ly, ry, -1});
        }
    }

    LazyTree tree(m, k + 1);
    vector <long long> ans(k, 0);

    for (int x = 0; x < m; x++) {
        for (auto &[l, r, val] : events[x]) {
            tree.modify(l, r, val);
        }

        auto cnts = vals[0].second;

        ans[0] += cnts[0] - x - (tree.p - m);
        for (int v = 0; v < k; v++) {
            ans[v] += cnts[v + 1];
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector <vector<int>> tab(2, vector <int> (n));
    for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) {
        cin >> tab[i][j];
        tab[i][j]--;
    }

    auto ans = solve(n, k, tab);
    for (long long x : ans) {
        cout << x << ' ';
    }

    return 0;
}