#include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, k, val[3][100005], rep[200005], rang[200005], ile, res[12]; pair<int, int> pos[200005]; int find(int a) { if (a == rep[a]) return a; else return rep[a] = find(rep[a]); } void merge(int a, int b) { int x = find(a); int y = find(b); if (x != y) { if (rang[x] > rang[y]) { rep[y] = x; rang[x] += rang[y]; } else { rep[x] = y; rang[y] += rang[x]; } ile--; } } int main() { qio; cin >> n >> k; for (int i = 1; i <= 2; i++) { for (int j = 1; j <= n; j++) { cin >> val[i][j]; pos[val[i][j]] = { i,j }; } } for (int i = 1; i <= 2 * n; i++) { ile = 0; for (int j = i; j <= 2 * n; j++) { rep[j] = j; rang[j] = 1; ile++; int x = pos[j].st; int y = pos[j].nd; int l = y - 1; int r = y + 1; int p = x - 1; if (l == 0) l = n; if (r == n + 1) r = 1; if (p == 0) p = 2; int a = val[x][l]; int b = val[x][r]; int c = val[p][y]; if (a >= i && a <= j) merge(a, j); if (b >= i && b <= j) merge(b, j); if (c >= i && c <= j) merge(c, j); if (ile <= k) res[ile]++; //cout << i << " " << j << " " << ile << endl; } //cout << i << endl; } for (int i = 1; i <= k; i++) cout << res[i] << " "; cout << endl; }
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 | #include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, k, val[3][100005], rep[200005], rang[200005], ile, res[12]; pair<int, int> pos[200005]; int find(int a) { if (a == rep[a]) return a; else return rep[a] = find(rep[a]); } void merge(int a, int b) { int x = find(a); int y = find(b); if (x != y) { if (rang[x] > rang[y]) { rep[y] = x; rang[x] += rang[y]; } else { rep[x] = y; rang[y] += rang[x]; } ile--; } } int main() { qio; cin >> n >> k; for (int i = 1; i <= 2; i++) { for (int j = 1; j <= n; j++) { cin >> val[i][j]; pos[val[i][j]] = { i,j }; } } for (int i = 1; i <= 2 * n; i++) { ile = 0; for (int j = i; j <= 2 * n; j++) { rep[j] = j; rang[j] = 1; ile++; int x = pos[j].st; int y = pos[j].nd; int l = y - 1; int r = y + 1; int p = x - 1; if (l == 0) l = n; if (r == n + 1) r = 1; if (p == 0) p = 2; int a = val[x][l]; int b = val[x][r]; int c = val[p][y]; if (a >= i && a <= j) merge(a, j); if (b >= i && b <= j) merge(b, j); if (c >= i && c <= j) merge(c, j); if (ile <= k) res[ile]++; //cout << i << " " << j << " " << ile << endl; } //cout << i << endl; } for (int i = 1; i <= k; i++) cout << res[i] << " "; cout << endl; } |