#include <bits/stdc++.h> using namespace std; typedef long long lld; int sft[] { 1, -1, 0, 0}; int n, k; int parts; vector<int> parent, rnk; vector<lld> cnt; int find(int x) { if (x == parent[x]) return x; int res = find(parent[x]); parent[x] = res; return res; } int join(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (rnk[x] < rnk[y]) swap(x, y); if (rnk[x] == rnk[y]) ++rnk[x]; parent[y] = x; return 1; } void add_vertex(pair<int, int> &p) { parent[p.first*n + p.second] = p.first*n + p.second; rnk[p.first*n + p.second] = 0; ++parts; for (int i = 0; i < 4; ++i) { int x = p.first + sft[i], y = p.second + sft[3-i]; if (x < 0 || x >= 2) continue; if (y >= n) y -= n; if (y < 0) y += n; if (parent[x * n + y] >= 0) parts -= join(x*n + y, p.first*n + p.second); } } void calc(vector<pair<int, int>> &pos, int start) { parent.assign(2*n, -1); rnk.assign(2*n, 0); parts = 0; for (int i = start; i < 2*n; ++i) { add_vertex(pos[i]); ++cnt[parts]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; vector<int> v[2] {vector<int>(n), vector<int> (n)}; for (int i = 0; i < 2; ++i) for (int j = 0; j < n; ++j) cin >> v[i][j]; vector<pair<int, int>> pos(2*n); for (int i = 0; i < 2; ++i) for (int j = 0; j < n; ++j) pos[v[i][j] - 1] = {i, j}; cnt.assign(2*n, 0); for (int i = 0; i < 2*n; ++i) calc(pos, i); for (int i = 1; i <= k; ++i) cout << cnt[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 | #include <bits/stdc++.h> using namespace std; typedef long long lld; int sft[] { 1, -1, 0, 0}; int n, k; int parts; vector<int> parent, rnk; vector<lld> cnt; int find(int x) { if (x == parent[x]) return x; int res = find(parent[x]); parent[x] = res; return res; } int join(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (rnk[x] < rnk[y]) swap(x, y); if (rnk[x] == rnk[y]) ++rnk[x]; parent[y] = x; return 1; } void add_vertex(pair<int, int> &p) { parent[p.first*n + p.second] = p.first*n + p.second; rnk[p.first*n + p.second] = 0; ++parts; for (int i = 0; i < 4; ++i) { int x = p.first + sft[i], y = p.second + sft[3-i]; if (x < 0 || x >= 2) continue; if (y >= n) y -= n; if (y < 0) y += n; if (parent[x * n + y] >= 0) parts -= join(x*n + y, p.first*n + p.second); } } void calc(vector<pair<int, int>> &pos, int start) { parent.assign(2*n, -1); rnk.assign(2*n, 0); parts = 0; for (int i = start; i < 2*n; ++i) { add_vertex(pos[i]); ++cnt[parts]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; vector<int> v[2] {vector<int>(n), vector<int> (n)}; for (int i = 0; i < 2; ++i) for (int j = 0; j < n; ++j) cin >> v[i][j]; vector<pair<int, int>> pos(2*n); for (int i = 0; i < 2; ++i) for (int j = 0; j < n; ++j) pos[v[i][j] - 1] = {i, j}; cnt.assign(2*n, 0); for (int i = 0; i < 2*n; ++i) calc(pos, i); for (int i = 1; i <= k; ++i) cout << cnt[i] << ' '; cout << '\n'; } |