#include <bits/stdc++.h>
using namespace std;
int n, k;
int a, b, cnt;
int t[200007][2];
int res[200007];
int pom[200007][2];
int spoj[200007];
int w[200007];
int f(int v){
if(v == spoj[v])
return v;
return spoj[v] = f(spoj[v]);
}
void u(int v1, int v2){
spoj[f(v1)] = f(v2);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a;
t[a][0] = i-1;
t[a][1] = 1;
}
for(int i = 1; i <= n; i++){
cin >> a;
t[a][0] = i-1;
t[a][1] = 0;
}
int x, y;
for(int i = 1; i <= 2*n; i++){
for(int j = 0; j < n; j++){
pom[j][0] = 0;
pom[j][1] = 0;
}
for(int j = 1; j <= 2*n; j++)
spoj[j] = j;
cnt = 0;
for(int j = i; j <= 2*n; j++){
// cout << i << ' ' << j << ' ';
x = t[j][0];
y = t[j][1];
// cout << "x=" << x << " y=" << y << '\n';
// cout << pom[x][(y+1)%2] << ' ' << pom[(x+1)%n][y] << ' ' << pom[(x-1+n)%n][y] << '\n';
// cout << f(1) << ' ' << f(3) << '\n';
pom[x][y] = j;
a = 0; b = 0;
if(pom[(x+1)%n][y]){
b++;
w[f(pom[(x+1)%n][y])]++;
}
if(pom[(x-1+n)%n][y]){
b++;
w[f(pom[(x-1+n)%n][y])]++;
if(w[f(pom[(x-1+n)%n][y])] > 1)
a++;
}
if(pom[x][(y+1)%2]){
b++;
w[f(pom[x][(y+1)%2])]++;
if(w[f(pom[x][(y+1)%2])] > 1)
a++;
}
// cout << "a=" << a << " b=" << b << '\n';
cnt -= b-1-a;
if(pom[(x+1)%n][y])
w[f(pom[(x+1)%n][y])]--;
if(pom[(x-1+n)%n][y])
w[f(pom[(x-1+n)%n][y])]--;
if(pom[x][(y+1)%2])
w[f(pom[x][(y+1)%2])]--;
if(pom[(x+1)%n][y])
u(j, pom[(x+1)%n][y]);
if(pom[(x-1+n)%n][y])
u(j, pom[(x-1+n)%n][y]);
if(pom[x][(y+1)%2])
u(j, pom[x][(y+1)%2]);
// cout << "res=" << cnt << '\n';
res[cnt]++;
}
}
for(int i = 1; i <= k; i++)
cout << res[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 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 | #include <bits/stdc++.h> using namespace std; int n, k; int a, b, cnt; int t[200007][2]; int res[200007]; int pom[200007][2]; int spoj[200007]; int w[200007]; int f(int v){ if(v == spoj[v]) return v; return spoj[v] = f(spoj[v]); } void u(int v1, int v2){ spoj[f(v1)] = f(v2); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> a; t[a][0] = i-1; t[a][1] = 1; } for(int i = 1; i <= n; i++){ cin >> a; t[a][0] = i-1; t[a][1] = 0; } int x, y; for(int i = 1; i <= 2*n; i++){ for(int j = 0; j < n; j++){ pom[j][0] = 0; pom[j][1] = 0; } for(int j = 1; j <= 2*n; j++) spoj[j] = j; cnt = 0; for(int j = i; j <= 2*n; j++){ // cout << i << ' ' << j << ' '; x = t[j][0]; y = t[j][1]; // cout << "x=" << x << " y=" << y << '\n'; // cout << pom[x][(y+1)%2] << ' ' << pom[(x+1)%n][y] << ' ' << pom[(x-1+n)%n][y] << '\n'; // cout << f(1) << ' ' << f(3) << '\n'; pom[x][y] = j; a = 0; b = 0; if(pom[(x+1)%n][y]){ b++; w[f(pom[(x+1)%n][y])]++; } if(pom[(x-1+n)%n][y]){ b++; w[f(pom[(x-1+n)%n][y])]++; if(w[f(pom[(x-1+n)%n][y])] > 1) a++; } if(pom[x][(y+1)%2]){ b++; w[f(pom[x][(y+1)%2])]++; if(w[f(pom[x][(y+1)%2])] > 1) a++; } // cout << "a=" << a << " b=" << b << '\n'; cnt -= b-1-a; if(pom[(x+1)%n][y]) w[f(pom[(x+1)%n][y])]--; if(pom[(x-1+n)%n][y]) w[f(pom[(x-1+n)%n][y])]--; if(pom[x][(y+1)%2]) w[f(pom[x][(y+1)%2])]--; if(pom[(x+1)%n][y]) u(j, pom[(x+1)%n][y]); if(pom[(x-1+n)%n][y]) u(j, pom[(x-1+n)%n][y]); if(pom[x][(y+1)%2]) u(j, pom[x][(y+1)%2]); // cout << "res=" << cnt << '\n'; res[cnt]++; } } for(int i = 1; i <= k; i++) cout << res[i] << ' '; return 0; } |
English