#include <bits/stdc++.h>
#define ft first
#define sc second
#define pb push_back
#define FOR(i,n) for(int i=0; i<n; i++)
#define watch(x) cout << (#x) << " == " << (x) << endl
#define int short
typedef long long ll;
typedef long double ld;
using namespace std;
const ll N = 1e5;
int tab[2*N+5][2];
int rep[2*N+5];
int height[2*N+5];
pair<int,int> nr[2*N+5];
ll ciek[11]={0};
int pn;
inline void comp_path(int &a, int &r){
while (rep[a] != a){
a = rep[a];
rep[a] = r;
}
}
inline int get_rep(int &a){
int it=0;
int b = a;
while (rep[b] != b){
it++;
b = rep[b];
}
if (it >= pn) {comp_path(a,b);}
return b;
}
inline void uni(int &a, int &b){
if (height[a] == height[b]){
rep[b]=a;
height[a]++;
} else if (height[b] > height[a]){
rep[a]=b;
} else {
rep[b]=a;
}
}
inline bool check(int a, int b){
int rep_a = get_rep(a);
int rep_b = get_rep(b);
if (rep_a != rep_b){
uni(rep_a, rep_b);
return 1;
}
return 0;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, x, y, ile;
cin>>n>>k;
pn=sqrt(n);
FOR(y,2){
for(int x=1; x<=n; x++){
cin>>tab[x][y];
nr[tab[x][y]]={x,y};
}
}
tab[0][0]=tab[n][0];
tab[n+1][0]=tab[1][0];
tab[0][1]=tab[n][1];
tab[n+1][1]=tab[1][1];
ciek[1]=(n<<1);
for (int l=1; l<=(n<<1); l++){
memset(height, 0, ((n<<1)+1)*sizeof(int));
for(int i=l; i<=(n<<1); i++){
rep[i]=i;
}
ile=1;
for (int r=l+1; r<=(n<<1); r++){
x = nr[r].ft; y = nr[r].sc;
int rep_a = get_rep(tab[x][y]);
ile++;
int b = tab[x-1][y];
if (l <= b && b <= r){
ile -= check(rep_a, b);
}
b = tab[x+1][y];
if (l <= b && b <= r){
ile -= check(rep_a, b);
}
b = tab[x][1-y];
if (l <= b && b <= r){
ile -= check(rep_a, b);
}
if (ile <= k){
ciek[ile]++;
}
}
}
FOR(i,k){
cout<<ciek[i+1]<<" ";
}
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 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <bits/stdc++.h> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define watch(x) cout << (#x) << " == " << (x) << endl #define int short typedef long long ll; typedef long double ld; using namespace std; const ll N = 1e5; int tab[2*N+5][2]; int rep[2*N+5]; int height[2*N+5]; pair<int,int> nr[2*N+5]; ll ciek[11]={0}; int pn; inline void comp_path(int &a, int &r){ while (rep[a] != a){ a = rep[a]; rep[a] = r; } } inline int get_rep(int &a){ int it=0; int b = a; while (rep[b] != b){ it++; b = rep[b]; } if (it >= pn) {comp_path(a,b);} return b; } inline void uni(int &a, int &b){ if (height[a] == height[b]){ rep[b]=a; height[a]++; } else if (height[b] > height[a]){ rep[a]=b; } else { rep[b]=a; } } inline bool check(int a, int b){ int rep_a = get_rep(a); int rep_b = get_rep(b); if (rep_a != rep_b){ uni(rep_a, rep_b); return 1; } return 0; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, x, y, ile; cin>>n>>k; pn=sqrt(n); FOR(y,2){ for(int x=1; x<=n; x++){ cin>>tab[x][y]; nr[tab[x][y]]={x,y}; } } tab[0][0]=tab[n][0]; tab[n+1][0]=tab[1][0]; tab[0][1]=tab[n][1]; tab[n+1][1]=tab[1][1]; ciek[1]=(n<<1); for (int l=1; l<=(n<<1); l++){ memset(height, 0, ((n<<1)+1)*sizeof(int)); for(int i=l; i<=(n<<1); i++){ rep[i]=i; } ile=1; for (int r=l+1; r<=(n<<1); r++){ x = nr[r].ft; y = nr[r].sc; int rep_a = get_rep(tab[x][y]); ile++; int b = tab[x-1][y]; if (l <= b && b <= r){ ile -= check(rep_a, b); } b = tab[x+1][y]; if (l <= b && b <= r){ ile -= check(rep_a, b); } b = tab[x][1-y]; if (l <= b && b <= r){ ile -= check(rep_a, b); } if (ile <= k){ ciek[ile]++; } } } FOR(i,k){ cout<<ciek[i+1]<<" "; } return 0; } |
English