#include<bits/stdc++.h> #define pb push_back #define popb pop_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vii vector<pii> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define vvvb vector<vvb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() using namespace std; const int N = 100200; int n, k; int X[] = {0, 0, 1, -1}; int Y[] = {1, -1, 0, 0}; int v[2][N]; vii pr; vi uf; vb zp; int global; int find(int nf){return (uf[nf] == nf ? nf : uf[nf] = find(uf[nf]));} void un(int a, int b) { a = find(a); b = find(b); if(a != b) { uf[a] = b; global--; } } vi res(20); bool som(int x, int y) { if(x < 0 or x >= 2) return false; return zp[v[x][(y+n)%n]]; } void add(int e) { zp[e] = true; auto [x, y] = pr[e]; global++; for(int l = 0; l < 4; l++) if(som(x+X[l], y+Y[l])) un(e, v[x+X[l]][(y+Y[l]+n)%n]); } void sett() { uf = vi(2*n+10); zp = vb(n*2+10); global = 0; for(int j = 1; j <= 2*n; j++) uf[j] = j; } int main() { turbo cin >> n >> k; pr = vii(2*n+10); for(int i = 0; i < 2; i++) for(int j = 0; j < n; j++) { cin >> v[i][j]; pr[v[i][j]] = {i, j}; } for(int i = 1; i <= n*2; i++) { sett(); for(int j = i; j <= 2*n; j++) { add(j); if(global < 20) res[global]++; } } for(int i = 1; i <= k; i++) cout << res[i] << " "; }
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 | #include<bits/stdc++.h> #define pb push_back #define popb pop_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vii vector<pii> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define vvvb vector<vvb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() using namespace std; const int N = 100200; int n, k; int X[] = {0, 0, 1, -1}; int Y[] = {1, -1, 0, 0}; int v[2][N]; vii pr; vi uf; vb zp; int global; int find(int nf){return (uf[nf] == nf ? nf : uf[nf] = find(uf[nf]));} void un(int a, int b) { a = find(a); b = find(b); if(a != b) { uf[a] = b; global--; } } vi res(20); bool som(int x, int y) { if(x < 0 or x >= 2) return false; return zp[v[x][(y+n)%n]]; } void add(int e) { zp[e] = true; auto [x, y] = pr[e]; global++; for(int l = 0; l < 4; l++) if(som(x+X[l], y+Y[l])) un(e, v[x+X[l]][(y+Y[l]+n)%n]); } void sett() { uf = vi(2*n+10); zp = vb(n*2+10); global = 0; for(int j = 1; j <= 2*n; j++) uf[j] = j; } int main() { turbo cin >> n >> k; pr = vii(2*n+10); for(int i = 0; i < 2; i++) for(int j = 0; j < n; j++) { cin >> v[i][j]; pr[v[i][j]] = {i, j}; } for(int i = 1; i <= n*2; i++) { sett(); for(int j = i; j <= 2*n; j++) { add(j); if(global < 20) res[global]++; } } for(int i = 1; i <= k; i++) cout << res[i] << " "; } |