#include <algorithm> #include <cstdio> #define DBG(X) using namespace std; int tab[2][100001]; int p[100001]; int w[100001]; int ret[100001]; pair<int, int> gdzie[100001]; int numsets = 0; int find(int x) { return (p[x] < 0) ? x : p[x] = find(p[x]); } void union_(int x, int y) { if ((x = find(x)) == (y = find(y))) return; numsets--; if (w[x] > w[y]) p[y] = x; else p[x] = y; if (w[x] == w[y]) w[y]++; } int main() { int n, k; scanf("%d %d", &n, &k); numsets = 2 * n; for (int i = 0; i < n; i++) { scanf("%d", tab[0] + i); tab[0][i]--; gdzie[tab[0][i]].first = 0; gdzie[tab[0][i]].second = i; } for (int i = 0; i < n; i++) { scanf("%d", tab[1] + i); tab[1][i]--; gdzie[tab[1][i]].first = 1; gdzie[tab[1][i]].second = i; } for (int i = 0; i < 2 * n; i++) { int d = 2 * n - 2; for (int j = 0; j < 2 * n; j++) { p[j] = w[j] = -1; } numsets = 2 * n; for (int j = i + 1; j < 2 * n; j++) { if (j == i + 1) { int r1 = gdzie[i].first; int c1 = gdzie[i].second; int r2 = gdzie[j].first; int c2 = gdzie[j].second; DBG(printf("i %d j %d r1 %d c1 %d r2 %d c2 %d\n", i,j,r1, c1, r2, c2);) if (r1 == r2 && ((c1 + 1) % n == c2 || (c1 - 1 + n) % n == c2) ) { DBG(printf("unia %d %d\n", i, j);) union_(i, j); } else if (r1 != r2 && c1 == c2) { DBG(printf("unia %d %d\n", i, j);) union_(i, j); } DBG(printf("numsets %d d %d\n", numsets, d);) ret[numsets - d]++; } else { int r2 = gdzie[j].first; int c2 = gdzie[j].second; int sas_left = (c2 - 1 + n) % n; int sas_right = (c2 + 1) % n; int sas_up_down = 1 - r2; DBG(printf("i %d j %d sas_left %d sas_right %d sas_up_down %d r2 %d c2 %d\n", i,j,sas_left, sas_right, sas_up_down,r2, c2); printf("upd %d left %d right %d\n", tab[sas_up_down][c2], tab[r2][sas_left], tab[r2][sas_right]);) if (i <= tab[sas_up_down][c2] && tab[sas_up_down][c2] < j) { union_(j, tab[sas_up_down][c2]); DBG(printf("unia %d %d\n", j, tab[sas_up_down][c2]);) } if (i <= tab[r2][sas_left] && tab[r2][sas_left] < j) { union_(j, tab[r2][sas_left]); DBG(printf("unia %d %d\n", j, tab[r2][sas_left]);) } if (i <= tab[r2][sas_right] && tab[r2][sas_right] < j) { union_(j, tab[r2][sas_right]); DBG(printf("unia %d %d\n", j, tab[r2][sas_right]);) } DBG(printf("numsets %d d %d\n", numsets, d);) ret[numsets - d]++; } d--; } } ret[1] += 2 * n; for (int i = 1; i <= k; i++) { printf("%d ", ret[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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include <algorithm> #include <cstdio> #define DBG(X) using namespace std; int tab[2][100001]; int p[100001]; int w[100001]; int ret[100001]; pair<int, int> gdzie[100001]; int numsets = 0; int find(int x) { return (p[x] < 0) ? x : p[x] = find(p[x]); } void union_(int x, int y) { if ((x = find(x)) == (y = find(y))) return; numsets--; if (w[x] > w[y]) p[y] = x; else p[x] = y; if (w[x] == w[y]) w[y]++; } int main() { int n, k; scanf("%d %d", &n, &k); numsets = 2 * n; for (int i = 0; i < n; i++) { scanf("%d", tab[0] + i); tab[0][i]--; gdzie[tab[0][i]].first = 0; gdzie[tab[0][i]].second = i; } for (int i = 0; i < n; i++) { scanf("%d", tab[1] + i); tab[1][i]--; gdzie[tab[1][i]].first = 1; gdzie[tab[1][i]].second = i; } for (int i = 0; i < 2 * n; i++) { int d = 2 * n - 2; for (int j = 0; j < 2 * n; j++) { p[j] = w[j] = -1; } numsets = 2 * n; for (int j = i + 1; j < 2 * n; j++) { if (j == i + 1) { int r1 = gdzie[i].first; int c1 = gdzie[i].second; int r2 = gdzie[j].first; int c2 = gdzie[j].second; DBG(printf("i %d j %d r1 %d c1 %d r2 %d c2 %d\n", i,j,r1, c1, r2, c2);) if (r1 == r2 && ((c1 + 1) % n == c2 || (c1 - 1 + n) % n == c2) ) { DBG(printf("unia %d %d\n", i, j);) union_(i, j); } else if (r1 != r2 && c1 == c2) { DBG(printf("unia %d %d\n", i, j);) union_(i, j); } DBG(printf("numsets %d d %d\n", numsets, d);) ret[numsets - d]++; } else { int r2 = gdzie[j].first; int c2 = gdzie[j].second; int sas_left = (c2 - 1 + n) % n; int sas_right = (c2 + 1) % n; int sas_up_down = 1 - r2; DBG(printf("i %d j %d sas_left %d sas_right %d sas_up_down %d r2 %d c2 %d\n", i,j,sas_left, sas_right, sas_up_down,r2, c2); printf("upd %d left %d right %d\n", tab[sas_up_down][c2], tab[r2][sas_left], tab[r2][sas_right]);) if (i <= tab[sas_up_down][c2] && tab[sas_up_down][c2] < j) { union_(j, tab[sas_up_down][c2]); DBG(printf("unia %d %d\n", j, tab[sas_up_down][c2]);) } if (i <= tab[r2][sas_left] && tab[r2][sas_left] < j) { union_(j, tab[r2][sas_left]); DBG(printf("unia %d %d\n", j, tab[r2][sas_left]);) } if (i <= tab[r2][sas_right] && tab[r2][sas_right] < j) { union_(j, tab[r2][sas_right]); DBG(printf("unia %d %d\n", j, tab[r2][sas_right]);) } DBG(printf("numsets %d d %d\n", numsets, d);) ret[numsets - d]++; } d--; } } ret[1] += 2 * n; for (int i = 1; i <= k; i++) { printf("%d ", ret[i]); } printf("\n"); return 0; } |