#include <bits/stdc++.h>
const int MAXN = 100005;
using namespace std;
using ll = long long;
using Pos = pair<int, int>;
int n, k;
int t[2][MAXN];
Pos pos[2 * MAXN];
ll res[15];
bool in_range(int x, int l, int r) { return l <= x && x <= r; }
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
Pos rep[2][MAXN];
Pos Find(Pos pos) {
if (rep[pos.first][pos.second] == pos) return pos;
return rep[pos.first][pos.second] = Find(rep[pos.first][pos.second]);
}
int Union(Pos pos1, Pos pos2) {
auto ra = Find(pos1);
auto rb = Find(pos2);
if (ra != rb) {
rep[ra.first][ra.second] = rb;
return 1;
}
return 0;
}
bool are_neighbors(int l, int r) {
int dist_x = pos[l].first != pos[r].first;
int dist_y = abs(pos[l].second - pos[r].second);
dist_y = min(dist_y, n - dist_y);
return (dist_x + dist_y) == 1;
}
ll solve_k1(int l, int r) {
if (l == r) return 1;
if (l + 1 == r) {
return 2 + are_neighbors(l, r);
}
int m = (l + r) / 2;
ll res = solve_k1(l, m - 1) + solve_k1(m + 1, r);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int row = 0; row < 2; row++) {
for (int column = 0; column < n; column++) {
cin >> t[row][column];
pos[t[row][column]] = Pos(row, column);
}
}
int COLORS = 2 * n;
// if (k == 1) {
// cout << solve_k1(1, COLORS);
// }
for (int left_color = 1; left_color <= COLORS; left_color++) {
for (int row = 0; row < 2; row++) {
for (int column = 0; column < n; column++) {
rep[row][column] = Pos(row, column);
}
}
int components = 0;
for (int right_color = left_color; right_color <= COLORS; right_color++) {
components++;
int row = pos[right_color].first, column = pos[right_color].second;
for (int i = 0; i < 4; i++) {
int nx = row + dx[i];
int ny = column + dy[i];
if (!in_range(nx, 0, 1)) continue;
if (ny < 0) ny = n - 1;
if (ny >= n) ny = 0;
if (in_range(t[nx][ny], left_color, right_color))
components -= Union({nx, ny}, {row, column});
}
if (components <= k) res[components]++;
}
}
for (int i = 1; i <= k; i++) cout << res[i] << " ";
cout << "\n";
}
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 | #include <bits/stdc++.h> const int MAXN = 100005; using namespace std; using ll = long long; using Pos = pair<int, int>; int n, k; int t[2][MAXN]; Pos pos[2 * MAXN]; ll res[15]; bool in_range(int x, int l, int r) { return l <= x && x <= r; } int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; Pos rep[2][MAXN]; Pos Find(Pos pos) { if (rep[pos.first][pos.second] == pos) return pos; return rep[pos.first][pos.second] = Find(rep[pos.first][pos.second]); } int Union(Pos pos1, Pos pos2) { auto ra = Find(pos1); auto rb = Find(pos2); if (ra != rb) { rep[ra.first][ra.second] = rb; return 1; } return 0; } bool are_neighbors(int l, int r) { int dist_x = pos[l].first != pos[r].first; int dist_y = abs(pos[l].second - pos[r].second); dist_y = min(dist_y, n - dist_y); return (dist_x + dist_y) == 1; } ll solve_k1(int l, int r) { if (l == r) return 1; if (l + 1 == r) { return 2 + are_neighbors(l, r); } int m = (l + r) / 2; ll res = solve_k1(l, m - 1) + solve_k1(m + 1, r); } int32_t main() { ios_base::sync_with_stdio(0); cin >> n >> k; for (int row = 0; row < 2; row++) { for (int column = 0; column < n; column++) { cin >> t[row][column]; pos[t[row][column]] = Pos(row, column); } } int COLORS = 2 * n; // if (k == 1) { // cout << solve_k1(1, COLORS); // } for (int left_color = 1; left_color <= COLORS; left_color++) { for (int row = 0; row < 2; row++) { for (int column = 0; column < n; column++) { rep[row][column] = Pos(row, column); } } int components = 0; for (int right_color = left_color; right_color <= COLORS; right_color++) { components++; int row = pos[right_color].first, column = pos[right_color].second; for (int i = 0; i < 4; i++) { int nx = row + dx[i]; int ny = column + dy[i]; if (!in_range(nx, 0, 1)) continue; if (ny < 0) ny = n - 1; if (ny >= n) ny = 0; if (in_range(t[nx][ny], left_color, right_color)) components -= Union({nx, ny}, {row, column}); } if (components <= k) res[components]++; } } for (int i = 1; i <= k; i++) cout << res[i] << " "; cout << "\n"; } |
English