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

using namespace std;

constexpr long INF = 1e9 + 7;

int width;
int maxNumberOfAreas;
int *canvas[2];
pair<int, int> *positions;

struct Node {
    int minNumberOfAreas;
    int areas[11];
    int delta;
    pair<int, int> range;
};

int firstLeafIndex;
Node *tree;
int globalResult[11];

void read() {
    cin >> width >> maxNumberOfAreas;

    canvas[0] = new int[width];
    canvas[1] = new int[width];
    positions = new pair<int, int>[2 * width];

    for (int i = 0; i < width; ++i) {
        cin >> canvas[0][i];
        positions[canvas[0][i]] = pair(0, i);
    }
    for (int i = 0; i < width; ++i) {
        cin >> canvas[1][i];
        positions[canvas[1][i]] = pair(1, i);
    }
}

void initTree() {
    firstLeafIndex = 1;
    while (firstLeafIndex < 2 * width) {
        firstLeafIndex *= 2;
    }
    tree = new Node[2 * firstLeafIndex];
}

Node createLeafNode(int areasNumber, int startEnd) {
    Node node{};
    node.minNumberOfAreas = areasNumber;
    node.areas[0] = 1;
    node.range = pair(startEnd, startEnd);
    return node;
}

Node createEmptyNode(int startEnd) {
    Node node{};
    node.minNumberOfAreas = INF;
    node.range = pair(startEnd, startEnd);
    return node;
}

int getLeft(const pair<int, int> &point) {
    return canvas[point.first][(width + point.second - 1) % width];
}

int getRight(const pair<int, int> &point) {
    return canvas[point.first][(point.second + 1) % width];
}

int getLeftDiagonally(const pair<int, int> &point) {
    return canvas[(point.first + 1) % 2][(width + point.second - 1) % width];
}

int getRightDiagonally(const pair<int, int> &point) {
    return canvas[(point.first + 1) % 2][(point.second + 1) % width];
}

int getHorizontal(const pair<int, int> &point) {
    return canvas[(point.first + 1) % 2][point.second];
}

bool oneTrue(bool value1, bool value2, bool value3) {
    int result = 0;
    if (value1) result++;
    if (value2) result++;
    if (value3) result++;
    return result == 1;
}

int getAreasChange(int start, int end, int color) {
    const pair<int, int> &point = positions[color];
    int horizontal = getHorizontal(point);
    int left = getLeft(point);
    int right = getRight(point);
    int leftDiagonally = getLeftDiagonally(point);
    int rightDiagonally = getRightDiagonally(point);
    bool isHorizontal = start <= horizontal && horizontal <= end;
    bool isLeft = start <= left && left <= end;
    bool isRight = start <= right && right <= end;
    bool isLeftDiagonally = start <= leftDiagonally && leftDiagonally <= end;
    bool isRightDiagonally = start <= rightDiagonally && rightDiagonally <= end;

    if (!isHorizontal && !isLeft && !isRight) {
        return 1;
    }
    if (isHorizontal && isLeft && isRight && !isLeftDiagonally && !isRightDiagonally) {
        return -2;
    }
    if (oneTrue(isLeft, isRight, isHorizontal)) {
        return 0;
    }
    if (isHorizontal && (!isLeft || isLeftDiagonally) && (!isRight || isRightDiagonally)) {
        return 0;
    }
    return -1;
}

void computeLeaves() {
    int areasNumber = 1;
    tree[firstLeafIndex] = createLeafNode(areasNumber, 1);

    for (int end = 2; end <= 2 * width; ++end) {
        areasNumber += getAreasChange(1, end - 1, end);
        tree[firstLeafIndex + end - 1] = createLeafNode(areasNumber, end);
    }
    for (int i = firstLeafIndex + 2 * width; i < 2 * firstLeafIndex; ++i) {
        tree[i] = createEmptyNode(i + 1 - firstLeafIndex);
    }
}

void updateNodeByChildren(const Node &leftChild, const Node &rightChild, Node &node) {
    node.minNumberOfAreas = min(leftChild.minNumberOfAreas, rightChild.minNumberOfAreas);
    for (int j = 0; j < 11; ++j) {
        int result = 0, diff;
        diff = leftChild.minNumberOfAreas - node.minNumberOfAreas;
        if (j - diff >= 0) {
            result += leftChild.areas[j - diff];
        }
        diff = rightChild.minNumberOfAreas - node.minNumberOfAreas;
        if (j - diff >= 0) {
            result += rightChild.areas[j - diff];
        }
        node.areas[j] = result;
        node.range.first = leftChild.range.first;
        node.range.second = rightChild.range.second;
    }
    node.minNumberOfAreas += node.delta;
}

