#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
const int K = 11;
int tab[N][2];
bool vis[N][2];
pair<int,int> pos[N];
ll ciek[K];
struct Fau{
vector<int> par,cnt;
Fau(int n){
par.resize(n+5);
cnt.resize(n+5);
for(int i=0; i<n+5; ++i){
par[i] = i;
cnt[i] = 1;
}
}
int find(int a){
if(a!=par[a]) par[a] = find(par[a]);
return par[a];
}
bool onion(int a, int b){
a = find(a);
b = find(b);
if(a==b) return false;
if(cnt[a] < cnt[b]) swap(a,b);
par[b] = a;
cnt[a] += cnt[b];
return true;
}
};
int main(){
ios_base::sync_with_stdio(0);
int n,k;
cin >> n >> k;
for(int y=1; y>=0; --y){
for(int x=0; x<n; ++x){
cin >> tab[x][y];
pos[tab[x][y]] = {x,y};
}
}
for(int l=1; l<=2*n; ++l){
int cnt=0;
Fau fau(2*n);
for(int y=0; y<2; ++y)
for(int x=0; x<n; ++x)
vis[x][y] = 0;
for(int r=l; r<=2*n; ++r){
++cnt;
auto& [x,y] = pos[r];
if(vis[(x-1+n)%n][y])
cnt -= fau.onion(r, tab[(x-1+n)%n][y]);
if(vis[(x+1)%n][y])
cnt -= fau.onion(r, tab[(x+1)%n][y]);
if(y!=1 && vis[x][y+1])
cnt -= fau.onion(r, tab[x][y+1]);
if(y!=0 && vis[x][y-1])
cnt -= fau.onion(r, tab[x][y-1]);
if(cnt < K){
ciek[cnt]++;
}
vis[x][y] = 1;
}
}
for(int i=1; i<=k; ++i){
cout << ciek[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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5; const int K = 11; int tab[N][2]; bool vis[N][2]; pair<int,int> pos[N]; ll ciek[K]; struct Fau{ vector<int> par,cnt; Fau(int n){ par.resize(n+5); cnt.resize(n+5); for(int i=0; i<n+5; ++i){ par[i] = i; cnt[i] = 1; } } int find(int a){ if(a!=par[a]) par[a] = find(par[a]); return par[a]; } bool onion(int a, int b){ a = find(a); b = find(b); if(a==b) return false; if(cnt[a] < cnt[b]) swap(a,b); par[b] = a; cnt[a] += cnt[b]; return true; } }; int main(){ ios_base::sync_with_stdio(0); int n,k; cin >> n >> k; for(int y=1; y>=0; --y){ for(int x=0; x<n; ++x){ cin >> tab[x][y]; pos[tab[x][y]] = {x,y}; } } for(int l=1; l<=2*n; ++l){ int cnt=0; Fau fau(2*n); for(int y=0; y<2; ++y) for(int x=0; x<n; ++x) vis[x][y] = 0; for(int r=l; r<=2*n; ++r){ ++cnt; auto& [x,y] = pos[r]; if(vis[(x-1+n)%n][y]) cnt -= fau.onion(r, tab[(x-1+n)%n][y]); if(vis[(x+1)%n][y]) cnt -= fau.onion(r, tab[(x+1)%n][y]); if(y!=1 && vis[x][y+1]) cnt -= fau.onion(r, tab[x][y+1]); if(y!=0 && vis[x][y-1]) cnt -= fau.onion(r, tab[x][y-1]); if(cnt < K){ ciek[cnt]++; } vis[x][y] = 1; } } for(int i=1; i<=k; ++i){ cout << ciek[i] << ' '; } return 0; } |
English