#include <bits/stdc++.h> using namespace std; const int MX=200100; int n,m,cnt,i,j,x,y,v,a[2][MX],wc[MX],wr[MX],p[MX],rk[MX],r[MX]; int fs(int i) { if (p[i]!=i) p[i]=fs(p[i]); return p[i]; } int un(int x, int y) { if (x==y) return 0; if (rk[x]>=rk[y]) { p[y]=x; if (rk[x]==rk[y]) ++rk[x]; } else p[x]=y; return -1; } int main() { scanf("%d%d",&n,&m); for (i=0; i<2; i++) for (j=0; j<n; j++) { scanf("%d",&a[i][j]); wr[a[i][j]]=i; wc[a[i][j]]=j; } for (i=1; i<=2*n; i++) { for (j=i; j<=2*n; j++) { p[j]=j; rk[j]=0; } ++r[cnt=1]; for (j=i+1; j<=2*n; j++) { x=wr[j]; y=wc[j]; ++cnt; if (a[x^1][y]>=i && a[x^1][y]<=j) cnt+=un(fs(j),fs(a[x^1][y])); v=y+1; if (v==n) v=0; if (a[x][v]>=i && a[x][v]<=j) cnt+=un(fs(j),fs(a[x][v])); v=y-1; if (v<0) v=n-1; if (a[x][v]>=i && a[x][v]<=j) cnt+=un(fs(j),fs(a[x][v])); ++r[cnt]; } } for (i=1; i<=m; i++) printf("%d ",r[i]); 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 | #include <bits/stdc++.h> using namespace std; const int MX=200100; int n,m,cnt,i,j,x,y,v,a[2][MX],wc[MX],wr[MX],p[MX],rk[MX],r[MX]; int fs(int i) { if (p[i]!=i) p[i]=fs(p[i]); return p[i]; } int un(int x, int y) { if (x==y) return 0; if (rk[x]>=rk[y]) { p[y]=x; if (rk[x]==rk[y]) ++rk[x]; } else p[x]=y; return -1; } int main() { scanf("%d%d",&n,&m); for (i=0; i<2; i++) for (j=0; j<n; j++) { scanf("%d",&a[i][j]); wr[a[i][j]]=i; wc[a[i][j]]=j; } for (i=1; i<=2*n; i++) { for (j=i; j<=2*n; j++) { p[j]=j; rk[j]=0; } ++r[cnt=1]; for (j=i+1; j<=2*n; j++) { x=wr[j]; y=wc[j]; ++cnt; if (a[x^1][y]>=i && a[x^1][y]<=j) cnt+=un(fs(j),fs(a[x^1][y])); v=y+1; if (v==n) v=0; if (a[x][v]>=i && a[x][v]<=j) cnt+=un(fs(j),fs(a[x][v])); v=y-1; if (v<0) v=n-1; if (a[x][v]>=i && a[x][v]<=j) cnt+=un(fs(j),fs(a[x][v])); ++r[cnt]; } } for (i=1; i<=m; i++) printf("%d ",r[i]); return 0; } |