#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 1e5; int a[2*MAX_N]; int loc[2*MAX_N]; int p[2*MAX_N]; int sz[2*MAX_N]; int n, k; int Find(int i) { if (i == p[i]) return i; p[i] = Find(p[i]); return p[i]; } bool Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; if (sz[x] > sz[y]) { p[y] = x; sz[x] += sz[y]; } else { p[x] = y; sz[y] += sz[x]; } return true; } vector<int> get_neighbors(int i) { if (i < n) { if (i == 0) { return {n-1, i+n, (i+1)%n}; } else { return {i-1, i+n, (i+1)%n}; } } else { if (i == n) { return {2*n-1, i-n, n+(i+1)%n}; } else { return {i-1, i-n, n+(i+1)%n}; } } } int main() { scanf("%d %d", &n, &k); vector<int> res(k+1); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); a[i]--; } for (int i = 0; i < n; ++i) { scanf("%d", &a[n+i]); a[n+i]--; } for (int i = 0; i < 2*n; ++i) { loc[a[i]] = i; } for (int l = 0; l < 2*n; ++l) { int n_sets = 0; for (int r = l; r < 2*n; ++r) { int i = loc[r]; p[i] = i; sz[i] = 1; n_sets++; auto ns = get_neighbors(i); for (auto j : ns) { if (a[j] >= l && a[j] < r) { bool joined = Union(i, j); if (joined) { n_sets--; } } } if (n_sets <= k) { res[n_sets]++; } } } for (int i = 1; i <= k; ++i) { printf("%d ", res[i]); } printf("\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 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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 1e5; int a[2*MAX_N]; int loc[2*MAX_N]; int p[2*MAX_N]; int sz[2*MAX_N]; int n, k; int Find(int i) { if (i == p[i]) return i; p[i] = Find(p[i]); return p[i]; } bool Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; if (sz[x] > sz[y]) { p[y] = x; sz[x] += sz[y]; } else { p[x] = y; sz[y] += sz[x]; } return true; } vector<int> get_neighbors(int i) { if (i < n) { if (i == 0) { return {n-1, i+n, (i+1)%n}; } else { return {i-1, i+n, (i+1)%n}; } } else { if (i == n) { return {2*n-1, i-n, n+(i+1)%n}; } else { return {i-1, i-n, n+(i+1)%n}; } } } int main() { scanf("%d %d", &n, &k); vector<int> res(k+1); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); a[i]--; } for (int i = 0; i < n; ++i) { scanf("%d", &a[n+i]); a[n+i]--; } for (int i = 0; i < 2*n; ++i) { loc[a[i]] = i; } for (int l = 0; l < 2*n; ++l) { int n_sets = 0; for (int r = l; r < 2*n; ++r) { int i = loc[r]; p[i] = i; sz[i] = 1; n_sets++; auto ns = get_neighbors(i); for (auto j : ns) { if (a[j] >= l && a[j] < r) { bool joined = Union(i, j); if (joined) { n_sets--; } } } if (n_sets <= k) { res[n_sets]++; } } } for (int i = 1; i <= k; ++i) { printf("%d ", res[i]); } printf("\n"); return 0; } |