void computeParents() {
    for (int i = firstLeafIndex - 1; i > 0; --i) {
        Node &leftChild = tree[2 * i];
        Node &rightChild = tree[2 * i + 1];
        Node node{};
        updateNodeByChildren(leftChild, rightChild, node);
        tree[i] = node;
    }
}

void showTree() {
    cout << "=====================" << endl;
    for (int i = 1; i < firstLeafIndex * 2; ++i) {
        string min = tree[i].minNumberOfAreas < INF ? to_string(tree[i].minNumberOfAreas) : "inf";
        cout << "{" << min << ",|";
        for (int area: tree[i].areas) {
            cout << area << "|";
        }
        if (tree[i].delta != 0) {
            cout << "," << tree[i].delta;
        }
        cout << "}, ";
        if (((i + 1) & i) == 0) {
            cout << endl;
        }
    }
}

void mergePath(int index) {
    while (index > 1) {
        index = index / 2;
        updateNodeByChildren(tree[2 * index], tree[2 * index + 1], tree[index]);
    }
}

void removeNode(int start) {
    tree[firstLeafIndex + start - 1] = createEmptyNode(start);
    mergePath(firstLeafIndex + start - 1);
}

vector<pair<int, int>> computeRanges(int color) {
    const pair<int, int> &point = positions[color];
    int horizontal = getHorizontal(point);
    int left = getLeft(point);
    int right = getRight(point);
    int leftDiagonally = getLeftDiagonally(point);
    int rightDiagonally = getRightDiagonally(point);
    vector<int> neighbors;
    if (horizontal > color) {
        neighbors.push_back(horizontal);
    }
    if (left > color) {
        neighbors.push_back(left);
    }
    if (right > color) {
        neighbors.push_back(right);
    }
    if (leftDiagonally > color) {
        neighbors.push_back(leftDiagonally);
    }
    if (rightDiagonally > color) {
        neighbors.push_back(rightDiagonally);
    }
    sort(neighbors.begin(), neighbors.end());
    vector<pair<int, int>> ranges;
    int previous = color+1;
    for (int neighbor: neighbors) {
        if (previous <= neighbor-1) {
            ranges.emplace_back(previous, neighbor - 1);
        }
        previous = neighbor;
    }
    ranges.emplace_back(previous, previous);
    if (previous < 2 * width) {
        ranges.emplace_back(previous+1, 2 * width);
    }
    return ranges;
}

void updateNodeByRange(int index, const pair<int, int> &range, int delta) {
    Node &node = tree[index];
    if (node.range == range) {
        if (node.minNumberOfAreas < INF) {
            node.delta += delta;
            node.minNumberOfAreas += delta;
        }
        return;
    }
    if (index < firstLeafIndex) {
        bool leftUpdated = false, rightUpdated = false;
        Node &leftChild = tree[2 * index];
        if (range.first <= leftChild.range.second) {
            updateNodeByRange(2 * index, pair(range.first, min(range.second, leftChild.range.second)),
                              node.delta + delta);
            leftUpdated = true;
        }
        Node &rightChild = tree[2 * index + 1];
        if (range.second >= rightChild.range.first) {
            updateNodeByRange(2 * index + 1, pair(max(range.first, rightChild.range.first), range.second),
                              node.delta + delta);
            rightUpdated = true;
        }
        if (leftUpdated || rightUpdated) {
            if (node.delta != 0) {
                if (!leftUpdated) {
                    updateNodeByRange(2 * index, leftChild.range, node.delta);
                }
                if (!rightUpdated) {
                    updateNodeByRange(2 * index + 1, rightChild.range, node.delta);
                }
                node.delta = 0;
            }
            updateNodeByChildren(leftChild, rightChild, node);
        }
    }
}

void updateTree(const pair<int, int> &range, int delta) {
    updateNodeByRange(1, range, delta);
}

void removeStart(int color) {
    removeNode(color);
//    showTree();
    for (pair<int, int> range: computeRanges(color)) {
        int delta = -getAreasChange(color, range.first, color);
//        cout << "[" << range.first << "," << range.second << "]: delta: " << delta << endl;
        if (delta != 0) {
            updateTree(range, delta);
//            showTree();
        }
    }
}

void updateGlobalResult() {
    Node &root = tree[1];
    for (int j = root.minNumberOfAreas; j < 11; ++j) {
        globalResult[j] += root.areas[j - root.minNumberOfAreas];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    read();
    initTree();

    computeLeaves();
    computeParents();
    updateGlobalResult();

//    showTree();
    for (int i = 1; i < 2*width; ++i) {
//        cout << "REMOVE START: " << i << endl;
        removeStart(i);
        updateGlobalResult();
//        showTree();
    }

    for (int i = 1; i < maxNumberOfAreas + 1; ++i) {
        int result = globalResult[i];
        if (i == 1) {
            result += globalResult[0];
        }
        cout << result << " ";
    }
    cout << endl;

    return 0;
}