#include <bits/stdc++.h> using namespace std; const int N = 100000 + 9; const int N2 = N + N; const int K = 10 + 9; int board[2][N]; int row[N2]; int col[N2]; int res[K]; class FU { int p[N2]; public: void init(int a, int b) { for (int i=a; i<=b; ++i) p[i] = -1; } int f(int x) { int r = x; while (p[r] >= 0) r = p[r]; int t; while (x != r) { t = p[x]; p[x] = r; x = t; } return r; } void u(int x, int y) { x = f(x); y = f(y); if (x == y) return; if (p[x] < p[y]) { p[x] += p[y]; p[y] = x; } else { p[y] += p[x]; p[x] = y; } } }; FU fu; int main() { int n; int k; scanf("%d%d", &n, &k); int n2 = n + n; for (int r=0; r<2; ++r) for (int c=0; c<n; ++c) { int &cur = board[r][c]; scanf("%d", &cur); row[cur] = r; col[cur] = c; } for (int i=1; i<=k; ++i) res[i] = 0; for (int a=1; a<=n2; ++a) { fu.init(a, n2); unordered_set<int> s; for (int b=a; b<=n2; ++b) { int r = row[b]; int c = col[b]; int v1 = board[r][(c+1)%n]; int v2 = board[r][(c+n-1)%n]; int v3 = board[1-r][c]; int v[3]; int vn = 0; if (a <= v1 && v1 <= b) v[vn++] = v1; if (a <= v2 && v2 <= b) v[vn++] = v2; if (a <= v3 && v3 <= b) v[vn++] = v3; for (int i=0; i<vn; ++i) s.erase(fu.f(v[i])); for (int i=0; i<vn; ++i) fu.u(b, v[i]); for (int i=0; i<vn; ++i) s.insert(fu.f(v[i])); s.insert(fu.f(b)); if (s.size() <= k) ++res[s.size()]; // fprintf(stderr, "%d %d => %d\n", a, b, s.size()); } } for (int i=1; i<=k; ++i) printf("%d%s", res[i], i<k ? " " : "\n"); return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int N = 100000 + 9; const int N2 = N + N; const int K = 10 + 9; int board[2][N]; int row[N2]; int col[N2]; int res[K]; class FU { int p[N2]; public: void init(int a, int b) { for (int i=a; i<=b; ++i) p[i] = -1; } int f(int x) { int r = x; while (p[r] >= 0) r = p[r]; int t; while (x != r) { t = p[x]; p[x] = r; x = t; } return r; } void u(int x, int y) { x = f(x); y = f(y); if (x == y) return; if (p[x] < p[y]) { p[x] += p[y]; p[y] = x; } else { p[y] += p[x]; p[x] = y; } } }; FU fu; int main() { int n; int k; scanf("%d%d", &n, &k); int n2 = n + n; for (int r=0; r<2; ++r) for (int c=0; c<n; ++c) { int &cur = board[r][c]; scanf("%d", &cur); row[cur] = r; col[cur] = c; } for (int i=1; i<=k; ++i) res[i] = 0; for (int a=1; a<=n2; ++a) { fu.init(a, n2); unordered_set<int> s; for (int b=a; b<=n2; ++b) { int r = row[b]; int c = col[b]; int v1 = board[r][(c+1)%n]; int v2 = board[r][(c+n-1)%n]; int v3 = board[1-r][c]; int v[3]; int vn = 0; if (a <= v1 && v1 <= b) v[vn++] = v1; if (a <= v2 && v2 <= b) v[vn++] = v2; if (a <= v3 && v3 <= b) v[vn++] = v3; for (int i=0; i<vn; ++i) s.erase(fu.f(v[i])); for (int i=0; i<vn; ++i) fu.u(b, v[i]); for (int i=0; i<vn; ++i) s.insert(fu.f(v[i])); s.insert(fu.f(b)); if (s.size() <= k) ++res[s.size()]; // fprintf(stderr, "%d %d => %d\n", a, b, s.size()); } } for (int i=1; i<=k; ++i) printf("%d%s", res[i], i<k ? " " : "\n"); return 0; } |