#include <bits/stdc++.h> using namespace std; const int N = 1e5+7; const int K = 11; int A[2][N],cnt[K]; pair<int,int> pos[N*2]; bool vis[N*2]; int P[N*2]; int F(int x){ if (x==P[x]){ return x; } return P[x] = F(P[x]); } int cmp = 0; void unite(int a,int b){ a = F(a); b = F(b); if (a!=b){ cmp -= 1; } P[a] = b; } int n,k; void add(int el){ int x = pos[el].first, y = pos[el].second; vis[el] = 1; cmp += 1; if (vis[A[1-x][y]]){ unite(el,A[1-x][y]); } if (vis[A[x][(y+1)%n]]){ unite(el,A[x][(y+1)%n]); } if (vis[A[x][(y+n-1)%n]]){ unite(el,A[x][(y+n-1)%n]); } } void solve(){ cin>>n>>k; for(int i = 1;i<=n*2;i+=1){ P[i] = i; } for(int c = 0;c<2;c+=1){ for(int i = 0;i<n;i+=1){ cin>>A[c][i]; pos[A[c][i]] = {c,i}; } } map<int,int> M; for(int i = 1;i<=2*n;i+=1){ cmp = 0; for(int j = i;j<=2*n;j+=1){ add(j); if (cmp<=k){ cnt[cmp] += 1; } } for(int j = i;j<=2*n;j+=1){ P[j] = j; vis[j] = 0; } } for(int i = 1;i<=k;i+=1){ cout<<cnt[i]<<' '; } cout<<endl; } signed main(){ int tt = 1; while(tt--){ solve(); } }
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 | #include <bits/stdc++.h> using namespace std; const int N = 1e5+7; const int K = 11; int A[2][N],cnt[K]; pair<int,int> pos[N*2]; bool vis[N*2]; int P[N*2]; int F(int x){ if (x==P[x]){ return x; } return P[x] = F(P[x]); } int cmp = 0; void unite(int a,int b){ a = F(a); b = F(b); if (a!=b){ cmp -= 1; } P[a] = b; } int n,k; void add(int el){ int x = pos[el].first, y = pos[el].second; vis[el] = 1; cmp += 1; if (vis[A[1-x][y]]){ unite(el,A[1-x][y]); } if (vis[A[x][(y+1)%n]]){ unite(el,A[x][(y+1)%n]); } if (vis[A[x][(y+n-1)%n]]){ unite(el,A[x][(y+n-1)%n]); } } void solve(){ cin>>n>>k; for(int i = 1;i<=n*2;i+=1){ P[i] = i; } for(int c = 0;c<2;c+=1){ for(int i = 0;i<n;i+=1){ cin>>A[c][i]; pos[A[c][i]] = {c,i}; } } map<int,int> M; for(int i = 1;i<=2*n;i+=1){ cmp = 0; for(int j = i;j<=2*n;j+=1){ add(j); if (cmp<=k){ cnt[cmp] += 1; } } for(int j = i;j<=2*n;j+=1){ P[j] = j; vis[j] = 0; } } for(int i = 1;i<=k;i+=1){ cout<<cnt[i]<<' '; } cout<<endl; } signed main(){ int tt = 1; while(tt--){ solve(); } } |