#include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() int tab[100009][2]; int ans[20]; pii pos[100009]; int uf[100009]; int Find(int x) { return x==uf[x]?x:uf[x]=Find(uf[x]); } void Union(int x, int y) { uf[Find(x)] = Find(y); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; for(int i=0;i<2;i++) { for(int j=0;j<n;j++) { cin >> tab[j][i]; pos[tab[j][i]] = {j, i}; } } for(int i=1;i<=2*n;i++) { for(int j=1;j<=2*n;j++) uf[j] = j; int res = 0; for(int j=i;j<=2*n;j++) { pii x = pos[j]; pii x1 = {x.st?x.st-1:n-1, x.nd}; pii x2 = {x.st, 1-x.nd}; pii x3 = {x.st==n-1?0:x.st+1, x.nd}; int n1 = tab[x1.st][x1.nd]; int n2 = tab[x2.st][x2.nd]; int n3 = tab[x3.st][x3.nd]; bool p1 = tab[x1.st][x1.nd]<=j and tab[x1.st][x1.nd]>=i; bool p2 = tab[x2.st][x2.nd]<=j and tab[x2.st][x2.nd]>=i; bool p3 = tab[x3.st][x3.nd]<=j and tab[x3.st][x3.nd]>=i; if(p1 && p2 && Find(n1)!=Find(n2)) { Union(n1, n2); res--; } if(p2 && p3 && Find(n2)!=Find(n3)) { Union(n2, n3); res--; } if(p1 && p3 && Find(n1)!=Find(n3)) { Union(n1, n3); res--; } if(p1) { Union(j, n1); } else if(p2) { Union(j, n2); } else if(p3) { Union(j, n3); } else { res++; } if(res <= k) ans[res]++; } } 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 | #include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() int tab[100009][2]; int ans[20]; pii pos[100009]; int uf[100009]; int Find(int x) { return x==uf[x]?x:uf[x]=Find(uf[x]); } void Union(int x, int y) { uf[Find(x)] = Find(y); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; for(int i=0;i<2;i++) { for(int j=0;j<n;j++) { cin >> tab[j][i]; pos[tab[j][i]] = {j, i}; } } for(int i=1;i<=2*n;i++) { for(int j=1;j<=2*n;j++) uf[j] = j; int res = 0; for(int j=i;j<=2*n;j++) { pii x = pos[j]; pii x1 = {x.st?x.st-1:n-1, x.nd}; pii x2 = {x.st, 1-x.nd}; pii x3 = {x.st==n-1?0:x.st+1, x.nd}; int n1 = tab[x1.st][x1.nd]; int n2 = tab[x2.st][x2.nd]; int n3 = tab[x3.st][x3.nd]; bool p1 = tab[x1.st][x1.nd]<=j and tab[x1.st][x1.nd]>=i; bool p2 = tab[x2.st][x2.nd]<=j and tab[x2.st][x2.nd]>=i; bool p3 = tab[x3.st][x3.nd]<=j and tab[x3.st][x3.nd]>=i; if(p1 && p2 && Find(n1)!=Find(n2)) { Union(n1, n2); res--; } if(p2 && p3 && Find(n2)!=Find(n3)) { Union(n2, n3); res--; } if(p1 && p3 && Find(n1)!=Find(n3)) { Union(n1, n3); res--; } if(p1) { Union(j, n1); } else if(p2) { Union(j, n2); } else if(p3) { Union(j, n3); } else { res++; } if(res <= k) ans[res]++; } } for(int i=1;i<=k;i++) { cout << ans[i] << " "; } cout << "\n"; } |