#include <bits/stdc++.h> #define f first #define s second using namespace std; const int MXN=1e5+1; int rep[MXN], ile[MXN]; int find(int x) { if (x!=rep[x]) rep[x]=find(rep[x]); return rep[x]; } bool uni(int a, int b) { a=find(a); b=find(b); if (a==b) return 0; if (ile[a]<ile[b]) swap(a, b); rep[b]=a; ile[a]+=ile[b]; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin>>n>>k; vector<int>a[2]; vector<pair<bool, int>>wh(2*n+1); vector<long long>ans(k+1); for (int i=0; i<2; i++) { a[i].resize(n+2); for (int j=1; j<=n; j++) { cin>>a[i][j]; wh[a[i][j]]={i, j}; } a[i][0]=a[i][n]; a[i][n+1]=a[i][1]; } for (int i=1; i<=2*n; i++) { for (int j=i; j<=2*n; j++) ile[i]=1, rep[j]=j; int ss=0; for (int j=i; j<=2*n; j++) { ss++; int x=wh[j].f, y=wh[j].s; if (a[x][y-1]>=i && a[x][y-1]<=j) ss-=uni(j, a[x][y-1]); if (a[x][y+1]>=i && a[x][y+1]<=j) ss-=uni(j, a[x][y+1]); if (a[x^1][y]>=i && a[x^1][y]<=j) ss-=uni(j, a[x^1][y]); //~ cout<<i<<" "<<j<<" "<<ss<<"\n";//<<a[x][y-1]<<" "<<a[x][y+1]<<" "<<a[x^1][y]<<"\n"; if (ss<=k) ans[ss]++; } } 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 | #include <bits/stdc++.h> #define f first #define s second using namespace std; const int MXN=1e5+1; int rep[MXN], ile[MXN]; int find(int x) { if (x!=rep[x]) rep[x]=find(rep[x]); return rep[x]; } bool uni(int a, int b) { a=find(a); b=find(b); if (a==b) return 0; if (ile[a]<ile[b]) swap(a, b); rep[b]=a; ile[a]+=ile[b]; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin>>n>>k; vector<int>a[2]; vector<pair<bool, int>>wh(2*n+1); vector<long long>ans(k+1); for (int i=0; i<2; i++) { a[i].resize(n+2); for (int j=1; j<=n; j++) { cin>>a[i][j]; wh[a[i][j]]={i, j}; } a[i][0]=a[i][n]; a[i][n+1]=a[i][1]; } for (int i=1; i<=2*n; i++) { for (int j=i; j<=2*n; j++) ile[i]=1, rep[j]=j; int ss=0; for (int j=i; j<=2*n; j++) { ss++; int x=wh[j].f, y=wh[j].s; if (a[x][y-1]>=i && a[x][y-1]<=j) ss-=uni(j, a[x][y-1]); if (a[x][y+1]>=i && a[x][y+1]<=j) ss-=uni(j, a[x][y+1]); if (a[x^1][y]>=i && a[x^1][y]<=j) ss-=uni(j, a[x^1][y]); //~ cout<<i<<" "<<j<<" "<<ss<<"\n";//<<a[x][y-1]<<" "<<a[x][y+1]<<" "<<a[x^1][y]<<"\n"; if (ss<=k) ans[ss]++; } } for (int i=1; i<=k; i++) cout<<ans[i]<<" "; cout<<"\n"; } |