#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int N = 2e5+10;
const int K = 11;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int S = 4;
int c[N][2], par[N], n, k;
ll ans[K];
pii x[N];
bool us[N];
int f(int x) {
if (par[x] == x) return x;
return par[x]=f(par[x]);
}
void merge(int x, int y) {
x=f(x), y=f(y);
if (x == y) return;
par[x]=y;
}
bool valid(int i, int j) {
return (0 <= i && i <= 1) && (0 <= j && j < n);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k;
for (int i=0; i<2; ++i) {
for (int j=0; j<n; ++j) {
cin>>c[j][i]; --c[j][i];
x[c[j][i]]={i, j};
}
}
for (int l=0; l<2*n; ++l) {
for (int h=0; h<n; ++h) {
par[c[h][0]]=c[h][0];
par[c[h][1]]=c[h][1];
}
memset(us, false, sizeof(us));
int tmp=0;
for (int r=l; r<2*n; ++r) {
int i=x[r].first, j=x[r].second;
us[r]=true;
bool emp=true;
for (int h=0; h<S; ++h) {
int ni=i+di[h], nj=(j+dj[h]+n)%n;
if (!valid(ni, nj) || !us[c[nj][ni]]) continue;
emp=false;
}
if (emp) ++tmp;
else {
set<int> s;
for (int h=0; h<S; ++h) {
int ni=i+di[h], nj=(j+dj[h]+n)%n;
if (!valid(ni, nj) || !us[c[nj][ni]]) continue;
if (f(c[nj][ni]) == f(c[j][i])) continue;
s.insert(f(c[nj][ni]));
merge(c[j][i], c[nj][ni]);
}
tmp-=s.size()-1;
}
if (tmp <= k) ++ans[tmp];
}
}
for (int i=1; i<=k; ++i) cout<<ans[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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> const int N = 2e5+10; const int K = 11; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const int S = 4; int c[N][2], par[N], n, k; ll ans[K]; pii x[N]; bool us[N]; int f(int x) { if (par[x] == x) return x; return par[x]=f(par[x]); } void merge(int x, int y) { x=f(x), y=f(y); if (x == y) return; par[x]=y; } bool valid(int i, int j) { return (0 <= i && i <= 1) && (0 <= j && j < n); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n>>k; for (int i=0; i<2; ++i) { for (int j=0; j<n; ++j) { cin>>c[j][i]; --c[j][i]; x[c[j][i]]={i, j}; } } for (int l=0; l<2*n; ++l) { for (int h=0; h<n; ++h) { par[c[h][0]]=c[h][0]; par[c[h][1]]=c[h][1]; } memset(us, false, sizeof(us)); int tmp=0; for (int r=l; r<2*n; ++r) { int i=x[r].first, j=x[r].second; us[r]=true; bool emp=true; for (int h=0; h<S; ++h) { int ni=i+di[h], nj=(j+dj[h]+n)%n; if (!valid(ni, nj) || !us[c[nj][ni]]) continue; emp=false; } if (emp) ++tmp; else { set<int> s; for (int h=0; h<S; ++h) { int ni=i+di[h], nj=(j+dj[h]+n)%n; if (!valid(ni, nj) || !us[c[nj][ni]]) continue; if (f(c[nj][ni]) == f(c[j][i])) continue; s.insert(f(c[nj][ni])); merge(c[j][i], c[nj][ni]); } tmp-=s.size()-1; } if (tmp <= k) ++ans[tmp]; } } for (int i=1; i<=k; ++i) cout<<ans[i]<<" "; cout<<"\n"; } |
English