#include <bits/stdc++.h> using namespace std; vector<bool> vis; vector<int> col; vector<vector<int>> Adj; vector<int> parent; vector<int> idx; vector<int> rankv; int l, r; void make_set(int v) { parent[v] = v; rankv[v] = 0; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rankv[a] < rankv[b]) swap(a, b); parent[b] = a; if (rankv[a] == rankv[b]) rankv[a]++; } } void brutdfs(int v) { if (l == 1 && r == 3) { int dupa = 5 + 5; } vis[v] = true; for (int u : Adj[v]) { if (l <= col[u] && col[u] <= r && !vis[u]) brutdfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> res(k + 1); vector<vector<int>> in; idx.resize(2 * n); col.resize(2 * n); in.resize(2); Adj.resize(2 * n); parent.resize(2 * n); rankv.resize(2 * n); for (int i = 0; i < 2 * n; i++) { cin >> col[i]; col[i]--; idx[col[i]] = i; } for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { int x1 = n * i + ((j + 1) % n); int x2 = n * i + ((j - 1 + n) % n); int x3 = (i ^ 1) * n + j; Adj[i * n + j].push_back(x1); Adj[i * n + j].push_back(x2); Adj[i * n + j].push_back(x3); } } for (int i = 0; i < 2 * n; i++) { for (int v = 0; v < 2 * n; v++) { make_set(v); } int comp = 0; for (int j = i; j < 2 * n; j++) { l = i; r = j; int v = idx[j]; comp++; for (int u : Adj[v]) { if (l <= col[u] && col[u] <= r) { if (find_set(u) != find_set(v)) { comp--; union_sets(u, v); } } } if (comp <= k) res[comp]++; } } 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 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 | #include <bits/stdc++.h> using namespace std; vector<bool> vis; vector<int> col; vector<vector<int>> Adj; vector<int> parent; vector<int> idx; vector<int> rankv; int l, r; void make_set(int v) { parent[v] = v; rankv[v] = 0; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rankv[a] < rankv[b]) swap(a, b); parent[b] = a; if (rankv[a] == rankv[b]) rankv[a]++; } } void brutdfs(int v) { if (l == 1 && r == 3) { int dupa = 5 + 5; } vis[v] = true; for (int u : Adj[v]) { if (l <= col[u] && col[u] <= r && !vis[u]) brutdfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> res(k + 1); vector<vector<int>> in; idx.resize(2 * n); col.resize(2 * n); in.resize(2); Adj.resize(2 * n); parent.resize(2 * n); rankv.resize(2 * n); for (int i = 0; i < 2 * n; i++) { cin >> col[i]; col[i]--; idx[col[i]] = i; } for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { int x1 = n * i + ((j + 1) % n); int x2 = n * i + ((j - 1 + n) % n); int x3 = (i ^ 1) * n + j; Adj[i * n + j].push_back(x1); Adj[i * n + j].push_back(x2); Adj[i * n + j].push_back(x3); } } for (int i = 0; i < 2 * n; i++) { for (int v = 0; v < 2 * n; v++) { make_set(v); } int comp = 0; for (int j = i; j < 2 * n; j++) { l = i; r = j; int v = idx[j]; comp++; for (int u : Adj[v]) { if (l <= col[u] && col[u] <= r) { if (find_set(u) != find_set(v)) { comp--; union_sets(u, v); } } } if (comp <= k) res[comp]++; } } for (int i = 1; i <= k; i++) { cout << res[i] << " "; } cout << '\n'; } |